简体   繁体   English

类属性/字段可以是 C# 4.0 中的匿名类型吗?

[英]Can a class property/field be of anonymous type in C# 4.0?

As in:如:

public class MyClass {

  private static var MyProp = new {item1 = "a", item2 = "b"};

}

Note: The above doesn't compile nor work (the var cannot be used there), it's only to show my point.注意:以上既不能编译也不能工作(不能在那里使用 var),这只是为了表明我的观点。

Update : Just to clarify the question, I had already tried using更新:只是为了澄清这个问题,我已经尝试使用

private static dynamic MyProp = new {item1 = "a", item2 = "b"};

and this works, but it doesn't generate intellisense because of the dynamic typing.这有效,但由于动态类型,它不会生成智能感知。 I am aware that anonymous typing is just a compiler trick, so I hoped I could use this trick to my advantage by declaring a structured field without having to declare a class beforehand (mainly because there's only going to be one instance of this particular kind of field).我知道匿名类型只是一个编译器技巧,所以我希望我可以通过声明一个结构化字段而不需要事先声明一个类来利用这个技巧来发挥我的优势(主要是因为只会有这种特殊类型的一个实例场)。 I can see now that it's not possible, but I'm not sure why that is.我现在可以看到这是不可能的,但我不确定为什么会这样。 If the compiler is simply generating an implicit type for an anonymous object, it should be fairly simply to have the compiler generate this implicit type for a field.如果编译器只是为匿名对象生成隐式类型,那么让编译器为字段生成此隐式类型应该相当简单。

It sounds like you could be asking one or two questions so I'll try and address them both.听起来你可能会问一两个问题,所以我会尝试同时解决它们。

Can a class field be strongly typed to an anonymous type类字段可以强类型化为匿名类型吗

No. Anonymous type names cannot be stated in C# code (hence anonymous).不可以。匿名类型名称不能在 C# 代码中声明(因此是匿名的)。 The only way to statically type them is静态输入它们的唯一方法是

  1. Generic type inferencee泛型类型推断
  2. Use of the var keyword var关键字的使用

Neither of these are applicable to the field of a type.这些都不适用于类型的字段。

Can a class field be initialized with an anonymous type expression?可以使用匿名类型表达式初始化类字段吗?

Yes.是的。 The field just needs to be declared to a type compatible with anonymous types: object for example该字段只需要声明为与匿名类型兼容的类型:例如object

public class MyClass { 
  private static object MyProp = new {item1 = "a", item2 = "b"}; 
} 

No, any member should be a strongly typed.不,任何成员都应该是强类型的。

You might go for dynamic type to give your member a chance to be evaluated at runtime though.不过,您可能会选择动态类型,让您的成员有机会在运行时进行评估。

Edit : Members should be Explicitly Typed.编辑:成员应该显式键入。

在 C# 7 中,您终于可以这样做:

private (string Login, string Password) _credentials = (Login: "123", Password: "123");

How about usingTuple<string, string> instead?改用Tuple<string, string>怎么样?

public class MyClass {

  private static Tuple<string, string> MyProp = Tuple.Create("a", "b");

}

If this is C# 4, look into the dynamic keyword.如果这是 C# 4,请查看 dynamic 关键字。

public class MyClass 
{ 
  private static dynamic MyProp = new {item1 = "a", item2 = "b"}; 
} 

However, as soon as you do this you lose any sort of type-safety and handy compiler checks.然而,一旦你这样做,你就会失去任何类型安全和方便的编译器检查。

An property (or field) of a named class can't have an anonymous type, because such a type can't be referred to in code.命名类的属性(或字段)不能有匿名类型,因为这样的类型不能在代码中引用。 However , an anonymous type can contain properties of anonymous type:但是,匿名类型可以包含匿名类型的属性:

var myObj = new
{
    Foo = new { Name = "Joe", Age = 20 }
};

But such a construct has no practical use outside the local scope...但是这样的构造在本地范围之外没有实际用途......

No, anonymous types cannot be exposed outside of their declaring function as anything other than object .不,匿名类型不能作为object以外的任何东西在其声明函数之外公开。 While the anonymous type is just syntactical sugar and does, in fact, create a concrete type, this type is never known (nor available) to the developer, and thus can't be used.虽然匿名类型只是语法糖,并且实际上创建了一个具体类型,但开发人员永远不知道(也不可用)这种类型,因此无法使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM