16
ジェネリッククラスの静的フィールドは、ジェネリックパラメータの組み合わせごとに別々の値を持ちます。したがって、この良くも悪くも静的辞書<タイプ、どんな >よりは何でも >静的ジェネリッククラスを辞書として
辞書<タイプ、として使用することができますか?
つまり、これらの実装のどちらが効率的ですか?特に
public static class MethodGen<TParam> {
public static readonly Action<TParam> Method = CreateMethod();
static Action<TParam> CreateMethod() { /*...*/ }
}
あるいは、
public static class MethodGen {
static readonly Dictionary<Type, Delegate> methods
= new Dictionary<Type, Delegate>();
public static Action<T> GetMethod<T>() {
//In production code, this would ReaderWriterLock
Delegate method;
if(!methods.TryGetValue(typeof(T), out method)
methods.Add(typeof(t), method = CreateMethod<T>());
return method;
}
static Action<T> CreateMethod<T>() { /*...*/ }
}
、どのようにCLRは、ジェネリック型パラメータで静的なフィールドを検索しますか?
どのように静的なアクションを実装しますか? CreateMethod ()? –
ちょうど私がそれについて考えていた時! +1 – nawfal