在运行时生成通用类型
我想知道是否可以使用一个变量的类型设置为另一个泛型变量的类型?
例如,假设我有这个代码:
public class Foo: IBar<ushort>
{
public FooBar()
{
Value = 0;
}
public ushort Value { get; private set; }
}
我也有这个类:
public class FooDTO<TType> : IBar<TType>
{
public TType Value { get; private set; }
}
在这些示例中,在 IBar 的接口中具有属性
TType Value;
然后在我的代码中我有这个 这
var myFoo = new Foo();
var fooDataType = myFoo.Value.GetType();
//I know this line of code does not compile, but this is what I am looking to be able to do
var myFooDTO= new FooDTO<fooDataType>();
是我正在寻找的可能吗?对于高使用率的代码来说会不会太慢(因为使用了反射。
I am wondering if it is possible to use the type of one variable to set as the type of another generic variable?
For example, say I have this code:
public class Foo: IBar<ushort>
{
public FooBar()
{
Value = 0;
}
public ushort Value { get; private set; }
}
I also have this class:
public class FooDTO<TType> : IBar<TType>
{
public TType Value { get; private set; }
}
In these examples, in the interface for IBar has the property
TType Value;
Then in my code I have this
var myFoo = new Foo();
var fooDataType = myFoo.Value.GetType();
//I know this line of code does not compile, but this is what I am looking to be able to do
var myFooDTO= new FooDTO<fooDataType>();
Is what I am looking for possible? Would it be too slow for high use code (because of using reflection.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过反射,使用 Type.MakeGenericType。
由于反射,这会产生一些开销,因此您需要对其进行分析,看看这对您来说是否会成为问题。
You can do this via Reflection, by using Type.MakeGenericType.
This will have some overhead due to reflection, so you'd need to profile it to see if that will be an issue for you.
为什么不使用方法类型推断:
Why not use Method type inference:
总是当我在一个句子中读到“通用”和“运行时”时,我总是想到“糟糕的设计”或“不理解通用的含义”。可能两者皆有。
泛型参数是类型的组成部分。所以说“在运行时生成通用类型”与“在运行时生成 Foo 类”相同。您要么寻找反思,要么改变算法的设计。
在这种情况下,
var
关键字也不会为您提供帮助。忘记它吧。Always when I read "generic" and "runtime" in one sentence, I always thing "bad design" or "doesnt understant what generic means". Possibly both.
Generic parameter is integral part of the type. So saying "Generate Generic Type At Runtime" is same as "Generate Foo class at runtime". You are either looking for reflection or change design of your algorithm.
Also
var
keyword is not going to help you in this case. Forget about it.您正在寻找编译时反射,这是 C# 所没有的功能。因此,如果您正在寻求性能优化,那么解决方案比问题本身更糟糕。
D 确实有这个功能;您可以轻松地
在 D 中编写甚至更复杂的表达式,并且所有这些都在编译时进行评估。
You're looking for compile-time reflection, a feature that C# doesn't have. So if you're looking for performance optimizations, the solutions are worse than the problem.
D does have this feature, though; you can easily write
or even much more complicated expressions in D, and it's all evaluated at compile-time.
您想要的核心是:
但是,您会注意到这是反射,并且几乎将您与
对象
联系在一起。通常的解决方法是访问 API 的非通用版本,以便您可以使用object
- 例如(添加非通用IBar< /code>):
当然,您可以将运行时/泛型移至更高的位置 - 也许移至泛型方法中;那么泛型方法中的所有内容都可以使用
T
,并且您可以使用MakeGenericMethod
在仅在以下位置已知的特定T
上下文中执行该方法运行时。The core of what you want is:
however, you'll notice that this is reflection, and pretty much ties you to
object
. The usual workaround to this is to have access to a non-generic version of the API, so that you can work withobject
- for example (with the addition of a non-genericIBar
):You can of course move the runtime/generics hit higher up - perhaps into a generic method; then everything in the generic method can use
T
, and you can useMakeGenericMethod
to execute that method in the context of a particularT
known only at runtime.