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
c# vb.net system.reflection
add a comment |
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
c# vb.net system.reflection
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
add a comment |
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
c# vb.net system.reflection
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
c# vb.net system.reflection
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
add a comment |
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
add a comment |
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))
add a comment |
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))
add a comment |
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))
add a comment |
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))
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))
answered Nov 13 at 12:46
the_lotus
9,39912245
9,39912245
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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