1

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().

5
  • 1
    Because typeof() returns a Type object. The Type class doesn't have such a method. Commented Dec 22, 2010 at 5:24
  • Yes. That part I get. So what is the alternative to this call? Commented Dec 22, 2010 at 5:26
  • The typeof(T) operator returns an instance of System.Type, not an instance of T. 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 write var x = Feeds.GetFeed<MyFeedImpl>() instead of var x = new MyFeedImpl()? Commented Dec 22, 2010 at 5:32
  • This is part of a factory pattern Commented Dec 22, 2010 at 5:40
  • Coolness :) I would still recommend making your factory method non-static, and returning new instances of your classes rather than evil singletons. Factories are meant to put a layer of indirection between an object instance and its constructor, so that the object which gets created can vary independently of whoever uses the factory (i.e. like a LogFactory class that returns a FileLogger or DbLogger based on configuration). Static factory methods prevent reduce testability, and specifying the concrete type returned from factory seems weird since Feeds.GetFeed<X>() == X.GetInstance(). Commented Dec 22, 2010 at 6:00

2 Answers 2

2

You can use Reflection to call a static method like so:

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))
        {                   
            return typeof(T).GetMethod("GetInstance", System.Reflection.BindingFlags.Static).Invoke(null,null);

        }
        return _singletons[typeof(T)];          
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

You'll need BindingFlags.Public as well.
2

The simplest way is to use the new constraint

private static Dictionary<Type, Feed> _singletons = new Dictionary<Type, Feed>();

public static Feed GetFeed<T>() where T:Feed, new()
{    
    lock(_padlock)
    {
        if (!_singletons.ContainsKey(typeof(T))
        {                   
            _singletons[typeof(T)] = new T();
        }
        return _singletons[typeof(T)];          
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.