C# 如何获取类的属性列表?

C# 如何获取类的属性列表?,c#,.net,reflection,properties,C#,.net,Reflection,Properties,如何获取类的所有属性的列表?反射;例如: obj.GetType().GetProperties(); 对于类型: typeof(Foo).GetProperties(); 例如: class Foo { public int A {get;set;} public string B {get;set;} } ... Foo foo = new Foo {A = 1, B = "abc"}; foreach(var prop in foo.GetType().GetPrope

如何获取类的所有属性的列表?

反射;例如:

obj.GetType().GetProperties();
对于类型:

typeof(Foo).GetProperties();
例如:

class Foo {
    public int A {get;set;}
    public string B {get;set;}
}
...
Foo foo = new Foo {A = 1, B = "abc"};
foreach(var prop in foo.GetType().GetProperties()) {
    Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(foo, null));
}

下面的反馈

  • 要获取静态属性的值,请将
    null
    作为第一个参数传递给
    GetValue
  • 要查看非公共属性,请使用(例如)
    GetProperties(BindingFlags.public | BindingFlags.NonPublic | BindingFlags.Instance)
    (返回所有公共/私有实例属性)

    • 您可以使用反射

      Type typeOfMyObject = myObject.GetType();
      PropertyInfo[] properties =typeOfMyObject.GetProperties();
      

      您可以使用反射来执行此操作: (从我的库-获取名称和值)

      这也适用于匿名类型
      要获取名称,请执行以下操作:

      public static string[] PropertiesFromType(object atype)
      {
          if (atype == null) return new string[] {};
          Type t = atype.GetType();
          PropertyInfo[] props = t.GetProperties();
          List<string> propNames = new List<string>();
          foreach (PropertyInfo prp in props)
          {
              propNames.Add(prp.Name);
          }
          return propNames.ToArray();
      }
      

      但我想这会慢一点。

      您可以使用
      System.Reflection
      命名空间和
      Type.GetProperties()
      方法:

      PropertyInfo[] propertyInfos;
      propertyInfos = typeof(MyClass).GetProperties(BindingFlags.Public|BindingFlags.Static);
      

      我也面临着这样的要求

      从这次讨论中我得到了另一个想法

      Obj.GetType().GetProperties()[0].Name
      
      这也会显示属性名称

      Obj.GetType().GetProperties().Count();
      
      这显示了属性的数量

      谢谢大家。这是一个很好的讨论。

      公共列表GetPropertiesNameOfClass(对象POObject)
      
      public List<string> GetPropertiesNameOfClass(object pObject)
      {
          List<string> propertyList = new List<string>();
          if (pObject != null)
          {
              foreach (var prop in pObject.GetType().GetProperties())
              {
                  propertyList.Add(prop.Name);
              }
          }
          return propertyList;
      }
      
      { List propertyList=新列表(); if(pObject!=null) { foreach(pObject.GetType().GetProperties()中的var prop) { propertyList.Add(prop.Name); } } 返回属性列表; }
      此函数用于获取类属性列表。

      这是我的解决方案

      public class MyObject
      {
          public string value1 { get; set; }
          public string value2 { get; set; }
      
          public PropertyInfo[] GetProperties()
          {
              try
              {
                  return this.GetType().GetProperties();
              }
              catch (Exception ex)
              {
      
                  throw ex;
              }
          }
      
          public PropertyInfo GetByParameterName(string ParameterName)
          {
              try
              {
                  return this.GetType().GetProperties().FirstOrDefault(x => x.Name == ParameterName);
              }
              catch (Exception ex)
              {
      
                  throw ex;
              }
          }
      
          public static MyObject SetValue(MyObject obj, string parameterName,object parameterValue)
          {
              try
              {
                  obj.GetType().GetProperties().FirstOrDefault(x => x.Name == parameterName).SetValue(obj, parameterValue);
                  return obj;
              }
              catch (Exception ex)
              {
                  throw ex;
              }
          }
      }
      

      这里是改进的@lucasjones答案。在他的回答之后,我在评论部分提到了改进。我希望有人会觉得这很有用

      public static string[] GetTypePropertyNames(object classObject,  BindingFlags bindingFlags)
      {
          if (classObject == null)
          {
              throw new ArgumentNullException(nameof(classObject));
          }
      
              var type = classObject.GetType();
              var propertyInfos = type.GetProperties(bindingFlags);
      
              return propertyInfos.Select(propertyInfo => propertyInfo.Name).ToArray();
       }
      

      根据@MarcGravel的回答,这里有一个在Unity C#中工作的版本

      试试这个:

      var model = new MyObject();
      foreach (var property in model.GetType().GetProperties())
      {
          var descricao = property;
          var type = property.PropertyType.Name;
      }
      

      下面的代码将为您提供类属性/属性/表列的列表

      var Properties = typeof(className).GetProperties().Select(x=>x.Name).Tolist();
      

      ... 但是根据链接的MSDN文章,atype.GetProperty(prp.Name)将返回prp?关于公共属性位:“注意,您必须指定实例或静态以及公共或非公共,否则不会返回任何成员。”。因此示例代码应该是:
      t.GetProperties(BindingFlags.Instance | BindingFlags.Public)
      t.GetProperties(BindingFlags.Static | BindingFlags.Public)
      没有寻找代码,我正在寻找对反射和哇的解释,非常感谢!将此设置为泛型,您还可以说您的程序具有超能力;)为完整起见,还有由TypeDescriptor.GetProperties(…)公开的ComponentModel,它允许动态运行时属性(反射在编译时固定).建议:扩展答案以涵盖受保护/私有/静态/继承的属性。您显示的foreach语句甚至可以在您想要获取其属性的类中工作:)我不清楚附加注释的说明方式,但使用所有3个标志也可以获取
      内部属性。也许我是唯一一个挂断了
      私有
      /
      非公共
      语法的人?@Tadej你的目标是什么框架?如果您使用的是.NET core,则需要确保有
      using System.Reflection
      指令和引用的包-这通过扩展方法提供缺少的API表面。您可能希望将其更改为使用
      yield return
      。这没什么大不了的,但这是一个更好的方法。我喜欢这个方法,因为它(几乎)是唯一一个不包含反射这个词的答案。但它仍然使用反射。我想这比pObject.GetType().GetProperties()好得多。请选择(p=>p.Name)。请不要这样做。这个S.O.应该尽最大努力回答关于做事方式的问题。如果您的库要求甚至允许使用强类型对象成员名称的字符串表示来更改强类型对象,那么您的代码库就存在一个巨大的问题。更不用说,在某些情况下,你显然无法控制系统的其他部分、公司政治或其他事情,这是不必要的。但这看起来像是一个架构上的决定,而且是一个可怕的决定。这是一连串可怕的编程选择中的一个决定。这以最直接的方式回答了OP提出的问题。6行,完成。
      public class MyObject
      {
          public string value1 { get; set; }
          public string value2 { get; set; }
      
          public PropertyInfo[] GetProperties()
          {
              try
              {
                  return this.GetType().GetProperties();
              }
              catch (Exception ex)
              {
      
                  throw ex;
              }
          }
      
          public PropertyInfo GetByParameterName(string ParameterName)
          {
              try
              {
                  return this.GetType().GetProperties().FirstOrDefault(x => x.Name == ParameterName);
              }
              catch (Exception ex)
              {
      
                  throw ex;
              }
          }
      
          public static MyObject SetValue(MyObject obj, string parameterName,object parameterValue)
          {
              try
              {
                  obj.GetType().GetProperties().FirstOrDefault(x => x.Name == parameterName).SetValue(obj, parameterValue);
                  return obj;
              }
              catch (Exception ex)
              {
                  throw ex;
              }
          }
      }
      
      public static string[] GetTypePropertyNames(object classObject,  BindingFlags bindingFlags)
      {
          if (classObject == null)
          {
              throw new ArgumentNullException(nameof(classObject));
          }
      
              var type = classObject.GetType();
              var propertyInfos = type.GetProperties(bindingFlags);
      
              return propertyInfos.Select(propertyInfo => propertyInfo.Name).ToArray();
       }
      
      ObjectsClass foo = this;
      foreach(var prop in foo.GetType().GetProperties()) {
          Debug.Log("{0}={1}, " + prop.Name + ", " + prop.GetValue(foo, null));
      }
      
      var model = new MyObject();
      foreach (var property in model.GetType().GetProperties())
      {
          var descricao = property;
          var type = property.PropertyType.Name;
      }
      
      var Properties = typeof(className).GetProperties().Select(x=>x.Name).Tolist();