.NET 泛型类实例 - 传递变量数据类型
正如标题所示,我试图将变量数据类型传递给模板类。像这样的事情:
frmExample = New LookupForm(Of Models.MyClass) 'Works fine
Dim SelectedType As Type = InstanceOfMyClass.GetType() 'Works fine
frmExample = New LookupForm(Of SelectedType) 'Ba-bow!
frmExample = New LookupForm(Of InstanceOfMyClass.GetType()) 'Ba-bow!
LookupForm<Models.MyClass> frmExample;
Type SelectedType = InstanceOfMyClass.GetType();
frmExample = new LookupForm<SelectedType.GetType()>(); //Ba-bow
frmExample = new LookupForm<(Type)SelectedType>(); //Ba-bow
我假设这与编译时处理的模板有关,但即使我在那里偏离了目标,它也无法解决我的问题。我也找不到任何有关使用反射来实例模板类的相关信息。
(如何)我可以在运行时创建动态类型存储库的实例吗?
As the title suggests, I'm tyring to pass a variable data type to a template class. Something like this:
frmExample = New LookupForm(Of Models.MyClass) 'Works fine
Dim SelectedType As Type = InstanceOfMyClass.GetType() 'Works fine
frmExample = New LookupForm(Of SelectedType) 'Ba-bow!
frmExample = New LookupForm(Of InstanceOfMyClass.GetType()) 'Ba-bow!
LookupForm<Models.MyClass> frmExample;
Type SelectedType = InstanceOfMyClass.GetType();
frmExample = new LookupForm<SelectedType.GetType()>(); //Ba-bow
frmExample = new LookupForm<(Type)SelectedType>(); //Ba-bow
I'm assuming it's something to do with the template being processed at compile time but even if I'm off the mark there, it wouldn't solve my problem anyway. I can't find any relevant information on using Reflection to instance template classes either.
(How) can I create an instance of a dynamically typed repository at runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C# 非常接近的示例位于此处我有一个问题:
转换为 VB.NET,将其更改为类型创建而不是方法调用,并为您使用示例名称:(
啊,出于某种原因,我以为您想要在 VB.NET 中使用它,但这里是一个 C# 示例)
A C# example of something pretty close is located here on a question I had:
Converting to VB.NET, changing it to type creation not method invocation and using your example names for you:
(Ah for some reason I thought you wanted it in VB.NET but here is a C# example)
使用
Type.MakeGenericType
。和 VB.NET 版本:)
Use
Type.MakeGenericType
.And VB.NET version :)
这听起来像是动态语言运行时(C# 中的“动态”类型)的候选者,但这需要您使用 .NET 4.0
This sounds like a candidate for the Dynamic Language Runtime, 'Dynamic' type in C#, but that would require you to use .NET 4.0
不幸的是,你不能不反思就做到这一点,即使如此,它也不是很友好。反射代码将如下所示:
当然,现在您不知道将对象转换为什么(假设
selectedType
是动态设置的),但您仍然可以通过反射调用其上的方法,或者您可以创建一个非通用接口将其强制转换为 &调用该方法。Unfortunately you can't do this without reflection and even then its not very friendly. The reflection code will looks something like this:
Of course now you don't know what to cast your object to (assuming
selectedType
was dynamically set), but you can still call the methods on it via reflection, or you could create a non-generic interface to cast it to & call the methods on that.