C# 使用泛型类型对象的反射获取属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18126723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:23:13  来源:igfitidea点击:

get properties using reflections for generic type object

c#genericsreflection

提问by user2486535

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

并将该结果传递为 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

我什至想在这里使用第一个模式,即 type(T).GetProperties

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

所以,请解决任何问题......................

采纳答案by Marc Gravell

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

typeof(T)作品的反思;这是一个基于您的更简单的示例,但是(重要的是)runnable。它输出:

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