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........................
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.
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");
An attribute cannot inherit from a generic class, nor can a generic class inherit from an attribute.
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.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With