4
votes

I've got an object which I create out of an entity(T) using reflection. The object is my implementation of a table. It holds a list of columns, and I extract their properties from the entity using reflection :

  public class Generic_Table<T> : Table 
  {        
      ...// in ctor 
      type = this.GetType().GetGenericArguments()[0]; // type of T 
      BuildColumns(type); 

      private void BuildColumns(Type type)
      {
          PropertyInfo[] properties = type.GetProperties();

          Columns = new KeyValuePair<string, Type>[properties.Count()];
          int i = 0;
          foreach (PropertyInfo property in properties)
          {
              Columns[i++] = new KeyValuePair<string, Type>(property.Name, property.PropertyType);                
          }
      } 

I'm looking for a way to cast the PropertyType value as a nullable type, so that the Type value in columns would be int? if, for example, some property has int for its PropertyType value.

1
i need to get 15 reputation before i can accept answers so i really , cant work on it yet ...eran otzap
ok, iv'e marked all the help full answers .. i don't know why it says 100% now .. i didn't mark them all but i did mark one where ever i got an answereran otzap

1 Answers

4
votes

This will do what you need:

Type nullableType = typeof(Nullable<>).MakeGenericType(property.PropertyType);

The MakeGenericType method accepts a params array of the generic type arguments. See the documentation for more details:

Also, this article has a good example of something similar to what you're doing here: