Reflect the value of the property for which the getter has an optional value

I am extracting several properties of the control. This is how I used to retrieve properties (with pinfo of type PropertyInfo):

value = pinfo.GetValue(obj, nothing)

This worked fine, but now I come across a property that has an optional value, and I get an error message telling me that the number of parameters is incorrect. So I changed my code to this:

Dim index As Object() = {Nothing}
value = pinfo.GetValue(obj, index)

At this point, I did not receive an error message, but this code does not get a good value. It only works if I replace Nothing with the default value provided by the property accessory ...

But I don’t know what the default is! And this code is inside a function that retrieves properties that do not have additional values, so I cannot change the code, especially for one case.

Any idea? I am working on .NET 2.0


EDIT: additional comments on the case leading to the problem

Here is an example of the property leading to the problem:

ReadOnly Property Foo(Optional ByVal Number As Integer = -1) As String
    Get
        If Number = -1 Then
            Return "Your number is the default number: " & Number
        Else
            Return "Your number is " & Number
        End If
    End Get
End Property

With this property, none of the above codes returns a good string.

It would be best to try the first code for general purposes, catch the corresponding exception, and then dynamically get the default value of the parameter (in this case, a number) and enter it so that I can call getValuewith this default value.

, ?

+5
2

:

ReadOnly Property Foo(Optional name As String = Nothing) As String
    Get
        If name Is Nothing Then
            Return "Hello World"
        Else
            Return "Hello " & name
        End If
    End Get
End Property


Dim pinfo As Reflection.PropertyInfo = Me.GetType().GetProperty("Foo")
Dim value As Object = pinfo.GetValue(Me, New Object() {"Tim"})  ' Hello Tim '
value = pinfo.GetValue(Me, New Object(){Nothing})               ' Hello World '

:. , , , Property. , , ( Int32.MinValue 0):

ReadOnly Property Foo(Optional age As Int32 = Int32.MinValue) As String
    Get
        If age = Int32.MinValue Then
            Return "I don't know how old i am"
        Else
            Return String.Format("I am {0} years old", age)
        End If
    End Get
End Property

Dim pinfo As Reflection.PropertyInfo = Me.GetType.GetProperty("Foo")
Dim value As Object = pinfo.GetValue(Me, New Object() {38})  ' I am 38 years old '
value = pinfo.GetValue(Me, New Object() {Nothing})           ' I am 0 years old '
value = pinfo.GetValue(Me, New Object() {Int32.MinValue})    ' I don't know how old i am '

Edit2. @Rup , GetIndexParameters - . .

Dim pinfo As Reflection.PropertyInfo = Me.GetType.GetProperty("Foo")
Dim parameters() As Reflection.ParameterInfo = pinfo.GetIndexParameters()
Dim params(parameters.Length - 1) As Object
For i As Int32 = 0 To parameters.Length - 1
    Dim paramInfo As Reflection.ParameterInfo = parameters(i)
    If paramInfo.IsOptional Then
        params(i) = paramInfo.DefaultValue
    Else
        If paramInfo.ParameterType.IsValueType Then
            params(i) = Activator.CreateInstance(paramInfo.ParameterType)
        Else
            params(i) = Nothing
        End If
    End If
Next
Dim value As Object = pinfo.GetValue(Me, params)
+3

. , null, . #.

class Program
{
    static void Main(string[] args)
    {
        Test testObj = null;
        testObj = testObj ?? Activator.CreateInstance<Test>();
        var ty = testObj.GetType().GetProperty("MyProperty").GetValue(testObj, null);

    }
}
public class Test
{
    public Test2 MyProperty { get; set; }
}
public class Test2
{
    public int Prty { get; set; }
}
+1

All Articles