Как вызвать универсальный метод с более сильным ограничением? - PullRequest
0 голосов
/ 15 сентября 2010
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 ]

1 голос
/ 15 сентября 2010

Вы хотите сказать, что хотите иметь возможность вызывать метод 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();
    }
}
0 голосов
/ 16 сентября 2010

Похоже, вы пытаетесь реализовать шаблон проектирования фабрики. Посмотрите на следующий фрагмент кода. Я убрал интерфейс из ограничений 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>(); 
    }
}
0 голосов
/ 15 сентября 2010

Вы могли бы просто сделать это

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

Если он возвращает null, вы передали тип, который не был ISomeInterface.

...