📜  c# 获取所有类属性 - C# 代码示例

📅  最后修改于: 2022-03-11 14:48:40.451000             🧑  作者: Mango

代码示例1
//through reflection
using System.Reflection;

//Get a List of the properties from a type
public static PropertyInfo[] ListOfPropertiesFromInstance(Type AType)
{
      if (InstanceOfAType == null) return null;
    return AType.GetProperties(BindingFlags.Public);
}

//Get a List of the properties from a instance of a class
public static PropertyInfo[] ListOfPropertiesFromInstance(object InstanceOfAType)
{
      if (InstanceOfAType == null) return null;
      Type TheType = InstanceOfAType.GetType();
    return TheType.GetProperties(BindingFlags.Public);
}

//purrfect for usage example and Get a Map of the properties from a instance of a class
public static Dictionary DictionaryOfPropertiesFromInstance(object InstanceOfAType)
{
    if (InstanceOfAType == null) return null;
    Type TheType = InstanceOfAType.GetType();
    PropertyInfo[] Properties = TheType.GetProperties(BindingFlags.Public);
    Dictionary PropertiesMap = new Dictionary();
    foreach (PropertyInfo Prop in Properties)
    {
        PropertiesMap.Add(Prop.Name, Prop);
    }
    return PropertiesMap;
}