views:

15

answers:

2

Hi,

I am using Fieldinfo.FieldType.FullName to get the field datatype. For a string i get System.String but for a Double i get

System.Nullable`1[[System.Double, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]

similarly for DateTime i get System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

i need to check in my code if this particular field is a datetime then do certain action. How can i check that the field is a double or string or double etc

Thanks in advance

+1  A: 

The problem here is the field in question is actually a double? and DateTime? and not a plain old double / DateTime. The ? trailing the type name is a shorthand for the full name Nullable<double> which is not the same as a double.

It sounds like you actually want to use this field though. There are a couple of ways you could approach this

  • Make the field type actually double instead of double?
  • Do the action if the type is double or double?.
JaredPar
I am getting this data from salesforce. I am really not sure if the datatype is double or double?. My problem is how would i check which datatype is it. I am checking the datatype by using the FieldInfo class. Fieldinfo.FieldType.FullName. This returns the version culture etc along with the Syatem.DateTime. as mentioned in my question. How can i confirm the type is double or string or datetime in the If statement
Prady
+3  A: 

JaredPar's answer is a nice explanation. For a solution, try:

Type fieldType = fieldInfo.FieldType;
if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(Nullable<>))
    fieldType = fieldType.GetGenericArguments()[0];

Now you can just say:

if (fieldType == typeof(double))
    ...

etc. This code will basically "undo" the nullable type semantics.

Kirk Woll