如何在C#类型和名称

时间:2015-06-11 15:47:54

标签: c# linq list dynamically-generated

enter image description here

我想创建一个动态创建的对象列表。

然后我想将此对象添加到列表中。

所以在上面的例子中,我想创建一个名为Image

的类型实体列表
        List<DataConsistencyModel> dataConsistencyModels = new List<DataConsistencyModel>
        {
              new DataConsistencyModel{   PropertyName = "Logo",TypeOf = typeof (Entity),ValidFunction =  x => x.GroupId == null},                      
        };

        foreach (DataConsistencyModel dcm in dataConsistencyModels)
        {

            List<TypeOf> PropertyName = Data.Where(dcm.ValidFunction).ToList();

        }   

1 个答案:

答案 0 :(得分:0)

使用词典列表将是一个有用的解决方法!

        var myObject = new List<Dictionary<string,object>>
        {
            new Dictionary<string, object>
            {
                {"val1", 1},
                {"val2", true},
                {"val3", "String1"}
            },
            new Dictionary<string, object>
            {
                {"val1", 2},
                {"val2", true},
                {"val3", "String2"}
            }
        };
        // you can add as many Values as you want
        myObject.Add(new Dictionary<string, object>
        {
            {"val1", 2},
            {"val2", true},
            {"val3", "my new String"},
            {"val4", "my new String 2"}
        });
        // Use Keys to access values (Don't Forget casting)
        var output = (String) myObject[0]["val3"];