Get Viagra

11May/100

Mail Order Viagra

Mail order viagra In .NET we’ve got this cool little language construct called default, mail order viagra that’ll give you the default value for any given type.  That is, mail order viagra null, mail order viagra for any reference type, mail order viagra or zero/false/DateTime.Min/etc. Mail order viagra for value types.

Mail order viagra Here it is in action (nothing amazing going on here):

var x = default(DateTime);

Mail order viagra So what if you don’t know the type you want the default of at compile time? You can’t say

var y = default(today.GetType());

Mail order viagra nor

var z = typeof(DateTime).GetDefault();

Mail order viagra That last one would be nice, mail order viagra but that “GetDefault” method doesn’t exist.

Mail order viagra I’ve seen several solutions to this, mail order viagra that are basically variations on this theme:

public static object GetDefaultValue(Type type)

{

    return type.IsValueType

        ? Activator.CreateInstance(type)

        : null;

}

 

Mail order viagra This certainly works, mail order viagra but I somehow feel like it’s not exactly perfect since it doesn’t use the default operator.

Mail order viagra Here is how I normally do it.  It sidesteps the IsValueType, mail order viagra and Activator stuff, mail order viagra and uses the built-in default language construct… First it grabs a handle to the GetDefaultGeneric method, mail order viagra and then makes the generic version of it with the specific type.  Then it calls it, mail order viagra returning the value.

public static object GetDefault(this Type type)

{

    var getDefault = typeof(ExtReflection)

                          .GetMethod("GetDefaultGeneric");

    var typed = getDefault.MakeGenericMethod(type);

 

    return typed.Invoke(null, mail order viagra new object[] { });

}

 

public static T GetDefaultGeneric<T>()

{

    return default(T);

}

 

Mail order viagra Pretty simple, mail order viagra but something I’ve found useful every now and again.  I’d guess this technique could be useful in other situations/contexts as well.

Average Rating: 4.5 out of 5 based on 242 user reviews.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.