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'
Remarks
If this
PropertyInfo
object is a value type and
value
is
null
, then the property will be set to the default value for that type.
To determine whether a property is indexed, use the
GetIndexParameters
method. If the resulting array has 0 (zero) elements, the property is not indexed.
This is a convenience method that calls the runtime implementation of the abstract
SetValue(Object, Object, BindingFlags, Binder, Object[], CultureInfo)
method, specifying
BindingFlags.Default
for the
BindingFlags
parameter,
null
for
Binder
, and
null
for
CultureInfo
.
To use the
SetValue
method, first get a
Type
object that represents the class. From the
Type
, get the
PropertyInfo
. From the
PropertyInfo
, use the
SetValue
method.
Starting with .NET Framework 2.0, this method can be used to access non-public members if the caller has been granted
ReflectionPermission
with the
ReflectionPermissionFlag.RestrictedMemberAccess
flag and if the grant set of the non-public members is restricted to the caller's grant set, or a subset thereof. (See
Security Considerations for Reflection
.) To use this functionality, your application should target .NET Framework 3.5 or later.
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)
Parameters
A bitwise combination of the following enumeration members that specify the invocation attribute:
InvokeMethod
,
CreateInstance
,
Static
,
GetField
,
SetField
,
GetProperty
, or
SetProperty
. You must specify a suitable invocation attribute. For example, to invoke a static member, set the
Static
flag.
The culture for which the resource is to be localized. If the resource is not localized for this culture, the
Parent
property will be called successively in search of a match. If this value is
null
, the culture-specific information is obtained from the
CurrentUICulture
property.
Implements
SetValue(Object, Object, BindingFlags, Binder, Object[], CultureInfo)
The
index
array does not contain the type of arguments needed.
The property's
set
accessor is not found.
value
cannot be converted to the type of
PropertyType
.
Remarks
If this
PropertyInfo
object is a value type and
value
is
null
, then the property will be set to the default value for that type.
To determine whether a property is indexed, use the
GetIndexParameters
method. If the resulting array has 0 (zero) elements, the property is not indexed.
Access restrictions are ignored for fully trusted code. That is, private constructors, methods, fields, and properties can be accessed and invoked via Reflection whenever the code is fully trusted.
To use the
SetValue
method, first get the class
Type
. From the
Type
, get the
PropertyInfo
. From the
PropertyInfo
, use the
SetValue
method.
Starting with .NET Framework 2.0, this method can be used to access non-public members if the caller has been granted
ReflectionPermission
with the
ReflectionPermissionFlag.RestrictedMemberAccess
flag and if the grant set of the non-public members is restricted to the caller's grant set, or a subset thereof. (See
Security Considerations for Reflection
.) To use this functionality, your application should target .NET Framework 3.5 or later.