create nullable type with string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dan Holmes

    #1

    create nullable type with string

    i need to get an object of type Type when i have a string. That is the
    easy part (Type.GetType(" int")). What if i want that type to be
    nullable? This doesn't work Type.GetType("i nt?"). It returns null.

    This does work but i don't know how to know all that information at runtime.

    Type.GetType("S ystem.Nullable` 1[[System.Int32, mscorlib,
    Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9]]")

    dan
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: create nullable type with string

    Dan,

    Assuming you have the type for T in Nullable<T>, you can do the
    following:

    // This is where you would get typeof(int)
    Type typeParameter = ...

    // Get the Nullable<Tgener ic type.
    Type nullable = typeof(Nullable <>);

    // Now make the constructed type.
    Type constructedType = nullable.MakeGe nericType(new Type[]{typeParameter} );

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Dan Holmes" <danholmes@bigf oot.comwrote in message
    news:uHmQXRaFIH A.1184@TK2MSFTN GP04.phx.gbl...
    >i need to get an object of type Type when i have a string. That is the
    >easy part (Type.GetType(" int")). What if i want that type to be nullable?
    >This doesn't work Type.GetType("i nt?"). It returns null.
    >
    This does work but i don't know how to know all that information at
    runtime.
    >
    Type.GetType("S ystem.Nullable` 1[[System.Int32, mscorlib, Version=2.0.0.0 ,
    Culture=neutral , PublicKeyToken= b77a5c561934e08 9]]")
    >
    dan

    Comment

    Working...