How to get a Nullable<T> instance's type

Matias Cicero

If I do this:

Type type = typeof(Nullable<int>);

The type variable holds the correct Nullable'1 type.

However, If I try to do this:

Nullable<int> n = 2;
Type type = n.GetType();

type ends up holding System.Int32 which is the underlying type, but not what I am expecting.

How can I get the type of a Nullable<> instance?

Keep in mind that I don't know the underlying type of the nullable instance, so I cannot call typeof.

clcto

Not elegant, but you can wrap it in a generic function:

public static void Main()
{
    int? i = 2;
    Console.WriteLine( type(i) );
}

public static Type type<T>( T x ) { return typeof(T); }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create an instance of a Nullable<T>?

From Java

How do I get a class instance of generic type T?

From Dev

How to make an Integer instance not nullable

From Dev

How do I get a string representation from a nullable type?

From Dev

How does the compiler recognise Nullable<T> to be a 'special type'?

From Dev

Create instance of generic class based on nullable type

From Dev

Class of type <T> with nullable value of Type<T>

From Dev

Class of type <T> with nullable value of Type<T>

From Dev

How to get the parameterized type of an instance with Dart and smoke?

From Dev

How can I get a Binary instance of the thyme package's UTCTime data type?

From Dev

How to create an instance of type T at runtime with TypeTags

From Dev

How to create an instance just by generic type T

From Dev

Determine if a type is Nullable and get the base type?

From Dev

Check if property is either of type or it's nullable type

From Java

How to declare a type as nullable in TypeScript?

From Dev

How to cast an int into a nullable type?

From Dev

How to cast an int into a nullable type?

From Dev

how to get an aws instance given I have the instance's ip

From Dev

how to get an aws instance given I have the instance's ip

From Dev

How get correct type of T

From Dev

How to get type of T generic?

From Dev

Get an instance of generic type

From Dev

How to get a pointer type's pointed type?

From Java

How to specify "nullable" return type with type hints

From Dev

How to specify "nullable" return type with type hints

From Dev

How to get at a C++ Container<T>'s T if no Container::value_type is provided?

From Dev

TypeScript guard for nullable type doesn't work

From Dev

Safest way to get the Invoke MethodInfo from Action<T>'s Instance

From Dev

How to get instance of T of an Expression<Func<T, TK>>

Related Related

  1. 1

    How to create an instance of a Nullable<T>?

  2. 2

    How do I get a class instance of generic type T?

  3. 3

    How to make an Integer instance not nullable

  4. 4

    How do I get a string representation from a nullable type?

  5. 5

    How does the compiler recognise Nullable<T> to be a 'special type'?

  6. 6

    Create instance of generic class based on nullable type

  7. 7

    Class of type <T> with nullable value of Type<T>

  8. 8

    Class of type <T> with nullable value of Type<T>

  9. 9

    How to get the parameterized type of an instance with Dart and smoke?

  10. 10

    How can I get a Binary instance of the thyme package's UTCTime data type?

  11. 11

    How to create an instance of type T at runtime with TypeTags

  12. 12

    How to create an instance just by generic type T

  13. 13

    Determine if a type is Nullable and get the base type?

  14. 14

    Check if property is either of type or it's nullable type

  15. 15

    How to declare a type as nullable in TypeScript?

  16. 16

    How to cast an int into a nullable type?

  17. 17

    How to cast an int into a nullable type?

  18. 18

    how to get an aws instance given I have the instance's ip

  19. 19

    how to get an aws instance given I have the instance's ip

  20. 20

    How get correct type of T

  21. 21

    How to get type of T generic?

  22. 22

    Get an instance of generic type

  23. 23

    How to get a pointer type's pointed type?

  24. 24

    How to specify "nullable" return type with type hints

  25. 25

    How to specify "nullable" return type with type hints

  26. 26

    How to get at a C++ Container<T>'s T if no Container::value_type is provided?

  27. 27

    TypeScript guard for nullable type doesn't work

  28. 28

    Safest way to get the Invoke MethodInfo from Action<T>'s Instance

  29. 29

    How to get instance of T of an Expression<Func<T, TK>>

HotTag

Archive