C# 如何使用列表从通用方法读取值<;T>;作为参数

C# 如何使用列表从通用方法读取值<;T>;作为参数,c#,generics,C#,Generics,可能是个愚蠢的问题,我可以读取列表参数中的所有属性,但不能读取字段中的值 这就是结构 public class TestRecord { public string StringTest { get; set; } public int IntegerTest { get; set; } public DateTime DateTimeTest { get; set; } } 通用方法 public void

可能是个愚蠢的问题,我可以读取列表参数中的所有属性,但不能读取
字段中的值

这就是结构

    public class TestRecord {
          public string StringTest { get; set; }
          public int IntegerTest { get; set; }
          public DateTime DateTimeTest { get; set; }
    }
通用方法

    public void TestOfT<T>(List<T> pList) where T:class, new() {
        T xt = (T)Activator.CreateInstance(typeof(T));
        foreach (var tp in pList[0].GetType().GetProperties()) {
        // System.Reflection.PropertyInfo pi = xt.GetType().GetProperty("StringTest");
        // object s = pi.GetValue(tp, null) ;  -- failed
            Debug.WriteLine(tp.Name);
            Debug.WriteLine(tp.PropertyType);
            Debug.WriteLine(tp.GetType().Name);
        }
     }
    public void TestTCode()  {
        List<TestRecord> rec = new List<TestRecord>();
        rec.Add(new TestRecord() {
             StringTest = "string",
             IntegerTest = 1,
             DateTimeTest = DateTime.Now
        });
        TestOfT<TestRecord>(rec);
    }
public void TestOfT(List pList),其中T:class,new(){
T xt=(T)Activator.CreateInstance(typeof(T));
foreach(pList[0]中的var tp.GetType().GetProperties()){
//System.Reflection.PropertyInfo pi=xt.GetType().GetProperty(“StringTest”);
//对象s=pi.GetValue(tp,null);--失败
Debug.WriteLine(tp.Name);
Debug.WriteLine(tp.PropertyType);
Debug.WriteLine(tp.GetType().Name);
}
}
通用方法的测试代码

    public void TestOfT<T>(List<T> pList) where T:class, new() {
        T xt = (T)Activator.CreateInstance(typeof(T));
        foreach (var tp in pList[0].GetType().GetProperties()) {
        // System.Reflection.PropertyInfo pi = xt.GetType().GetProperty("StringTest");
        // object s = pi.GetValue(tp, null) ;  -- failed
            Debug.WriteLine(tp.Name);
            Debug.WriteLine(tp.PropertyType);
            Debug.WriteLine(tp.GetType().Name);
        }
     }
    public void TestTCode()  {
        List<TestRecord> rec = new List<TestRecord>();
        rec.Add(new TestRecord() {
             StringTest = "string",
             IntegerTest = 1,
             DateTimeTest = DateTime.Now
        });
        TestOfT<TestRecord>(rec);
    }
public void TestTCode(){
List rec=新列表();
rec.Add(新的TestRecord(){
StringTest=“string”,
整数测试=1,
DateTimeTest=DateTime.Now
});
TestOfT(rec);
}
感谢您的帮助。

public void TestOfT(List pList)其中T:class,new(){
public void TestOfT<T>(List<T> pList) where T:class, new() {
    var xt = Activator.CreateInstance(typeof(T));
    foreach (var tp in pList[0].GetType().GetProperties()) {
        Debug.WriteLine(tp.Name);
        Debug.WriteLine(tp.PropertyType);
        Debug.WriteLine(tp.GetType().Name);
        Debug.WriteLine(tp.GetValue(pList[0], null));
    }
 }
var xt=Activator.CreateInstance(typeof(T)); foreach(pList[0]中的var tp.GetType().GetProperties()){ Debug.WriteLine(tp.Name); Debug.WriteLine(tp.PropertyType); Debug.WriteLine(tp.GetType().Name); Debug.WriteLine(tp.GetValue(pList[0],null)); } }
公共void TestOfT(列表pList),其中T:class,new(){
var xt=Activator.CreateInstance(typeof(T));
foreach(pList[0]中的var tp.GetType().GetProperties()){
Debug.WriteLine(tp.Name);
Debug.WriteLine(tp.PropertyType);
Debug.WriteLine(tp.GetType().Name);
Debug.WriteLine(tp.GetValue(pList[0],null));
}
}

问题是您正在从新实例读取值(可以简单地写为
var xt=new T();

如果要获取项的属性,需要从实例中提取值

void TestOfT<T>(IEnumerable<T> list) where T: class, new()
{
    var properties = typeof(T).GetProperties();
    foreach (var item in list)
    foreach (var property in properties)
    {
        var name = property.Name;
        var value = property.GetValue(item, null);
        Debug.WriteLine("{0} is {1}", name, value);
    }
}
void TestOfT(IEnumerable列表),其中T:class,new()
{
var properties=typeof(T).GetProperties();
foreach(列表中的变量项)
foreach(属性中的var属性)
{
var name=property.name;
var value=property.GetValue(项,空);
WriteLine(“{0}是{1}”,名称,值);
}
}

问题是您正在从新实例读取值(可以简单地写为
var xt=new T();

如果要获取项的属性,需要从实例中提取值

void TestOfT<T>(IEnumerable<T> list) where T: class, new()
{
    var properties = typeof(T).GetProperties();
    foreach (var item in list)
    foreach (var property in properties)
    {
        var name = property.Name;
        var value = property.GetValue(item, null);
        Debug.WriteLine("{0} is {1}", name, value);
    }
}
void TestOfT(IEnumerable列表),其中T:class,new()
{
var properties=typeof(T).GetProperties();
foreach(列表中的变量项)
foreach(属性中的var属性)
{
var name=property.name;
var value=property.GetValue(项,空);
WriteLine(“{0}是{1}”,名称,值);
}
}

你的问题到底是什么?你说的是
--failed
但到底是什么失败了?是编译错误吗?运行时?什么?你根本不需要
xt
实例,因为你只是在它上面调用
GetType()
。事实上,你不需要在任何实例上调用
GetType()
。你可以说
typeof(t).GetProperties()
。你的问题是什么?你说的是
--failed
,但到底是什么失败了?是编译错误吗?运行时?什么?你根本不需要
xt
实例,因为你只是在它上面调用
GetType()
。事实上,你不需要调用
GetType()
在任何实例上。你可以说
typeof(T).GetProperties()
。这看起来像是问题中代码的副本。这有什么意义?我添加了GetValue()方法:)现在我知道我的错误是将tp传递给GetValue,而GetValue应该是记录。Debug.WriteLine(tp.GetValue(pList[0],null));而不是Debug.WriteLine(tp.GetValue(tp,null));是的,您应该提供要从中获取值的对象。这看起来像是问题中代码的副本。重点是什么?我添加了GetValue()方法:)现在我知道我的错误是将tp传递到GetValue,而GetValue应该是记录。Debug.WriteLine(tp.GetValue(pList[0],null));而不是Debug.WriteLine(tp.GetValue(tp,null));是的,你应该提供你想从中获取值的对象。