Imports System.Reflection
Imports System.Collections.Generic
Class Example
Private Shared _sharedProperty As Integer = 41
Public Shared Property SharedProperty As Integer
Return _sharedProperty
End Get
_sharedProperty = Value
End Set
End Property
Private _instanceProperty As Integer = 42
Public Property InstanceProperty As Integer
Return _instanceProperty
End Get
_instanceProperty = Value
End Set
End Property
Private _indexedInstanceProperty As New Dictionary(Of Integer, String)
Default Public Property IndexedInstanceProperty(ByVal key As Integer) As String
Dim returnValue As String = Nothing
If _indexedInstanceProperty.TryGetValue(key, returnValue) Then
Return returnValue
Return Nothing
End If
End Get
If Value Is Nothing Then
Throw New ArgumentNullException( _
"IndexedInstanceProperty value can be an empty string, but it cannot be Nothing.")
If _indexedInstanceProperty.ContainsKey(key) Then
_indexedInstanceProperty(key) = Value
_indexedInstanceProperty.Add(key, Value)
End If
End If
End Set
End Property
Shared Sub Main()
Console.WriteLine("Initial value of class-level property: {0}", _
Example.SharedProperty)
Dim piShared As PropertyInfo = _
GetType(Example).GetProperty("SharedProperty")
piShared.SetValue( _
Nothing, _
76, _
Nothing)
Console.WriteLine("Final value of class-level property: {0}", _
Example.SharedProperty)
Dim exam As New Example
Console.WriteLine(vbCrLf & _
"Initial value of instance property: {0}", _
exam.InstanceProperty)
Dim piInstance As PropertyInfo = _
GetType(Example).GetProperty("InstanceProperty")
piInstance.SetValue( _
exam, _
37, _
Nothing)
Console.WriteLine("Final value of instance property: {0}", _
exam.InstanceProperty)
exam(17) = "String number 17"
exam(46) = "String number 46"
' In Visual Basic, a default indexed property can also be referred
' to by name.
exam.IndexedInstanceProperty(9) = "String number 9"
Console.WriteLine(vbCrLf & _
"Initial value of indexed instance property(17): '{0}'", _
exam(17))
Dim piIndexedInstance As PropertyInfo = _
GetType(Example).GetProperty("IndexedInstanceProperty")
piIndexedInstance.SetValue( _
exam, _
"New value for string number 17", _
New Object() { CType(17, Integer) })
Console.WriteLine("Final value of indexed instance property(17): '{0}'", _
exam(17))
End Sub
End Class
' This example produces the following output:
'Initial value of class-level property: 41
'Final value of class-level property: 76
'Initial value of instance property: 42
'Final value of instance property: 37
'Initial value of indexed instance property(17): 'String number 17'
'Final value of indexed instance property(17): 'New value for string number 17'
如果此
PropertyInfo
对象是值类型且
value
为
null
,则 属性将设置为该类型的默认值。
若要确定是否为属性编制索引,请使用
GetIndexParameters
方法。 如果生成的数组具有 0 个 (零) 元素,则不会为 属性编制索引。
这是一种方便的方法,它调用抽象
SetValue(Object, Object, BindingFlags, Binder, Object[], CultureInfo)
方法的运行时实现,为
BindingFlags
参数指定
BindingFlags.Default
、
null
和
null
Binder
CultureInfo
。
若要使用
SetValue
方法,请首先获取表示
Type
类的 对象。 从 中
Type
获取
PropertyInfo
。 在 中
PropertyInfo
,使用
SetValue
方法。
从 .NET Framework 2.0 开始,如果调用方已使用 标志授予
ReflectionPermission
ReflectionPermissionFlag.RestrictedMemberAccess
调用方,并且非公共成员的授予集仅限于调用方授权集或其子集,则此方法可用于访问非公共成员。 (请参阅 Reflection.)
的安全注意事项
若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。
public:
abstract void SetValue(System::Object ^ obj, System::Object ^ value, System::Reflection::BindingFlags invokeAttr, System::Reflection::Binder ^ binder, cli::array <System::Object ^> ^ index, System::Globalization::CultureInfo ^ culture);
public abstract void SetValue (object? obj, object? value, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object?[]? index, System.Globalization.CultureInfo? culture);
public abstract void SetValue (object obj, object value, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] index, System.Globalization.CultureInfo culture);
abstract member SetValue : obj * obj * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo -> unit
Public MustOverride Sub SetValue (obj As Object, value As Object, invokeAttr As BindingFlags, binder As Binder, index As Object(), culture As CultureInfo)
要为其本地化资源的区域性。 请注意,如果没有为此区域性本地化该资源,则在搜索匹配项的过程中将继续调用
Parent
属性。 如果该值为
null
,则从
CurrentUICulture
属性获取区域性的特定信息。
SetValue(Object, Object, BindingFlags, Binder, Object[], CultureInfo)
如果此
PropertyInfo
对象是值类型且
value
为
null
,则 属性将设置为该类型的默认值。
若要确定是否为属性编制索引,请使用
GetIndexParameters
方法。 如果生成的数组具有 0 个 (零) 元素,则不会为 属性编制索引。
对于完全受信任的代码,将忽略访问限制。 也就是说,只要代码完全受信任,就可以通过反射访问和调用专用构造函数、方法、字段和属性。
若要使用
SetValue
方法,请首先获取 类
Type
。 从 中
Type
获取
PropertyInfo
。 在 中
PropertyInfo
,使用
SetValue
方法。
从 .NET Framework 2.0 开始,如果调用方已使用 标志授予
ReflectionPermission
ReflectionPermissionFlag.RestrictedMemberAccess
调用方,并且非公共成员的授予集仅限于调用方授权集或其子集,则此方法可用于访问非公共成员。 (请参阅 Reflection.)
的安全注意事项
若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。