Passing the type of the property to generic function









up vote
0
down vote

favorite












I am reading a text file, translate the data where each line either goes in header object or items object. I am having an issue with nullables



'p' in below code is coming from





Dim properties As PropertyInfo() = GetType(UploadMain).GetProperties()


The code below throws the error:




Invalid cast from 'System.DateTime' to 'System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'




p.SetValue(header, Convert.ChangeType(dataObject, p.PropertyType))


the corresponding property in the class



private _orderDate As Date?
Public Property OrderDate As Date?
Get
Return _orderDate
End Get
Set(value As Date?)
_orderDate = value
End Set
End Property


After looking around, found the following function, which i translated from C#



Public Class ChangeTypeUtlity

Public Shared Function ChangeType(Of T)(ByVal value As Object) As T
Dim conversionType As Type = GetType(T)

If conversionType.IsGenericType AndAlso conversionType.GetGenericTypeDefinition().Equals(GetType(Nullable)) Then

If value Is Nothing Then
Return Nothing
Else
Dim nullableConverter As NullableConverter = New NullableConverter(conversionType)
conversionType = nullableConverter.UnderlyingType
End If
End If

Return CType(Convert.ChangeType(value, conversionType), T)
End Function

End Class


I have tried it with both type1 and type2 but getting error that it is not defined.



Dim type1 as Type = p.[GetType]()
Dim type2 As Type = p.PropertyType
p.SetValue(header, ChangeTypeUtlity.ChangeType(Of type2)(dataObject))


How can i pass my property type to the above function?



You can give the solution in C#. It doesn't need to be VB.Net










share|improve this question



















  • 2




    Your problem is with ChangeType, do you even need it? If you do, detect if the property is Nullable, if it is you can then use Convert.ChangeType(dataObject, Nullable.GetUnderlyingType(p.PropertyType))
    – the_lotus
    Nov 9 at 18:23











  • I have been asking the same question myself as well and looking into it. From learning point of view, still would like to know how to pass the property type.
    – learning...
    Nov 9 at 19:18











  • @the_lotus please move your comment as an answer so that i can accept it. Convert.ChangeType(dataObject, Nullable.GetUnderlyingType(p.PropertyType)) helped solve the issue. I need to use nullable properties plus some other data that is weird as well like ints being passed as decimal. This is a vendor file, i have no control over it.
    – learning...
    Nov 9 at 19:51










  • ChangeType is extraneous even for C#. The line from the_lotus is basically identical to the code I use for the same purpose in a C# module.
    – Craig
    Nov 9 at 20:01










  • @learning... I'm glad it helped. I don't know how to properly write an answer for this but I copy/pasted my comment.
    – the_lotus
    Nov 13 at 12:46














up vote
0
down vote

favorite












I am reading a text file, translate the data where each line either goes in header object or items object. I am having an issue with nullables



'p' in below code is coming from





Dim properties As PropertyInfo() = GetType(UploadMain).GetProperties()


The code below throws the error:




Invalid cast from 'System.DateTime' to 'System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'




p.SetValue(header, Convert.ChangeType(dataObject, p.PropertyType))


the corresponding property in the class



private _orderDate As Date?
Public Property OrderDate As Date?
Get
Return _orderDate
End Get
Set(value As Date?)
_orderDate = value
End Set
End Property


After looking around, found the following function, which i translated from C#



Public Class ChangeTypeUtlity

Public Shared Function ChangeType(Of T)(ByVal value As Object) As T
Dim conversionType As Type = GetType(T)

If conversionType.IsGenericType AndAlso conversionType.GetGenericTypeDefinition().Equals(GetType(Nullable)) Then

If value Is Nothing Then
Return Nothing
Else
Dim nullableConverter As NullableConverter = New NullableConverter(conversionType)
conversionType = nullableConverter.UnderlyingType
End If
End If

Return CType(Convert.ChangeType(value, conversionType), T)
End Function

End Class


I have tried it with both type1 and type2 but getting error that it is not defined.



Dim type1 as Type = p.[GetType]()
Dim type2 As Type = p.PropertyType
p.SetValue(header, ChangeTypeUtlity.ChangeType(Of type2)(dataObject))


How can i pass my property type to the above function?



You can give the solution in C#. It doesn't need to be VB.Net










