简体   繁体   English

C#和匿名对象的数组

[英]C# and arrays of anonymous objects

What does such an expression mean? 这样的表达是什么意思?

obj.DataSource = new[]
{
    new {Text = "Silverlight", Count = 10, Link="/Tags/Silverlight" },
    new {Text = "IIS 7", Count = 11, Link="http://iis.net" },
    new {Text = "IE 8", Count = 12, Link="/Tags/IE8" },
    new {Text = "C#", Count = 13, Link="/Tags/C#" },
    new {Text = "Azure", Count = 13, Link="?Tag=Azure" }
};

Especially these lines: new {Text = "IIS 7"... } 特别是这些行:new {Text =“IIS 7”...}

How can I create an array like this manually to suit this DataSource. 如何手动创建这样的数组以适应此DataSource。

First, let's reformat that code a little: 首先,让我们重新格式化一下代码

obj.DataSource = new[]
{
    new {  Text = "Silverlight",  Count = 10,  Link = "/Tags/Silverlight"  },
    new {  Text = "IIS 7",        Count = 11,  Link = "http://iis.net"     }, 
    new {  Text = "IE 8",         Count = 12,  Link = "/Tags/IE8"          }, 
    new {  Text = "C#",           Count = 13,  Link = "/Tags/C#"           },
    new {  Text = "Azure",        Count = 13,  Link = "?Tag=Azure"         } 
};

This does not look like a multi-dimensional array, but rather like an array of 5 objects. 这看起来不像是一个多维数组,而是一个包含5个对象的数组。 These objects inside the array are of an anonymous type , created and initialized using new { ... } . 数组中的这些对象是匿名类型 ,使用new { ... }创建和初始化。

Concerning your question how you can manually create such an array to suit the data source: you seem to be doing exactly that with the above code. 关于你的问题如何手动创建这样一个数组以适应数据源:你似乎正在用上面的代码完成这个。

That's not a multidimensional array. 那不是一个多维数组。 That's an array of objects which have been created using object initializers with anonymous types . 这是使用具有匿名类型的对象初始值设定项创建的对象数组。

Looks like an array of anonymous types. 看起来像一个匿名类型的数组。

http://msdn.microsoft.com/en-us/library/bb397696.aspx http://msdn.microsoft.com/en-us/library/bb397696.aspx

Just to add: Anonymous types are converted by the compiler to a real object. 只是添加:匿名类型由编译器转换为真实对象。 So the code will be changed to something equivalent of this (MUCH simplified, only to show that the compiler will create an actual class): 所以代码将被更改为相当于此的东西(MUCH简化,只是为了表明编译器将创建一个实际的类):

internal sealed class AnonymousClass1
{
    internal string Text { get; set; }
    internal int Count { get; set; }
    internal string Link { get; set; }
}

And your code will then be changed to: 然后您的代码将更改为:

obj.DataSource = new AnonymousClass1[]
{
    new AnonymousClass1 {Text = "Silverlight", Count = 10, Link="/Tags/Silverlight" },
    new AnonymousClass1 {Text = "IIS 7", Count = 11, Link="http://iis.net" },
    new AnonymousClass1 {Text = "IE 8", Count = 12, Link="/Tags/IE8" },
    new AnonymousClass1 {Text = "C#", Count = 13, Link="/Tags/C#" },
    new AnonymousClass1 {Text = "Azure", Count = 13, Link="?Tag=Azure" }
};

In one of my programs, I have code like this (simplified!): 在我的一个程序中,我有这样的代码(简化!):

var myObjects = new []{
    new { Id = Guid.NewGuid(), Title = "Some Title", Description = string.Empty },
    new { Id = Guid.NewGuid(), Title = "Another Title", Description = string.Empty },
    new { Id = Guid.NewGuid(), Title = "Number 3", Description = "Better than No2, but not much" }
}

foreach(var myObject in myObjects) {
    DoSomeThingWith(myObject.Title);
}

This works because it is just another class (I even get IntelliSense) behind the scenes. 这是有效的,因为它只是幕后的另一个类(我甚至得到了IntelliSense)。 The benefit is obviously that I just saved myself from creating a class for this object. 显而易见的是,我只是为自己创建了一个用于此对象的类。 In my example, all objects need to look the same as well. 在我的例子中,所有对象也需要看起来一样。 (Obviously, doing this for any public members would be a bad idea as the compiler might change the name of the anonymous class if you add/remove some) (显然,对任何公共成员执行此操作都是一个坏主意,因为如果添加/删除某些内容,编译器可能会更改匿名类的名称)

It's making a new object array, containing a group of anonymous objects. 它正在创建一个新的对象数组,其中包含一组匿名对象。

new {Text = "Azure", Count = 13, Link="?Tag=Azure" }

is not creating a hash like similar syntax in say php would, but rather real a real object with the properties Test, Count, and Link set. 不是像php那样创建类似语法的哈希,而是实际上是一个具有属性Test,Count和Link设置的真实对象。

A good primer for anonymous objects can be found here 这里可以找到一个很好的匿名对象入门

You should be able to use the same syntax to create new structures like this, the property values do not have to be constants: 您应该能够使用相同的语法来创建这样的新结构,属性值不必是常量:

string text = "Azure";
int count = 13;
string link =  "?Tag=Azure";
new {Text = text, Count = count, Link=link }

To return such an array from a function I used: 从我使用的函数返回这样一个数组:

public object GetAnonymousArray()
{
    var tbl = new List<object>();
    while (___)
    {
        //fill array in cycle
        tbl.Add(new {Text = someString, Count = someInt, Link = link});
    }  
    return tbl;
}

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

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