Interface Creating a base class through a common method

I have an interface that, for example, looks like this:

interface IFoo<TEnum> where TEnum : struct, IConvertible, IComparable, IFormattable { TEnum MyEnum { get; set; } } 

Then I have an abstract base class that looks like this:

 abstract class FooBase<TEnum> : IFoo<TEnum> where TEnum : struct, IConvertible, IFormattable, IComparable { public TEnum MyEnum { get; set; } } 

Then I inherit the base class as follows:

 class MyFoo : FooBase<MyFoo.MyFooEnum> { public enum MyFooEnum { Foo1, Foo2, } } 

How can I create an instance of MyFoo from a generic method with a parameter of type FooBase ?

I'm pretty much looking for something like this:

 static class FooMethods { public static TFooClass GetFoo<TFooClass>() where TFooClass : FooBase, new() { TFooClass fooclass = new TFooClass(); return fooclass; } } 

The problem is that he wants type parameters for FooBase , but I really don't care what parameters are, since my MyFoo already has these type parameters.

+6
source share
2 answers

You can inherit your common class from a common base:

 abstract class FooBase { } abstract class FooBase<TEnum> : FooBase, IFoo<TEnum> where TEnum : struct, IConvertible, IFormattable, IComparable { public TEnum MyEnum { get; set; } } public static TFooClass GetFoo<TFooClass>() where TFooClass : FooBase, new() { TFooClass fooclass = new TFooClass(); return fooclass; } 

But you will not be able to access the MyEnum property with a common FooBase restriction. (And how could you, without specifying the type?)

This, or you need to add another type parameter in GetFoo :

 abstract class FooBase<TEnum> : IFoo<TEnum> where TEnum : struct, IConvertible, IFormattable, IComparable { public TEnum MyEnum { get; set; } } public static TFooClass GetFoo<TFooClass, TEnum>() where TFooClass : FooBase<TEnum>, new() where TEnum : struct, IConvertible, IFormattable, IComparable { TFooClass fooclass = new TFooClass(); return fooclass; } 

UPDATE: Another thing that I could point out is that if you find that you need to call this method GetFoo many times, then if you put it in an instance class instead of a static class, you can click one or both type arguments to a class instead of always specifying it in a method. This may make some code a little less verbose, but in fact, only if you call this method a lot. Example:

 public sealed FooFactory<TEnum> where TEnum : struct, IConvertible, IFormattable, IComparable { public static TFooClass GetFoo<TFooClass>() where TFooClass : FooBase<TEnum>, new() { TFooClass fooclass = new TFooClass(); return fooclass; } } ... var factory = new FooFactory<SomeEnum>(); var foo1 = factory.GetFoo<SomeFooClass1>(); var foo2 = factory.GetFoo<SomeFooClass2>(); // or the other way: var factory = new FooFactory<SomeFooClass>(); var foo1 = factory.GetFoo<SomeEnum1>(); var foo2 = factory.GetFoo<SomeEnum2>(); 
+3
source

You can do the following:

 abstract class FooBase { } abstract class FooBase<TEnum> : FooBase, IFoo<TEnum> where TEnum : struct, IConvertible, IFormattable, IComparable { public TEnum MyEnum { get; set; } } static class FooMethods { public static TFooClass GetFoo<TFooClass>() where TFooClass : FooBase, new() { TFooClass fooclass = new TFooClass(); return fooclass; } } 
0
source

All Articles