C# c对静态函数的泛型方法调用

C# c对静态函数的泛型方法调用,c#,generics,static,C#,Generics,Static,我在类中有一个泛型方法,如下所示 private static Dictionary<Type, Feed> _singletons = new Dictionary<Type, Feed>(); public static Feed GetFeed<T>() where T:Feed { lock(_padlock) { if (!_singletons.Contain

我在类中有一个泛型方法,如下所示

    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)];          
        }
    }

这里,Feed是一个接口,Type是实现Feed接口的类的类型。GetInstance是这些类中的静态方法。typeofT.GetInstance;是否有问题;?它表示System.Type不包含GetInstance的定义。

最简单的方法是使用新的约束

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)];          
    }
}

最简单的方法是使用新约束

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)];          
    }
}

您可以使用反射调用静态方法,如下所示:

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)];          
    }
}

您可以使用反射调用静态方法,如下所示:

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)];          
    }
}

因为typeof返回一个Type对象。类型类没有这样的方法。是。我得到的那部分。那么,这个调用的替代方法是什么呢?Type Oft操作符返回Syt.Type的一个实例,而不是T.T.的实例。Singleton是一个非常讨厌的反模式,再加上反射魔法,看起来你在为自己的应用程序设置一个有问题的可维护性。问问你自己,你的类型字典有什么好处:为什么客户端要编写var x=Feeds.GetFeed而不是var x=new MyFeedImpl?这是工厂模式的一部分很酷:我仍然建议将工厂方法设置为非静态,并返回类的新实例,而不是邪恶的单例。工厂意味着在对象实例及其构造函数之间放置一层间接层,以便创建的对象可以独立于使用工厂的人而变化,即类似于根据配置返回FileLogger或DbLogger的LogFactory类。静态工厂方法防止降低可测试性,并且指定工厂返回的具体类型似乎很奇怪,因为Feeds.GetFeed==X.GetInstance.cause typeof返回类型对象。类型类没有这样的方法。是。我得到的那部分。那么,这个调用的替代方法是什么呢?Type Oft操作符返回Syt.Type的一个实例,而不是T.T.的实例。Singleton是一个非常讨厌的反模式,再加上反射魔法,看起来你在为自己的应用程序设置一个有问题的可维护性。问问你自己,你的类型字典有什么好处:为什么客户端要编写var x=Feeds.GetFeed而不是var x=new MyFeedImpl?这是工厂模式的一部分很酷:我仍然建议将工厂方法设置为非静态,并返回类的新实例,而不是邪恶的单例。工厂意味着在对象实例及其构造函数之间放置一层间接层,以便创建的对象可以独立于使用工厂的人而变化,即类似于根据配置返回FileLogger或DbLogger的LogFactory类。静态工厂方法防止降低可测试性,并且指定工厂返回的具体类型似乎很奇怪,因为Feeds.GetFeed==X.GetInstance。您还需要BindingFlags.Public。您还需要BindingFlags.Public。