views:

113

answers:

3
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
}

I was forced to add IAdditional to SomeOtherImp to be able to call StaticClass.DoBar And now I can't implement ISome with SomeOtherImp....

+1  A: 

Do you mean that you want to be able to call the Get method? If you can edit the interface ISome, try this:

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

...otherwise you're going to have to use reflection:

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();
    }
}
Andrew
Unfortunately i can't edit ISome, SomeStaticClass
Broken Pipe
Heh I was hoping this can be done without reflection with some inheritance magic.. looks like not.
Broken Pipe
A: 

You could just do this

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

If it returns null, you passed in a type that was not an ISomeInterface.

kevev22
Looks like I have oversimplified my code actualy Get<T>() returns IBar<T> I should edit the question )
Broken Pipe
A: 

It looks like you are trying to implement factory design pattern. Take a look at the following piece of code. I removed the interface from restrictions of SomeClass. It compiles and will work. In my opinion ISome and its implementation Foo class are obsolete.

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>(); 
    }
}
Boris Modylevsky