通过通用的方法接口基类的实例 [英] Interface Base class instantiation via generic method
问题描述
我有如的缘故看起来是这样的接口:
I have an interface that for example's sake looks like this:
interface IFoo<TEnum> where TEnum : struct, IConvertible, IComparable, IFormattable
{
TEnum MyEnum { get; set; }
}
然后我有一个看起来像这样一个抽象基类:
I then 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; }
}
我再从像这样的基类继承的:
I then inherit from the base class like so:
class MyFoo : FooBase<MyFoo.MyFooEnum>
{
public enum MyFooEnum
{
Foo1,
Foo2,
}
}
我如何实例化一个 MyFoo
从一个普通的方法类型参数 FooBase
?
How can I instantiate a MyFoo
from a generic method with type parameter FooBase
?
我是pretty的热切的是这样的:
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;
}
}
我的问题是,它希望键入 FooBase
参数,但在现实中,我真的不关心什么参数,因为我的 MyFoo
有那些类型的参数了。
The problem I have is that it wants type parameters for FooBase
, but in reality I don't really care what the parameters are since my MyFoo
has those type parameters already.
推荐答案
您可以从一个公共基础继承你的泛型类:
You can inherit your generic 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;
}
但是,你将无法与 FooBase
的泛型约束访问 MyEnum
属性。 (而且你怎么可以,而不必指定的类型?)
But you won't be able to access the MyEnum
property with a generic constraint of FooBase
. (And how could you, without having specified the type?)
这,或者您需要另一种类型的参数添加到的getFoo
:
That, or you need to add another type parameter to 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;
}
更新:另一件事我可以指出的是,如果你发现你需要调用这个的getFoo
的方法有很多,那么如果你把它的类的实例,而不是一个静态类的,你可以把总是指定它的方法之一类型参数或两者成的类来代替。这可以使一些codeA少冗长,但实际上只有当你调用此方法很多。例如:
UPDATE: Another thing I could point out is that if you find that you need to call this GetFoo
method a lot, then if you put it in an instance class instead of a static class, you can push one or both of the type arguments up into the class instead of always specifying it in the method. This can make some code a little less verbose, but really only if you call this method a lot. Ex:
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>();
这篇关于通过通用的方法接口基类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!