I have a generic method in a class as follows
private static Dictionary<Type, Feed> _singletons = new Dictionary<Type, Feed>();
public static Feed GetFeed<T>() where T:Feed
{
lock(_padlock)
{
if (!_singletons.ContainsKey(typeof(T))
{
_singletons[typeof(T)] = typeof(T).GetInstance();
}
return _singletons[typeof(T)];
}
}
Here, Feed is an interface and Type is of types of classes that implement the Feed interface. GetInstance() is a static method in these classes. Is there something wrong with typeof(T).GetInstance();? It says System.Type does not contain a definition for GetInstance().
typeof()returns aTypeobject. TheTypeclass doesn't have such a method.typeof(T)operator returns an instance ofSystem.Type, not an instance ofT. Now with that being said, I think you should really consider re-writing whatever you're trying to do. Singleton is a very nasty anti-pattern, and combined with reflection magic, seems like you're setting yourself up for an application of questionable maintainability. Ask yourself what advantage you get with your type dictionary: why would a client writevar x = Feeds.GetFeed<MyFeedImpl>()instead ofvar x = new MyFeedImpl()?Feeds.GetFeed<X>()==X.GetInstance().