C#-动态添加对象(添加动态属性名称) [英] C# - Adding objects dynamically (adding dynamic property names)

查看:745
本文介绍了C#-动态添加对象(添加动态属性名称)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一些动态的ExpandoObject.我遇到了一个问题.

I'm trying to create some dynamic ExpandoObject. I've encountered a certain problem.

由于我不知道对象中这些不同属性的名称应该是什么,所以我不能这样做:

As I don't know what the name of these different properties in my objects should be, I can't do like this:

var list = new ArrayList();

var obj = new ExpandoObject();
obj.ID = 1,
obj.Product = "Pie",
obj.Days = 1,
obj.QTY = 65

list.Add(obj);

让我解释一下我的情况:我希望从随机数据库中获取数据(我不知道该数据,而是根据从UI中获取的信息构建一个连接字符串),因此我不知道我要获取哪些数据需要得到.这可能是数据库表的示例

Let me explain my situation: I wish to get data from a random DB (I don't know which, but building a connection string from the information I get from the UI), therefore I don't know what data I need to get. This could be an example of a DB table

餐桌特卖

  • ID:int,
  • 产品:nvarchar(100),
  • 天数:整数,
  • 数量:bigint

这可能是另一个例子:

表Foobar

  • Id:int,
  • 天数:整数
  • 数量:bigint
  • Product_Id:int
  • 部门编号:int

如您所见,我不知道数据库是什么样的(这是100%匿名的,因此它必须是100%动态的),而我要返回的数据应该看起来像一个结构良好的JSON,例如所以:

As you see, I don't know what the DB looks like (this is 100% anonymous, therefore it needs to be 100% dynamic), and the data I want to return should look like a well constructed JSON, like so:

[
  {
    "ID": 1,
    "Product": "Pie"
    "Days": 1,
    "QTY": 65
  },
  {
    "ID": 2,
    "Product": "Melons"
    "Days": 5,
    "QTY": 12
  }
]

或者,用另一个示例:

[
  {
    "ID": 1,
    "Days": 2,
    "QTY": 56,
    "Product_Id": 5,
    "Department_Id": 2
  }
  {
    "ID": 2,
    "Days": 6,
    "QTY": 12,
    "Product_Id": 2,
    "Department_Id": 5
  }
]

我曾尝试使用这些ExpandoObject,但似乎无法使其正常工作,因为我无法完成此问题顶部的说明(我不知道名字属性).有没有办法让我这样说:

I've tried working with these ExpandoObjects, but can't seem to make it work, as I can't do what's illustrated in the top of this question (I don't know the names of the properties). Is there a way for me to say something like:

var obj = new ExpandoObject();
var propName = "Product";

var obj.propName = "Pie"

Console.WriteLine("Let's print!: " + obj.Product);

//OUTPUT
Let's print!: Pie

有人能解决这种情况吗?有没有解决方案,或者只是对结构的指导?

Does anyone have a solution, og simply guidance to a structure, that might solve this situation?

推荐答案

除了创建ExpandoObject或其他某种动态类型,您还可以创建一个List<Dictionary<string, object>>,其中每个Dictionary<string, object>包含所需的名称/值对进行序列化.然后使用 Json.NET (或

Rather than creating an ExpandoObject or some other dynamic type, you could create a List<Dictionary<string, object>> where each Dictionary<string, object> contains the name/value pairs you want to serialize. Then serialize to JSON using Json.NET (or JavaScriptSerializer, though that is less flexible):

        var list = new List<Dictionary<string, object>>();

        // Build a dictionary entry using a dictionary initializer: https://msdn.microsoft.com/en-us/library/bb531208.aspx
        list.Add(new Dictionary<string, object> { { "ID", 1 }, {"Product", "Pie"}, {"Days", 1}, {"QTY", 65} });

        // Build a dictionary entry incrementally
        // See https://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx
        var dict = new Dictionary<string, object>();
        dict["ID"] = 2;
        dict["Product"] = "Melons";
        dict["Days"] = 5;
        dict["QTY"] = 12;
        list.Add(dict);

        Console.WriteLine(JsonConvert.SerializeObject(list, Formatting.Indented));
        Console.WriteLine(new JavaScriptSerializer().Serialize(list));

第一个输出:

[
  {
    "ID": 1,
    "Product": "Pie",
    "Days": 1,
    "QTY": 65
  },
  {
    "ID": 2,
    "Product": "Melons",
    "Days": 5,
    "QTY": 12
  }
]

第二个输出不带缩进的相同内容:

The second outputs the same without the indentation:

[{"ID":1,"Product":"Pie","Days":1,"QTY":65},{"ID":2,"Product":"Melons","Days":5,"QTY":12}]

这篇关于C#-动态添加对象(添加动态属性名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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