English 中文(简体)
匿名 直到经营时间为止财产不明的类型
原标题:Anonymous Type where Properties are unknown until runtime

我有以下设想,想建立一个数据组,然后在运行时间充实内容。

我面临的问题是,因为我不知道在我建立电网之前哪些领域? 我不知道如何正确确定项目来源。 如你在以下法典中看到的那样,我把外地名称列为一栏,然后通过我的物品,此时,我想为我把财产(所谓的外地名称)划入外地名称价值的每一项目创造匿名类型。

foreach (string fieldName in listViewFieldCollection)
{
    DataGridTextColumn newColumn = new DataGridTextColumn
                                       {
                                           Header = fieldName,
                                           Binding = new Binding(fieldName)
                                       };

    dataGrid.Columns.Add(newColumn);
}

List<object> list = new List<object>();

foreach (ListItem listItem in listItems)
{
    foreach (string fieldName in listViewFieldCollection)
    {
        list.Add(new
                     {
                         // I want to be able to dynamically build Anonymous type properties here
                         fieldName = listItem[fieldName]
                     });
    }
}

dataGrid.ItemsSource = list;

例如。 如果我有所谓的标题和链接,那么我就想到这份名单。 增(新)

    list.Add(new
                 {
                     Title = listItem["Title"],
                     Link = listItem["Link"]
                 });

但是,如果田间是管理人员、职衔和薪金,它就应该做同样的工作。

    list.Add(new
                 {
                     Manager = listItem["Manager"],
                     Title = listItem["Title"],
                     Salary = listItem["Salary"],
                 });
最佳回答

象利用思考一样,它能够发挥作用。 下面的法典是你可能开始的。 狙击手将穿透物体上的所有财产。

        foreach (var currentPropertyInformation in source.GetType().GetProperties())
        {
            if ((currentPropertyInformation.GetGetMethod() == null) || (currentPropertyInformation.GetSetMethod() == null)) 
                continue;

            if (string.Compare(currentPropertyInformation.Name, "Item") != 0) // handle non-enumerable properties
            {
                // place this snippet in method and yield info?
            }
            else // handle enumerable properties
            {
            }
        }
问题回答

如果没有反思或制定法典,就不可能实现。

new
                 {
                     Manager = listItem["Manager"],
                     Title = listItem["Title"],
                     Salary = listItem["Salary"],
                 }

转换成3个田级,然后确定这些田地的线路很少。 这是由汇编者编制的。 因此,在运行期间,你可以这样做。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签