share|improve this question



















  • 2




    Your problem is with ChangeType, do you even need it? If you do, detect if the property is Nullable, if it is you can then use Convert.ChangeType(dataObject, Nullable.GetUnderlyingType(p.PropertyType))
    – the_lotus
    Nov 9 at 18:23











  • I have been asking the same question myself as well and looking into it. From learning point of view, still would like to know how to pass the property type.
    – learning...
    Nov 9 at 19:18











  • @the_lotus please move your comment as an answer so that i can accept it. Convert.ChangeType(dataObject, Nullable.GetUnderlyingType(p.PropertyType)) helped solve the issue. I need to use nullable properties plus some other data that is weird as well like ints being passed as decimal. This is a vendor file, i have no control over it.
    – learning...
    Nov 9 at 19:51










  • ChangeType is extraneous even for C#. The line from the_lotus is basically identical to the code I use for the same purpose in a C# module.
    – Craig
    Nov 9 at 20:01










  • @learning... I'm glad it helped. I don't know how to properly write an answer for this but I copy/pasted my comment.
    – the_lotus
    Nov 13 at 12:46












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am reading a text file, translate the data where each line either goes in header object or items object. I am having an issue with nullables



'p' in below code is coming from





Dim properties As PropertyInfo() = GetType(UploadMain).GetProperties()


The code below throws the error:




Invalid cast from 'System.DateTime' to 'System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'




p.SetValue(header, Convert.ChangeType(dataObject, p.PropertyType))


the corresponding property in the class



private _orderDate As Date?
Public Property OrderDate As Date?
Get
Return _orderDate
End Get
Set(value As Date?)
_orderDate = value
End Set
End Property


After looking around, found the following function, which i translated from C#



Public Class ChangeTypeUtlity

Public Shared Function ChangeType(Of T)(ByVal value As Object) As T
Dim conversionType As Type = GetType(T)

If conversionType.IsGenericType AndAlso conversionType.GetGenericTypeDefinition().Equals(GetType(Nullable)) Then

If value Is Nothing Then
Return Nothing
Else
Dim nullableConverter As NullableConverter = New NullableConverter(conversionType)
conversionType = nullableConverter.UnderlyingType
End If
End If

Return CType(Convert.ChangeType(value, conversionType), T)
End Function

End Class


I have tried it with both type1 and type2 but getting error that it is not defined.



Dim type1 as Type = p.[GetType]()
Dim type2 As Type = p.PropertyType
p.SetValue(header, ChangeTypeUtlity.ChangeType(Of type2)(dataObject))


How can i pass my property type to the above function?



You can give the solution in C#. It doesn't need to be VB.Net










share|improve this question















I am reading a text file, translate the data where each line either goes in header object or items object. I am having an issue with nullables



'p' in below code is coming from





Dim properties As PropertyInfo() = GetType(UploadMain).GetProperties()


The code below throws the error:




Invalid cast from 'System.DateTime' to 'System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'




p.SetValue(header, Convert.ChangeType(dataObject, p.PropertyType))


the corresponding property in the class



private _orderDate As Date?
Public Property OrderDate As Date?
Get
Return _orderDate
End Get
Set(value As Date?)
_orderDate = value
End Set
End Property


After looking around, found the following function, which i translated from C#



Public Class ChangeTypeUtlity

Public Shared Function ChangeType(Of T)(ByVal value As Object) As T
Dim conversionType As Type = GetType(T)

If conversionType.IsGenericType AndAlso conversionType.GetGenericTypeDefinition().Equals(GetType(Nullable)) Then

If value Is Nothing Then
Return Nothing
Else
Dim nullableConverter As NullableConverter = New NullableConverter(conversionType)
conversionType = nullableConverter.UnderlyingType
End If
End If

Return CType(Convert.ChangeType(value, conversionType), T)
End Function

End Class


I have tried it with both type1 and type2 but getting error that it is not defined.



Dim type1 as Type = p.[GetType]()
Dim type2 As Type = p.PropertyType
p.SetValue(header, ChangeTypeUtlity.ChangeType(Of type2)(dataObject))


How can i pass my property type to the above function?



You can give the solution in C#. It doesn't need to be VB.Net







c# vb.net system.reflection






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 20:12









Visual Vincent

14.9k51947




14.9k51947










asked Nov 9 at 17:37









learning...

1,24653762




