I have following classes under different namespaces. I mean, the same 3 classes exist under different namespaces.
public class A
{
public int a { get; set; }
}
public class B
{
public A objA { get; set; }
}
public class C
{
public List<B> listBinC { get; set; }
}
In order to utilize/operate between the objects of these classes I thought of writing an interface wrapper such as
public interface iA
{
int a { get; set; }
}
public interface iB<T> where T: iA
{
T objA { get; set; }
}
public interface iC<T> where T : iB<iA>
{
List<T> listBinC {get; set; }
}
After this I have changed my Class definitions to
public class A : iA
{
public int a { get; set; }
}
public class B : iB<A>
{
public A objA { get; set; }
}
class C : iC<B>
{
public List<B> listBinC { get; set; }
}
When Compiled I am getting the following Error
The type 'Example.B' cannot be used as type parameter 'T' in the generic type or method 'Example.iC<T>'.
There is no implicit reference conversion from 'Example.B' to 'Example.iB<Example.iA>'.
I cannot change my class structure as it is provided by different team. What is the right way to enforce the interface 'constraints' to resolve the error?
public interface iC<T, TI>
where T : iB<TI>
where TI : iA
{
List<T> listBinC {get; set; }
}
Here iA
is pulled out as a separate generic parameter, so you can apply a constraint on it (for TI
to be derived from iA
) and then declare B
like this:
class C : iC<B, A>
{
public List<B> listBinC { get; set; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.