简体   繁体   English

动态创建匿名类型的属性

[英]Dynamically create properties in anonymous type

I want to create a new type using anonymous types. 我想使用匿名类型创建一个新类型。

This is my code: 这是我的代码:

 var t = paramaterList.Select(x => x).ToArray().Select(item => new
             {
                 item,
             });

I need to create properties for var t based on the name of item. 我需要根据商品名称为var t创建属性。

For example: 例如:

if paramaterList.Select(x => x).ToArray() contained 3 items: FirstName, Surname, Age 如果paramaterList.Select(x => x).ToArray()包含3个项目:名字,姓氏,年龄

This should create properties for each of those items: 这应该为每个项目创建属性:

paramaterList.Select(x => x).ToArray().Select(item => new
             {

             });

Anonymous types are still statically typed, even though they don't have a name that you can use to refer to them by. 匿名类型仍然是静态类型,即使它们没有可用来引用它们的名称。 If you don't know the fields that this object is going to have at compile time, then you can't create a variable of an anonymous type to hold that data. 如果您在编译时不知道该对象将要具有的字段,则无法创建匿名类型的变量来保存该数据。 You'll need to store the data in some form of data structure, such as a dictionary, that doesn't require you to know what the values are at compile time. 您将需要以某种形式的数据结构存储数据,例如字典,它不需要您知道编译时的值。

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

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