.net 如何循环一个类的所有属性?

.net 如何循环一个类的所有属性?,.net,vb.net,class,reflection,properties,.net,Vb.net,Class,Reflection,Properties,我有课 Public Class Foo Private _Name As String Public Property Name() As String Get Return _Name End Get Set(ByVal value As String) _Name = value End Set End Property Private _Age

我有课

Public Class Foo
    Private _Name As String
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property

    Private _Age As String
    Public Property Age() As String
        Get
            Return _Age
        End Get
        Set(ByVal value As String)
            _Age = value
        End Set
    End Property

    Private _ContactNumber As String
    Public Property ContactNumber() As String
        Get
            Return _ContactNumber
        End Get
        Set(ByVal value As String)
            _ContactNumber = value
        End Set
    End Property


End Class
我想循环上述类的属性。 eg

使用反射:

Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();

foreach (PropertyInfo property in properties)
{
    Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(obj, null));
}
对于Excel-由于列表中没有“System.Reflection”条目,必须添加哪些工具/参考项才能访问BindingFlags

编辑:您还可以将BindingFlags值指定给
type.GetProperties()

这将把返回的属性限制为公共实例属性(不包括静态属性、受保护属性等)

您不需要指定BindingFlags.GetProperty,您可以在调用
type.InvokeMember()
时使用它来获取属性的值。

C的VB版本#由Brannon给出:

Public Sub DisplayAll(ByVal Someobject作为Foo)
Dim_type As type=Someobject.GetType()
Dim properties()作为PropertyInfo=\u type.GetProperties()'第3行
对于每个_属性作为属性info在属性中
Console.WriteLine(“名称:”+\u property.Name+”,值:“+\u property.GetValue(Someobject,Nothing))
下一个
端接头
在中使用绑定标志而不是第3行

    Dim flags As BindingFlags = BindingFlags.Public Or BindingFlags.Instance
    Dim properties() As PropertyInfo = _type.GetProperties(flags)

请注意,如果您正在谈论的对象具有自定义属性模型(例如
DataRowView
等,用于
DataTable
),则需要使用
TypeDescriptor
;好消息是,这仍然适用于常规课程(甚至可以是):

这还提供了对
TypeConverter
等格式设置的轻松访问:

    string fmt = prop.Converter.ConvertToString(prop.GetValue(obj));
反射相当“沉重”

或许可以尝试以下解决方案:

C#

VB.Net

  If TypeOf item Is IEnumerable Then

    For Each o As Object In TryCast(item, IEnumerable)
               'Do Function
     Next
  Else
    For Each p As System.Reflection.PropertyInfo In obj.GetType().GetProperties()
         If p.CanRead Then
               Console.WriteLine("{0}: {1}", p.Name, p.GetValue(obj, Nothing))  'possible function
          End If
      Next
  End If
反射使方法调用的速度减慢+/-1000倍,如中所示


我使用上面的代码块重置了web用户控件对象中的所有字符串属性,该对象的名称以“Doc”开头。

下面是另一种方法,使用LINQ lambda:

C#:

VB.NET:

SomeObject.GetType.GetProperties.ToList.ForEach(Sub(x) Console.WriteLine($"{x.Name} = {x.GetValue(SomeObject, Nothing)}"))
我就是这样做的

foreach (var fi in typeof(CustomRoles).GetFields())
{
    var propertyName = fi.Name;
}

顺便说一句,这个GetProperties方法不应该有一些绑定标志吗?像
BindingFlags.Public | BindingFlags.GetProperty
或其他什么?@Svish,你说得对:)它可以使用一些BindingFlags,但它们是可选的。提示:如果您处理的是静态字段,那么只需在此处传递null:property.GetValue(null);那么obj.GetValue()就不存在了。在c#7或c#8中迭代属性和检查值的当前方式是什么?我觉得没什么用。谢谢,如果我转换成VB,我会花太长时间:)你可以随时使用自动转换器,网络上有很多:)是的,但没有手工编码那么好。一个值得注意的是telerik代码转换器telerik的转换方式:如果对象/类包含属性而不是字段,则使用GetProperties()而不是GetFields()。
if (item is IEnumerable) {
    foreach (object o in item as IEnumerable) {
            //do function
    }
} else {
    foreach (System.Reflection.PropertyInfo p in obj.GetType().GetProperties())      {
        if (p.CanRead) {
            Console.WriteLine("{0}: {1}", p.Name, p.GetValue(obj,  null)); //possible function
        }
    }
}
  If TypeOf item Is IEnumerable Then

    For Each o As Object In TryCast(item, IEnumerable)
               'Do Function
     Next
  Else
    For Each p As System.Reflection.PropertyInfo In obj.GetType().GetProperties()
         If p.CanRead Then
               Console.WriteLine("{0}: {1}", p.Name, p.GetValue(obj, Nothing))  'possible function
          End If
      Next
  End If
private void ResetAllProperties()
    {
        Type type = this.GetType();
        PropertyInfo[] properties = (from c in type.GetProperties()
                                     where c.Name.StartsWith("Doc")
                                     select c).ToArray();
        foreach (PropertyInfo item in properties)
        {
            if (item.PropertyType.FullName == "System.String")
                item.SetValue(this, "", null);
        }
    }
SomeObject.GetType().GetProperties().ToList().ForEach(x => Console.WriteLine($"{x.Name} = {x.GetValue(SomeObject, null)}"));
SomeObject.GetType.GetProperties.ToList.ForEach(Sub(x) Console.WriteLine($"{x.Name} = {x.GetValue(SomeObject, Nothing)}"))
foreach (var fi in typeof(CustomRoles).GetFields())
{
    var propertyName = fi.Name;
}