1,24653762







  • 2




    Your problem is with ChangeType, do you even need it? If you do, detect if the property is Nullable, if it is you can then use Convert.ChangeType(dataObject, Nullable.GetUnderlyingType(p.PropertyType))
    – the_lotus
    Nov 9 at 18:23











  • I have been asking the same question myself as well and looking into it. From learning point of view, still would like to know how to pass the property type.
    – learning...
    Nov 9 at 19:18











  • @the_lotus please move your comment as an answer so that i can accept it. Convert.ChangeType(dataObject, Nullable.GetUnderlyingType(p.PropertyType)) helped solve the issue. I need to use nullable properties plus some other data that is weird as well like ints being passed as decimal. This is a vendor file, i have no control over it.
    – learning...
    Nov 9 at 19:51










  • ChangeType is extraneous even for C#. The line from the_lotus is basically identical to the code I use for the same purpose in a C# module.
    – Craig
    Nov 9 at 20:01










  • @learning... I'm glad it helped. I don't know how to properly write an answer for this but I copy/pasted my comment.
    – the_lotus
    Nov 13 at 12:46












  • 2




    Your problem is with ChangeType, do you even need it? If you do, detect if the property is Nullable, if it is you can then use Convert.ChangeType(dataObject, Nullable.GetUnderlyingType(p.PropertyType))
    – the_lotus
    Nov 9 at 18:23











  • I have been asking the same question myself as well and looking into it. From learning point of view, still would like to know how to pass the property type.
    – learning...
    Nov 9 at 19:18











  • @the_lotus please move your comment as an answer so that i can accept it. Convert.ChangeType(dataObject, Nullable.GetUnderlyingType(p.PropertyType)) helped solve the issue. I need to use nullable properties plus some other data that is weird as well like ints being passed as decimal. This is a vendor file, i have no control over it.
    – learning...
    Nov 9 at 19:51










  • ChangeType is extraneous even for C#. The line from the_lotus is basically identical to the code I use for the same purpose in a C# module.
    – Craig
    Nov 9 at 20:01










  • @learning... I'm glad it helped. I don't know how to properly write an answer for this but I copy/pasted my comment.
    – the_lotus
    Nov 13 at 12:46







2




2




Your problem is with ChangeType, do you even need it? If you do, detect if the property is Nullable, if it is you can then use Convert.ChangeType(dataObject, Nullable.GetUnderlyingType(p.PropertyType))
– the_lotus
Nov 9 at 18:23





Your problem is with ChangeType, do you even need it? If you do, detect if the property is Nullable, if it is you can then use Convert.ChangeType(dataObject, Nullable.GetUnderlyingType(p.PropertyType))
– the_lotus
Nov 9 at 18:23













I have been asking the same question myself as well and looking into it. From learning point of view, still would like to know how to pass the property type.
– learning...
Nov 9 at 19:18





I have been asking the same question myself as well and looking into it. From learning point of view, still would like to know how to pass the property type.
– learning...
Nov 9 at 19:18













@the_lotus please move your comment as an answer so that i can accept it. Convert.ChangeType(dataObject, Nullable.GetUnderlyingType(p.PropertyType)) helped solve the issue. I need to use nullable properties plus some other data that is weird as well like ints being passed as decimal. This is a vendor file, i have no control over it.
– learning...
Nov 9 at 19:51




@the_lotus please move your comment as an answer so that i can accept it. Convert.ChangeType(dataObject, Nullable.GetUnderlyingType(p.PropertyType)) helped solve the issue. I need to use nullable properties plus some other data that is weird as well like ints being passed as decimal. This is a vendor file, i have no control over it.
– learning...
Nov 9 at 19:51












ChangeType is extraneous even for C#. The line from the_lotus is basically identical to the code I use for the same purpose in a C# module.
– Craig
Nov 9 at 20:01




ChangeType is extraneous even for C#. The line from the_lotus is basically identical to the code I use for the same purpose in a C# module.
– Craig
Nov 9 at 20:01












@learning... I'm glad it helped. I don't know how to properly write an answer for this but I copy/pasted my comment.
– the_lotus
Nov 13 at 12:46




@learning... I'm glad it helped. I don't know how to properly write an answer for this but I copy/pasted my comment.
– the_lotus
Nov 13 at 12:46












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Your problem is with ChangeType, do you even need it? If you do, detect if the property is Nullable, if it is you can then use GetUnderlyingType



Convert.ChangeType(dataObject, Nullable.GetUnderlyingType(p.PropertyType))





share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230759%2fpassing-the-type-of-the-property-to-generic-function%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Your problem is with ChangeType, do you even need it? If you do, detect if the property is Nullable, if it is you can then use GetUnderlyingType



    Convert.ChangeType(dataObject, Nullable.GetUnderlyingType(p.PropertyType))





    share|improve this answer
























      up vote
      1
      down vote



      accepted










      Your problem is with ChangeType, do you even need it? If you do, detect if the property is Nullable, if it is you can then use GetUnderlyingType



      Convert.ChangeType(dataObject, Nullable.GetUnderlyingType(p.PropertyType))





      share|improve this answer






















        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Your problem is with ChangeType, do you even need it? If you do, detect if the property is Nullable, if it is you can then use GetUnderlyingType



        Convert.ChangeType(dataObject, Nullable.GetUnderlyingType(p.PropertyType))





        share|improve this answer












        Your problem is with ChangeType, do you even need it? If you do, detect if the property is Nullable, if it is you can then use GetUnderlyingType



        Convert.ChangeType(dataObject, Nullable.GetUnderlyingType(p.PropertyType))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 at 12:46









        the_lotus

        9,39912245




        9,39912245



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230759%2fpassing-the-type-of-the-property-to-generic-function%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo