Given this:
class D{}
class A<T>{}
class B<T> : A<T> {}
class C : B<D> {}
enum openT
{
level1, level2
}
I think that you might be looking for this:
public A<T> FactoryMethod<T>(openT i)
{
if(i == openT.level1)
return new A<T>():
if(i == openT.level2)
return new B<T>():
}
public A<D> FactoryMethod()
{
return new C():
}
public static void Main()
{
A<string> first = OpenFactoryMethod<string>(1);
A<int> second = OpenFactoryMethod<int>(2);
A<D> third = FactoryMethod();
}
Note that A cannot be abstract since you are trying to construct it.
I'm failing to see what you are really trying to accomplish here though, since C is a closed type, and therefore your factory method will never make sense for it.
UPDATED
The following might be closer to what you are looking for:
public TAofT FactoryMethod<TAofT, T>() where TAofT : A<T>, new()
{
return new TAofT():
}
public static void Main()
{
A<string> first = FactoryMethod<A<string>, string>();
A<int> second = FactoryMethod<B<int>, int>();
A<D> third = FactoryMethod<C, D>();
}
But the factory method then seems redundant, since you could just do:
public static void Main()
{
A<string> first = new A<string>();
A<int> second = new B<int>();
A<D> third = new C();
}
UPDATE 2
Unless what you really want is this:
public abstract class AEnum<T, T3> where T3 : B<T>, new()
{
private static Func<A<T>> factoryMethod;
public static readonly Level1 = new AEnum<T>(()=>new A<T>());
public static readonly Level2 = new AEnum<T>(()=>new B<T>());
public static readonly Level3 = new AEnum<T>(()=>new T3());
protected AEnum(Func<A<T>> factoryMethod) { this.factoryMethod = factoryMethod; }
public A<T> New() { return this.factoryMethod(); }
}
used like this:
public class DEnum : AEnum<D, C>
{
}
with:
public static void Main()
{
A<D> first = DEnum.Level1.New();
A<D> second = DEnum.Level2.New();
A<D> third = DEnum.Level3.New();
}
But then you could not mix enum types, since the above is type constrained to D
.
Or you could do:
public class OpenAEnum<T, T3> : AEnum<T, T3> where T3 : B<T3>
{
}
public class CInt : B<int> {}
public class Cstring : B<string> {}
with:
public static void Main()
{
A<string> first = OpenAEnum<string, CString>.Level1.New();
A<int> second = OpenAEnum<int, CInt>.Level2.New();
A<D> third = OpenAEnum<D, C>.Level3.New();
}
What is it that you are trying to do?