struct Nullable<T> IMHO . - Nullable<String> Nullable<Nullable<Nullable<int>>> , ? ; HasValue, null. int, null HasValue, HasValue , .
T, , T . , , , T, struct, class, .
, ; , /, .
static class _FooDispatcher<T>
{
public static Action<T> Foo = setupFoo;
static void doFooWithIGoodFoo<TT>(TT param) where TT : IGoodFoo
{
Console.WriteLine("Dispatching as IGoodFoo with {1} type {0}", typeof(TT).ToString(), typeof(TT).IsValueType ? "value" : "reference");
param.Foo();
}
static void doFooWithIOkayFoo<TT>(TT param) where TT : IOkayFoo
{
Console.WriteLine("Dispatching as IOkayFoo with {1} type {0}", typeof(TT).ToString(), typeof(TT).IsValueType ? "value" : "reference");
param.Foo();
}
static void doFooSomehow<TT>(TT param)
{
Console.WriteLine("Nothing exciting with {1} type {0}", typeof(TT).ToString(), typeof(TT).IsValueType ? "value" : "reference");
}
static void setupFoo(T param)
{
System.Reflection.MethodInfo mi;
if (typeof(IGoodFoo).IsAssignableFrom(typeof(T)))
mi = typeof(_FooDispatcher<T>).GetMethod("doFooWithIGoodFoo", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
else if (typeof(IOkayFoo).IsAssignableFrom(typeof(T)))
mi = typeof(_FooDispatcher<T>).GetMethod("doFooWithIOkayFoo", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
else
mi = typeof(_FooDispatcher<T>).GetMethod("doFooSomehow", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
Foo = (Action<T>)(@Delegate.CreateDelegate(typeof(Action<T>), mi.MakeGenericMethod(typeof(T))));
Foo(param);
}
}
source
share