Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a nullable type from a type name

Tags:

c#

types

nullable

I have the following scenario where I get a string that looks as follows

"System.DateTime?"

or

"int?"

What I would like to be able to do, is retrieve the System.Type for that string which would look like:

{Name = "Nullable1" FullName = "System.Nullable1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}

Now I know how to retrieve the type from a string I can just say Type.GetType("System.DateTime") when dealing with non nullable type or typeof(DateTime?) when I know it's going to be DateTime nullable, but in this instance I'm unaware of the what nullable type might come through and will only receive it as string.

like image 636
Domitius Avatar asked Feb 13 '18 18:02

Domitius


People also ask

How do you make a type nullable?

You can declare nullable types using Nullable<t> where T is a type. Nullable<int> i = null; A nullable type can represent the correct range of values for its underlying value type, plus an additional null value. For example, Nullable<int> can be assigned any value from -2147483648 to 2147483647, or a null value.

Is nullable type reference type?

Nullable reference types aren't new class types, but rather annotations on existing reference types. The compiler uses those annotations to help you find potential null reference errors in your code. There's no runtime difference between a non-nullable reference type and a nullable reference type.

What is nullable type in C hash?

The Nullable type allows you to assign a null value to a variable. Nullable types introduced in C#2.0 can only work with Value Type, not with Reference Type. The nullable types for Reference Type is introduced later in C# 8.0 in 2019 so that we can explicitly define if a reference type can or can not hold a null value.

What is a nullable Boolean?

You typically use a nullable value type when you need to represent the undefined value of an underlying value type. For example, a Boolean, or bool , variable can only be either true or false . However, in some applications a variable value can be undefined or missing.


3 Answers

I'm missing part of the context - for example, does the string always represent a nullable value type?

Given that you can extract the value type name from the string and create a reference to that type:

var valueType = Type.GetType("System.DateTime");
var nullableType = typeof(Nullable<>).MakeGenericType(valueType);

That should solve part of the problem. The harder part will be determining the underlying type from the string, which depends on what sort of inputs you're receiving. It's easy to get a type from System.DateTime, but harder from int. This answer may help with that part.


As far as the input - is there any option that you could provide a list of available choices to the user so that input you receive would always be a valid type name? That would negate the need for any of this. Then you could just do Type.GetType(stringWithTypeName).

like image 111
Scott Hannen Avatar answered Oct 12 '22 23:10

Scott Hannen


In addition to Scott's answer, you could also use the Type.GetType with the correct name:

System.Nullable`1[System.DateTime]

So this function will get back the correct type for you.

public Type GetType(string typeString)
{
    if (typeString.EndsWith("?"))
    {
        typeString = $"System.Nullable`1[{typeString.TrimEnd('?')}]";
    }
    return Type.GetType(typeString);
}
like image 30
Ofir Winegarten Avatar answered Oct 13 '22 00:10

Ofir Winegarten


One way to solve this is by creating an extension method for type "Type" and make the implementation of that method to check if it is a nullable type and return the right type. follows example:

namespace ExtensionMethod  
    {
        public static class MyExtensions
        {
            public static Type GetNullabeType(this Type t, string typeName)
            {
                string typeString = typeName;
                var typeNonNullabe = Type.GetType(typeString.Replace("?", ""));
                if (typeString.Contains("?") && typeNonNullabe != null)
                {
                    return typeof(Nullable<>).MakeGenericType(typeNonNullabe);
                }
                return t;
            }
        }
    }

after that, just use this extension method as follows:

Type t = null;
var type = t.GetNullabeType("System.DateTime?");
like image 20
Bruno Cavallari Gois Avatar answered Oct 12 '22 22:10

Bruno Cavallari Gois