简体   繁体   English

C#确定通用列表的类型

[英]C# Determine the type of a generic list

I have a C# method that I need to pass various lists into. 我有一个C#方法,我需要将各种列表传递给。 The type of lists will be different very often. 列表的类型会经常不同。 The method itself is in a different dll and cannot know the class objects contained in the lists (no references, no using statements). 方法本身位于不同的dll中,无法知道列表中包含的类对象(没有引用,没有使用语句)。 The method needs to read each of the members of each of the items in a list and build and return a string based off of that. 该方法需要读取列表中每个项目的每个成员,并构建并返回基于该项目的字符串。

My question is how can get the metadata for for these various objects at compiletime / runtime? 我的问题是如何在编译时/运行时获取这些各种对象的元数据? Will reflection do that? 反思会这样做吗?

Also, once I get the metadata, how do i use it to actually use the variables? 此外,一旦我获得元数据,我如何使用它来实际使用变量?

Thank you 谢谢

EDIT: currently I am using it like the following: 编辑:目前我正在使用它如下:

public string GetData(List<User> list){
//...
    List<RowData> rows = new List<RowData>();
    foreach (User item in list)
    {
        RowData row = new RowData();
        row.id = count++;
        row.cell = new string[3];
        row.cell[0] = item.ID.ToString();
        row.cell[1] = item.name;
        row.cell[2] = item.age.ToString();

        rows.Add(row);
     }
//...
    return new JavaScriptSerializer().Serialize(rows.ToArray());

Right now this is very specific. 现在这是非常具体的。 I want to replace this with generics so I can pass items other than "User" 我想用泛型替换它,所以我可以传递“用户”以外的项目

If the parameter (will call it list for discussion's sake) coming into the method is List<T> then you can do the following: 如果进入方法的参数(将其称为讨论list )是List<T>那么您可以执行以下操作:

Type type = list.GetType().GetGenericArguments()[0];

If you just have a generic T class, then you can always just go with this: 如果您只有一个通用的T类,那么您可以随时使用:

Type typeParameterType = typeof(T);

.GetType() will get you a Type and you can get a lot of descriptions about the type. .GetType()会为你提供一个Type ,你可以获得很多关于类型的描述。

You can also discover members of the instances you have using reflection 您还可以使用反射发现您拥有的实例的成员

Please be precise with what you want more exactly ? 请准确地说明您想要的更多内容?

EDIT : Here is a way, try making it an extenstion method it would be better 编辑:这是一种方法,尝试使它成为一种扩展方法,它会更好

public static List<RowData> ToDataTable<T>(IEnumerable<T> source)
{
    System.Reflection.PropertyInfo[] properties = typeof(T).GetProperties();
    List<RowData> rows = new List<JQGridRow>();


    foreach (var item in source)
    {
        RowData row = new RowData();
        row.cells = new string[properties.Length];
        int i=0;
        foreach (var prop in properties)
        {  
             row.cells[i] = prop.GetValue(item, null);i++;
        }
       rows.Add(row);
    }
    return rows;
}

Yes, you can use reflection to obtain the type of an object at runtime. 是的,您可以使用反射在运行时获取对象的类型。

Simple call to object.GetType() will give you the type of this instance. object.GetType()简单调用将为您提供此实例的类型。

What you do with this information is up to you. 您对此信息的处理取决于您自己。

Maybe something like this? 也许是这样的?

    public string GetData<T>(IEnumerable<T> list, Func<T, RowData> fillRow)
    {
        List<RowData> rows = new List<JQGridRow>();
        foreach (User item in list)
        {
            RowData row = new RowData();
            row.id = count++;

            fillRow(row);

            rows.Add(row);
        }
        //...
        return new JavaScriptSerializer().Serialize(rows.ToArray());
    }

And use it like 并使用它

        string s = GetData(users, row =>
        {
            row.cell = new string[3];
            row.cell[0] = item.ID.ToString();
            row.cell[1] = item.name;
            row.cell[2] = item.age.ToString();
        });

如果您将它们全部视为对象,则可以使用内部ToString()方法进行字符串构建。

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

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