开发者

c#: IsNullableType helper class?

Can anyone help?

I have some code that is shared between 2 projects. The code points to a model which basically is a collection of properties that comes from a db.

Problem being is that some properties use nullable types in 1 model and the other it doesn't

Really the dbs should use the same but they don't ..

so for example there is a property called IsAvailble which uses "bool" in one model and the other it uses bool? (nullable type)

so in my code i do the following

 objContract.IsAvailble.Value ? "Yes" : "No"   //notice the property .VALUE as its a bool? (nullable type)

but this line will fail on model that uses a standard "bool" (not nullable) as there is no property .VALUE on types that are NOT nullable

Is there some kind of helper class that i check if the property is a nullable type and i can return .Value .. otherwise i just return the property.

Anybody have a solution for this?

EDIT

This is what i have now..... i am checking HasValue in the nullable type version

public static class NullableExtensions { public static T GetValue(this T obj) where T : struct { return obj; } public static T GetValue(this Nullable obj) where T : struct { return obj.Value; }

    public static T GetValue<T>(this T obj, T defaultValue) where T : struct
    {
        return obj;
 开发者_开发技巧   }

    public static T GetValue<T>(this Nullable<T> obj, T defaultValue) where T : struct
    {
        if (obj.HasValue)
            return obj.Value;
        else
            return defaultValue;
    }
}


This is a little weird, but maybe you can use an extension method here:

static class NullableExtensions
{
    public static T GetValue<T>(this T obj) where T : struct
    {
        return obj;
    }
    public static T GetValue<T>(this Nullable<T> obj) where T : struct
    {
        return obj.Value;
    }
}

They will work with nullable or regular types:

int? i = 4;
int j = 5;

int a = i.GetValue();
int b = j.GetValue();


I wouldn't cast. use the ?? operator

http://msdn.microsoft.com/en-us/library/ms173224(VS.80).aspx

bool? isAvailble = null;

//string displayIsAvailble = (bool)(isAvailble) ? "Yes" : "No"; //exception Nullable object must have a value.

string displayIsAvailble = (isAvailble ?? false) ? "Yes" : "No";  //outputs "no"

Console.WriteLine(displayIsAvailble);


(bool)(objContract.IsAvailble) ? "Yes" : "No"


Best I can suggest is to always cast to the nullable, then use the null coalescing operator to say what you want the value to be when it's null. e.g.:

    string s3 = (bool?)b ?? false ? "yes" : "no";

The above will work whether b is defined as bool or bool?


Convert.ToBoolean(objContract.IsAvailble) ? "yes" : "no"

OR

Is this what you are looking for?

bool? n = false;
bool nn = true;

Console.WriteLine(n ?? nn); 


One more alternative:

objContract.IsAvailble == true ? "Yes" : "No"

On the nullable, only true is true, null or false is false. On the regular bool, true/false is normal.


You could use:

bool? b1 = objContract.IsAvailable;
string s1 = b1.Value ? "Yes" : "No";`

This should work whether objectContract.IsAvailable is a bool or bool? or any other nullable type.

For dates for example:

DateTime? t1 = objContract.EitherNullableOrNotNullableDate;
string s1 = t1.Value.ToString();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