-3

Possible Duplicate:
Detecting a Nullable Type via reflection

I have this code :

string type = string.Empty;
PropertyInfo[] propertyInfos = typeof(T).GetProperties();
foreach (var item in propertyInfos)
    if (item.Name.ToUpper() == searchField.ToUpper())
    {
        type = item.PropertyType.FullName;
        break;
    }   

switch (type)
{
    case "System.Int32":
        //...
        break;
    case "System.Single":
        //...
        break;
}

The code works but the problem is when the type is a nullable. How know if the type is a nullable ? The nullable type (int32? long? double?) and how convert a string into this nullable type ?

Thanks,

Community
  • 1
  • 1
TheBoubou
  • 19,487
  • 54
  • 148
  • 236
  • 2
    Did you do any research before posting the question here?? Doesn't look like that as @Habib had found. Please do some reachable before posting any question :) – palaѕн Dec 18 '12 at 07:27

1 Answers1

0

try out below code will help you for that

System.Reflection.FieldInfo[] fieldsInfos = typeof(NullWeAre).GetFields();

        foreach (System.Reflection.FieldInfo fi in fieldsInfos)
        {
            if (fi.FieldType.IsGenericType
                && fi.FieldType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
            {
                // We are dealing with a generic type that is nullable
                Console.WriteLine("Name: {0}, Type: {1}", fi.Name, Nullable.GetUnderlyingType(fi.FieldType));
            }

    }
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263