Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a `Nullable<T>` as a Type parameter to a C# function

This is within a .NET Core 1.1.4 project, so please take this into consideration.

I'm trying to create a function that would verify if a value can be assigned to a type, but I'm encountering an issue with Nullable<T> types.

My function:

    protected void CheckIsAssignable(Object value, Type destinationType)
    {
        if (value == null)
        {
            // Nullable.GetUnderlyingType returns null for non-nullable types.
            if (Nullable.GetUnderlyingType(destinationType) == null)
            {
                var message =
                    String.Format(
                        "Property Type mismatch. Tried to assign null to type {0}",
                        destinationType.FullName
                    );
                throw new TargetException(message);
            }
        }
        else
        {
            // If destinationType is nullable, we want to determine if
            // the underlying type can store the value.
            if (Nullable.GetUnderlyingType(destinationType) != null)
            {
                // Remove the Nullable<T> wrapper
                destinationType = Nullable.GetUnderlyingType(destinationType);
            }
            // We can now verify assignability with a non-null value.
            if (!destinationType.GetTypeInfo().IsAssignableFrom(value.GetType()))
            {
                var message =
                    String.Format(
                        "Tried to assign {0} of type {1} to type {2}",
                        value,
                        value.GetType().FullName,
                        destinationType.FullName
                    );
                throw new TargetException(message);
            }
        }
    }

The if clause above handles the case if value is null, and attempts to verify that the destinationType is Nullable<T>; the else clause handles if value actually contains something, so it attempts to determine if you can assign it to destinationType or, if it's a Nullable<T>, that it's assignable to T.

The problem is that Nullable<T> isn't a Type, so calling CheckIfAssignable(3, Nullable<Int32>) doesn't match the function signature.

Changing the signature to:

protected void CheckIsAssignable(Object value, ValueType destinationType)

lets me pass a Nullable<T>, but then I can't submit it as an argument to Nullable.GetUnderlyingType.

I'm not sure if I've over-complicated the issue with this, but I feel there is a simple solution that I'm just not seeing.

like image 677
taserian Avatar asked Nov 21 '17 16:11

taserian


People also ask

How do you pass a null parameter?

You can pass NULL as a function parameter only if the specific parameter is a pointer. The only practical way is with a pointer for a parameter. However, you can also use a void type for parameters, and then check for null, if not check and cast into ordinary or required type.

Can a generic type be null?

This means that you can put any object in a collection because all classes in the C# programming language extend from the object base class. Also, we cannot simply return null from a generic method like in normal method.

How do you make a variable nullable in C#?

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.

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.


1 Answers

You are not passing the type itself there. You need to use the typeof() command like so:

CheckIfAssignable(3, typeof(Nullable<Int32>))
like image 103
David Watts Avatar answered Oct 12 '22 01:10

David Watts