Derived interface from generic method

Posted by Sunit on Stack Overflow See other posts from Stack Overflow or by Sunit
Published on 2010-05-18T20:48:02Z Indexed on 2010/05/18 20:50 UTC
Read the original article Hit count: 263

Filed under:

I'm trying to do this:

public interface IVirtualInterface{ }

public interface IFabricationInfo : IVirtualInterface
{
    int Type { get; set; }
    int Requirement { get; set; }
}

public interface ICoatingInfo : IVirtualInterface
{
    int Type { get; set; }
    int Requirement { get; set; }
}

public class FabInfo : IFabricationInfo {

    public int Requirement
    {
        get { return 1; }
        set { }
    }

    public int Type
    {
        get {return 1;}
        set{}
    }
}

public class CoatInfo : ICoatingInfo
{
    public int Type
    {
        get { return 1; }
        set { }
    }
    public int Requirement
    {
        get { return 1; }
        set { }
    }
}

public class BusinessObj
{
    public T VirtualInterface<T>() where T : IVirtualInterface
    {
        Type targetInterface = typeof(T);
        if (targetInterface.IsAssignableFrom(typeof(IFabricationInfo)))
        {
            var oFI = new FabInfo();
            return (T)oFI;
        }

        if (targetInterface.IsAssignableFrom(typeof(ICoatingInfo)))
        {
            var oCI = new CoatInfo();
            return (T)oCI;
        }
        return default(T);
    }
}

But getting a compiler error: Canot convert type 'GenericIntf.FabInfo' to T

How do I fix this?

thanks Sunit

© Stack Overflow or respective owner

Related posts about c#