如何调用具有更强约束的泛型方法?

时间:2010-09-15 14:35:15

标签: c# generics constraints

namespace Test
{
    #region Not my code
    public interface IAdditional
    {
    }
    public interface ISome
    {
        ISomeOther<T> GetSomeother<T>() where T : class;
    }
    public interface ISomeOther<T> where T : class
    {
        void DoFoo(T obj);
    }
    public class AnotherClass<T> where T : class
    {
    }
    public static class StaticClass
    {
        public static void DoBar<T>(AnotherClass<T> anotherClass, T obj) where T : class, IAdditional
        {
        }
    }
    #endregion

    #region MyCode
    public class SomeOtherImp<T> : ISomeOther<T> where T : class, IAdditional //Have to add IAdditional constraint to call StaticClass.DoBar
    {
        private AnotherClass<T> _anotherClass;
        public void DoFoo(T obj)
        {
            StaticClass.DoBar<T>(_anotherClass, obj); //I do need to call StaticClass.DoBar here....
        }
    }
    public class ISomeImp : ISome
    {
        public ISomeOther<T> GetSomeother<T>() where T : class
        {
            return new SomeOtherImp<T>(); //Can't do this no IAdditional constraint on T
        }
    }
    #endregion
}

我被迫将IAdditional添加到SomeOtherImp,以便能够致电StaticClass.DoBar

现在我无法使用ISome实施SomeOtherImp<T>

3 个答案:

答案 0 :(得分:1)

你的意思是你想要能够调用Get方法吗?如果您可以编辑ISome界面,请尝试以下方法:

public interface ISome
{
    T Get<T>() where T:class, ISomeInterface
}

...否则你将不得不使用反射:

public class Foo : ISome
{
    public T Get<T>() where T:class
    {
        if (!typeof(ISomeInterface).IsAssignableFrom(typeof(T))) throw new Exception();
        return (T)typeof(SomeStaticClass).GetMethod("Create").MakeGenericMethod(new [] {typeof(T)}).Invoke();
    }
}

答案 1 :(得分:0)

你可以这样做

public class Foo : ISome
{
    public T Get<T>() where T : class
    {
        return SomeStaticClass.Create<ISomeInterface>() as T; 
    }
}

如果返回null,则传入的类型不是ISomeInterface。

答案 2 :(得分:0)

看起来您正在尝试实施工厂设计模式。看看下面这段代码。我从SomeClass的限制中删除了界面。它编译并将工作。在我看来,ISome及其实现Foo类已经过时了。

public static class SomeStaticClass
{
    public static T Create<T>() where T:class
    {
        //Replace with actual construction of T
        return (T)(new object());
    }
}

public interface ISome
{
    T Get<T>() where T : class;
}

public class Foo : ISome
{
    public T Get<T>() where T:class
    {
        return SomeStaticClass.Create<T>(); 
    }
}