0

我想编写一个通用方法,它接受任何类型的类对象并为该对象的所有属性返回一个 keyValuepair。

public Dictionary<string,string> GetProperties(T classObj)
{
}

对此的任何帮助将非常有用。

4

1 回答 1

2

使用反射:

public Dictionary<string, object> GetProperties<T>(T classObj)
{
    return typeof(T).GetProperties()
            .ToDictionary(p => p.Name, p => p.GetValue(classObj, null));
}
于 2013-05-20T12:16:17.010 回答