Mike
Mike

Reputation: 346

C# Determine the type of a generic list

I have a C# method that I need to pass various lists into. 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). 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"

Upvotes: 3

Views: 3331

Answers (5)

sm_
sm_

Reputation: 2602

.GetType() will get you a Type and you can get a lot of descriptions about the 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;
}

Upvotes: 3

archil
archil

Reputation: 39501

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();
        });

Upvotes: 1

Curt
Curt

Reputation: 5722

This might work if you treat them all as objects, using the intrinsic ToString() method for your string building.

Upvotes: 0

Karl Anderson
Karl Anderson

Reputation: 34844

If the parameter (will call it list for discussion's sake) coming into the method is List<T> then you can do the following:

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

If you just have a generic T class, then you can always just go with this:

Type typeParameterType = typeof(T);

Upvotes: 5

Jakub Konecki
Jakub Konecki

Reputation: 46008

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.

What you do with this information is up to you.

Upvotes: 1

Related Questions