愚かな質問かもしれませんが、リストのパラメータからすべてのプロパティを読み取ることはできますが、フィールドの値は<T>
ではありません。Listフォームで汎用メソッドを読み取る方法
これは構造
public class TestRecord {
public string StringTest { get; set; }
public int IntegerTest { get; set; }
public DateTime DateTimeTest { get; set; }
}
ジェネリックメソッドのテストコードをあなたの助けを
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<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);
}
}
です。
? –
あなたは '--failed'言うけど?それはコンパイルエラーまさに** **失敗しているのですか?ランタイム?何? –
あなたはXT'ことを必要としません。 'インスタンスでは、' GetType() 'を呼び出しているだけなので。実際、どのインスタンスでも 'GetType()'を呼び出す必要はありません。あなたは 'typeof(T).GetProperties()'と言うことができます。 – phoog