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.
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.
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.
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.
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.
You are not passing the type itself there. You need to use the typeof()
command like so:
CheckIfAssignable(3, typeof(Nullable<Int32>))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With