.
public static class Cache
{
private static List<Dog> _dogs = new List<Dog>();
private static List<Cat> _cats = new List<Cat>();
public static TAnimal GetCachedObj<TAnimal>() where T: Animal
{
if(TAnimal == typeof(Dog))
return (TAnimal) _dogs.First();
else if (TAnimal == typeof(Cat))
return (TAnimal) _cats.First();
else throw new InvalidOperationException("Invalid generic type argument");
}
}
: .
LSP , T (, Cat) Animal, Animal T - .
. , , Giraffe. , GetCachedObj<Giraffe>, ! Animal, LSP !
public class Cache<T> where T: Animal
{
private static List<T> _animals = new List<T>();
public T GetCachedObj()
{
return _animals.First();
}
}
var dogsCache = new Cache<Dog>();
Dog dog = dogsCache.GetCachedObj();
var catsCache = new Cache<Cat>();
Cat cat = catsCache.GetCachedObj();
.
. , Cache . Singleton, ( ) Injection Dependency ( , Castle Windsor), .
generic ( @Sean), .
public class MyClass<T>
{
public FirstDelegate<T> Method(){...}
}
T unbound ( ), T , :
public FirstDelegate<T> Method<T>(){...}
, T . T MyClass (.. new MyClass<int>), List<T>.