none
Is it possible to get the element type of Property where the type of property is Array but the field referred by the getter is Double() ? There are no elements! RRS feed

  • Question


  • Private _d as Double() Public Property A as Array Get Return _d End Get End Property

    Using reflection how can I get 'Double' returned ?

    So far I tried:

    P.PropertyType.Name but returns 'Array'

    P.PropertyType.GetElementType but returns 'Nothing' (as explained here: http://msdn.microsoft.com/en-us/library/vstudio/system.array(v=vs.100).aspx)

    Of course if I try to set the property with a string array then I will get Unable to cast object of type 'System.String[]' to type 'System.Double[]'

    Is it possible with reflection or Try - Catch is my only option here?

    Thanks in advance !

    Thursday, June 27, 2013 2:43 PM

Answers

  • You can get the type of a value, but if there is no value (Nothing / null) you can consequently not get any type for it.

    If the return type of the property in your class is System.Array, this is the type that the GetProperty PropertyInfo property will return. The System.Array class is a base class for all arrays and doesn't know anything about any derived double[] array.

    Why don't you declare your property as Double() / double[] ? Then you will be able to get the type of the elements using the following code even if the Array was not initialized:

    PropertyInfo pi = src.GetType().GetProperty("A");
    Type type = pi.PropertyType.GetElementType();
    

    Monday, July 1, 2013 8:47 AM

All replies

  • I Gergely Melis,

    I am moving this thread to Base Class Library forum for better support.

    Have a nice day!


    Min Zhu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 28, 2013 9:06 AM
    Moderator
  • Use the GetElementType() method to get the type of an Array object:

    Item src = new Item();
    Array arr = src.GetType().GetProperty("A").GetValue(src, null) as Array;
    Type t = arr.GetType().GetElementType(); //=double

    Friday, June 28, 2013 10:10 AM
  • Hi Magnus,

    thanks for your reply!

    Using below code (stripped-down version of my class) as target.

    Your suggestion produces the same result as with P.PropertyType.GetElementType.

    The problem is that arr will be 'Nothing' as _parameter1 is uninitialized.

    Once _property1 has elements your suggestion will work of course.

    However, is it possible to figure out the type of _parameter1 before it is initialized?

    Thanks ,

    Gergely

        Public Class cArrayTest
            Private _name As String
            Private _parameter1 As Double()
            Public Property Name As String
                Get
                    Return _name
                End Get
                Set(value As String)
                    _name = Name
                    For Each PI As PropertyInfo In Me.GetType.GetProperties
                        If PI.Name <> "Parameters" Then Parameters.Add(PI)
                    Next
                End Set
            End Property
            Public Property Parameters As New List(Of PropertyInfo)
            Public Property Parameter1 As Array
                Get
                    Return _parameter1
                End Get
                Set(value As Array)
                    _parameter1 = value
                End Set
            End Property
            Public Sub New(ByVal Name As String)
                Me.Name = Name
            End Sub
            Public Sub New()
            End Sub
        End Class


    Monday, July 1, 2013 7:21 AM
  • You can get the type of a value, but if there is no value (Nothing / null) you can consequently not get any type for it.

    If the return type of the property in your class is System.Array, this is the type that the GetProperty PropertyInfo property will return. The System.Array class is a base class for all arrays and doesn't know anything about any derived double[] array.

    Why don't you declare your property as Double() / double[] ? Then you will be able to get the type of the elements using the following code even if the Array was not initialized:

    PropertyInfo pi = src.GetType().GetProperty("A");
    Type type = pi.PropertyType.GetElementType();
    

    Monday, July 1, 2013 8:47 AM
  • I assume there are no other way to get the type but trial and error:

    Try
       'assume a type of array and set property
    Catch ex As ArgumentException
        TypeName = ex.Message.Split("'")(3)
        T = Type.GetType(TypeName)
        'set the property with array of T
    End Try

    This isn't good coding so I will change the property types to match the relevant field types.

    Thanks for your help!

    Gergely

    Monday, July 1, 2013 10:04 AM