I have need for nesting Generics, as in A< B< Base > > .
When I do this, only the outer properties (A) are exposed. I can not figure out how to access (B)'s methods, etc. I then tried to access the interfaces internally with the same results.
(edit) To clarify the use case, the solution I need should work with public class C : A < B < Base > > or public class C : B < A < Base > > I don't need those to result in identical classes, but both definitions have the corresponding methods. As you might suspect, I am trying to use this to implement common functionality in a modular mode across several objects. Extension methods get me close, but they won't allow overridden behavior as this solution would (if it is achievable).
I have attached test code, which shows the problems perhaps more clearly than I ca.
using System;
using System.Reflection;
namespace ArchitecturalTestGround
{
public interface IBase
{
void BaseMethod1();
}
public interface IA : IBase
{
void AMethod();
}
public interface IB : IBase
{
void BMethod();
}
public class Base : IBase
{
public void BaseMethod1() { }
}
public class A<T> : IA where T : IBase
{
public void BaseMethod1() { }
public void AMethod() { }
}
public class B<T> : IB where T : IBase
{
public void BaseMethod1() { }
public void BMethod() { }
}
public class Test1 : A<B<Base>>
{
}
public class Test2 : B<A<Base>>
{
}
public class Program
{
public static void Main(string[] args)
{
Test1 e1 = new Test1();
Test2 e2 = new Test2();
Console.WriteLine("Test1 - A<B<Base>>");
foreach (MemberInfo mi in typeof(Test1).GetMembers())
{
Console.WriteLine($" {mi.Name}.{mi.MemberType}");
}
if (e1 is IB) { Console.WriteLine(" Supports IB"); }
if (e1 is IA) { Console.WriteLine(" Supports IA"); }
Console.WriteLine();
Console.WriteLine("Test2 - B<A<Base>>");
foreach (MemberInfo mi in typeof(Test2).GetMembers())
{
Console.WriteLine($" {mi.Name}.{mi.MemberType}");
}
if (e2 is IB) { Console.WriteLine(" Supports IB"); }
if (e2 is IA) { Console.WriteLine(" Supports IA"); }
Console.ReadKey();
}
}
}