I began the process of upgrading one of our Beta2 applications to run under the 2.0 RTM and the compiler is complaining that INullableValue cannot be found in the System namespace. Using the object browser I can verify that there isn't a INullableValue interface defined in the mscorlib assembly version 2.0.50727.
Was it intentionally removed If so, why has the documentation not been updated to reflect that change
The code I am trying to use has this form:
if ( value is INullableValue && ( (INullableValue) value ).HasValue == false )
{
//do something
}
else if ( value == null )
{
//do something else
}
How can this logic be reproduced without the INullableValue interface

INullableValue removed from .NET 2.0 RTM??
Maheswaran
Note that if i is assigned a value such as 42, then (i is int) and (i is int ) both evaluate to true.
So I don't think there is a way around this, but I might be wrong.
boulder
BoneCrusher
Big V
That's very interesting, but this hurts quite a bit. I wish they had left the Nullable<T> class implementing the INullableValue interface and made the CLR change.
The code block in my post is part of a data abstraction layer and it needs to be able to differentiate between nullable value types and nullable objects since the system handles the semantics of objects and values differently.
As it stands, I have no idea how to cleanly implement the intended logic with the new language features. I was thinking of trying to check whether the value was null and then to also check the value's type to see whether the IsValueType property is true, but since the null check is going to always return null, I'm not sure how to do that.
public void SomeMethod (object value)
{
if (value == null && IsNullableValueType(value))
{
//special case for nullable value types
} else {
//normal case
}
}
private bool IsNullableValueType( object value )
{
//what should go here
}
RobertRFreeman
I came up with a solution that suits my present purpose, but it will not due for generic purposes. I am walking the readable/writable properties of a class (the data abstraction layer uses reflection) and pulling PropertyInfo information for each property that is found, so I have that type information to work with.
So, I made a nullable value type checking method that requires both a value and its type information to do the trick:
public static bool IsNullableValueType(object value, Type type){
if (!type.IsValueType)
{
return false;
}
if (value == null)
{
return true;
}
if (type.GetProperty("HasValue") != null)
{
return true;
}
return false;
}
This is awful that we have to do this kind of thing... we need INullableValue back! The solution I'm using will not work in most cases since you'll need to already known the object's undelying type before calling the method -- value.GetType() will not work since null Nullable<T> cases will evaluate to calling GetType() on a null reference.
The problem that the C# team was attempting to solve by moving Nullable<T> dereferencing into the CLR simply did not exist on a broad scale. The fact that we had to write special logic to handle INullableValue was a good thing -- it was a special case after all.