C# 和匿名对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2035017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:52:44  来源:igfitidea点击:

C# and arrays of anonymous objects

c#asp.net.netarraysanonymous-types

提问by Yevhen

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.

如何手动创建这样的数组以适合此数据源。

采纳答案by Mark Byers

That's not a multidimensional array. That's an array of objects which have been created using object initializers with anonymous types.

那不是多维数组。这是一个使用匿名类型的对象初始值设定项创建的对象数组。

回答by Kris Krause

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

回答by stakx - no longer contributing

First, let's reformat that codea 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. These objects inside the array are of an anonymous type, created and initialized using new { ... }.

这看起来不像一个多维数组,而是一个包含 5 个对象的数组。数组中的这些对象是匿名类型,使用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.

关于您如何手动创建这样一个数组以适应数据源的问题:您似乎正在使用上面的代码做到这一点。

回答by anq

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 中的类似语法那样创建哈希,而是创建一个具有测试、计数和链接属性的真实对象。

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 }

回答by Yevhen

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;
}

回答by Michael Stum

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):

补充一点:匿名类型由编译器转换为真实对象。因此,代码将更改为与此等效的内容(大大简化,只是为了表明编译器将创建一个实际的类):

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. 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)

这是有效的,因为它只是幕后的另一个类(我什至得到了 IntelliSense)。好处显然是我刚刚避免为这个对象创建一个类。在我的示例中,所有对象也需要看起来相同。(显然,为任何公共成员执行此操作将是一个坏主意,因为如果添加/删除一些,编译器可能会更改匿名类的名称)