Sac*_*van 165 .net vb.net reflection properties class
我上课了.
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
Run Code Online (Sandbox Code Playgroud)
我想循环上面的类的属性.例如;
Public Sub DisplayAll(ByVal Someobject As Foo)
For Each _Property As something In Someobject.Properties
Console.WriteLine(_Property.Name & "=" & _Property.value)
Next
End Sub
Run Code Online (Sandbox Code Playgroud)
Bra*_*non 291
使用反射:
Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(obj, null));
}
Run Code Online (Sandbox Code Playgroud)
编辑:您还可以将BindingFlags值指定为type.GetProperties()
:
BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
PropertyInfo[] properties = type.GetProperties(flags);
Run Code Online (Sandbox Code Playgroud)
这会将返回的属性限制为公共实例属性(不包括静态属性,受保护属性等).
您无需指定BindingFlags.GetProperty
,在调用type.InvokeMember()
以获取属性的值时使用它.
Mar*_*ell 41
请注意,如果您正在谈论的对象具有自定义属性模型(例如DataRowView
等DataTable
),那么您需要使用TypeDescriptor
; 好消息是这对于常规课程仍然可以正常工作(甚至可以比反思快得多):
foreach(PropertyDescriptor prop in TypeDescriptor.GetProperties(obj)) {
Console.WriteLine("{0} = {1}", prop.Name, prop.GetValue(obj));
}
Run Code Online (Sandbox Code Playgroud)
这也可以轻松访问TypeConverter
格式化等内容:
string fmt = prop.Converter.ConvertToString(prop.GetValue(obj));
Run Code Online (Sandbox Code Playgroud)
Sac*_*van 32
Brannon给出的VB版C#:
Public Sub DisplayAll(ByVal Someobject As Foo)
Dim _type As Type = Someobject.GetType()
Dim properties() As PropertyInfo = _type.GetProperties() 'line 3
For Each _property As PropertyInfo In properties
Console.WriteLine("Name: " + _property.Name + ", Value: " + _property.GetValue(Someobject, Nothing))
Next
End Sub
Run Code Online (Sandbox Code Playgroud)
使用绑定标志而不是第3行
Dim flags As BindingFlags = BindingFlags.Public Or BindingFlags.Instance
Dim properties() As PropertyInfo = _type.GetProperties(flags)
Run Code Online (Sandbox Code Playgroud)
反思非常"沉重"
也许尝试这个解决方案:// C#
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
"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
Run Code Online (Sandbox Code Playgroud)
反射速度减慢了方法调用的速度+/- 1000 x,如"日常事物的表现"中所示
归档时间: |
|
查看次数: |
145942 次 |
最近记录: |