如何将动态成员名称和值添加到ExpandoObject类的实例? [英] How to add dynamic member name and value to an instance of ExpandoObject class?

查看:202
本文介绍了如何将动态成员名称和值添加到ExpandoObject类的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说说存储在如下数组中的成员名称列表,

Let’s say list of member names stored in an array like below,

string[] myMembers = { "ChapterName", "Medium", "LastName", "OrderID" };

我编写了以下代码来生成动态类和成员以及随机值.

I wrote the following code to generate dynamic class and members along with random values.

var myDynamicClassList = new List<ExpandoObject>();
        foreach (var MemberName in myMembers)
        {
            dynamic dynamicClass = new ExpandoObject();
            dynamicClass.MemberName = new Random().Next();
            myDynamicClassList.Add(dynamicClass);
        }

为了显示上述myDynamicClassList的输出,我编写了以下代码.

In order to display output of the above myDynamicClassList I wrote the following code.

foreach (var x in myDynamicClassList)
        {
            foreach (var property in (IDictionary<String, Object>)x)
            {
                Console.WriteLine(property.Key + ": " + property.Value);
            }
        }

这样显示输出

会员名:123465461

MemberName : 123465461

会员名:564613611

MemberName : 564613611

会员名:134654321

MemberName : 134654321

会员名:786451214

MemberName : 786451214

但是我期望的输出不是上面的输出,而是下面的

But Instead of above output I am expecting the output like below

ChapterName:123465461

ChapterName : 123465461

中等:564613611

Medium : 564613611

姓氏:134654321

LastName : 134654321

OrderID:786451214

OrderID : 786451214

这是我的问题,是否可以在c#中向动态类添加动态成员名称.如果可以的话,请让我知道,否则请指导我完成这项工作.

Here is my question, is it possible to add dynamic member name to a dynamic class in c#. If it is possible please let me know, if not please guide me to complete this job.

非常感谢您在高级方面的帮助.

I really appreciate your help in advanced.

推荐答案

类似的问题(向ExpandoObject动态添加属性"),您可以通过直接使用IDictionary来做到这一点:

As described in a similar question ("Dynamically adding properties to an ExpandoObject"), you can do this by using IDictionary directly:

string[] myMembers = { "ChapterName", "Medium", "LastName", "OrderID" };
var myDynamicClassList = new List<ExpandoObject>();
Random random = new Random();
foreach (var MemberName in myMembers)
{
    IDictionary<string, object> dynamicClass = (IDictionary<string, object>)(new ExpandoObject());
    dynamicClass.Add(MemberName, random.Next());
    myDynamicClassList.Add((ExpandoObject)dynamicClass);
}
foreach (var x in myDynamicClassList)
{
    foreach (var property in (IDictionary<String, Object>)x)
    {
        Console.WriteLine(property.Key + ": " + property.Value);
    }
}

但是,您应该知道此方法可能存在的已知内存限制,如上面链接的文章的注释或

However, you should be aware of possible known memory limitations with this method as described in the comments of the post linked above, or in the Microsoft issue. I would possibly rethink the design to consider what properties are actually needed, or simply using a Dictionary if it's feasible (to avoid the dynamic ExpandoObject entirely).

我也将您的Random移到了循环之外. 在循环内创建Random的新实例可能不会为您提供结果您期望...

I also moved your Random outside of the loop. Creating new instances of Random inside a loop may not give you the results you expect...

这篇关于如何将动态成员名称和值添加到ExpandoObject类的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