Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get properties using reflections for generic type object

I am having a generic class in which I have a function to get properties of the generic object passed.It is as below.

public class ExportToCsv<T>        
        where T: class
{
    public ExportToCsv(List<T> obj)
    {
            this.Data = obj;       
    }

    public StringBuilder CreateRows()
   {
       IEnumerable<PropertyInfo> properties = typeof(T).GetProperties();
   }
}

It works fine and returns me properties if I pass the object by selecting from a object(class) like below

//GetLeadingRoutingRecords returns a class/object
var result = from obj in GetLeadRoutingRecords()
                    select new
                    {
                        LeadRoutingId = obj.LeadRoutingID,
                        Make = obj.Make
                     };

and pass that result as result.ToList();

but when I try to create my own anonymous object by creating a class for the properties like below it doesn't work not returning any properties

Note : the below code is being called in a loop and it well functioning and being passed to the above function can see all values by debugging.

public CsvReport function return(){
    return new CsvReport
                {
                    ShopName = this.val,
                    TargetVehicleName = val
                 }.ToList();
}

class that I wrote for the above anonymous object is like below :

public class CsvReport
    {
        public string ShopName { get; set; }
        public string TargetVehicleName { get; set; }
    }

so in this case its not working, i am selecting first record and getting properties like below

this.Data.First().GetType().GetProperties();

I want to use the first pattern even here, which is type(T).GetProperties

So, any work around please........................

like image 414
user2486535 Avatar asked Aug 08 '13 12:08

user2486535


People also ask

How do you find the property of a generic type?

Use the IsGenericType property to determine whether the type is generic, and use the IsGenericTypeDefinition property to determine whether the type is a generic type definition. Get an array that contains the generic type arguments, using the GetGenericArguments method.

How do I use reflection to call a generic method?

The first step to dynamically invoking a generic method with reflection is to use reflection to get access to the MethodInfo of the generic method. To do that simply do this: var methodInfo = typeof(ClassWithGenericMethod). GetMethod("MethodName");

Is it possible to inherit from a generic type?

An attribute cannot inherit from a generic class, nor can a generic class inherit from an attribute.

How do you indicate that a class has a generic type parameter?

A generic type is declared by specifying a type parameter in an angle brackets after a type name, e.g. TypeName<T> where T is a type parameter.


1 Answers

Reflection on typeof(T) works fine; here's a simpler example based on yours, but (importantly) runnable. It outputs:

ShopName
TargetVehicleName

code:

using System;
using System.Collections.Generic;
public class CsvReport
{
    public string ShopName { get; set; }
    public string TargetVehicleName { get; set; }
}
class ExportToCsv<T>
{
    List<T> data;
    public ExportToCsv(List<T> obj)
    {
        data = obj;
    }
    public void WritePropNames()
    {
        foreach (var prop in typeof(T).GetProperties())
        {
            Console.WriteLine(prop.Name);
        }
    }

}
static class Program
{
    static void Main()
    {
        var obj = new List<CsvReport>();
        obj.Add(new CsvReport { ShopName = "Foo", TargetVehicleName = "Bar" });
        new ExportToCsv<CsvReport>(obj).WritePropNames();
    }
}
like image 189
Marc Gravell Avatar answered Sep 19 '22 20:09

Marc Gravell