当编译时类型未知时使用泛型
平台:C# 2.0 WinForms
我有一个工厂类,它根据我发送的类型提供特定数据映射器的实例化,代码如下:
public static IDataMapper<T> GetMapper<T>() where T: IDto
{
Type mapperType = MapperLocator.GetMapper(typeof(T));
return (IDataMapper<T>)mapperType.Assembly.CreateInstance(mapperType.FullName);
}
我正在使用 DynamicProxy2 拦截对我的 DTO 对象的方法调用。 在我的拦截方法中,我尝试使用 Inspiration.TargetType 中的类型来调用上述工厂。 但是,返回时出现异常:
找不到类型或命名空间名称“调用”。
显然,这是因为对泛型方法的任何调用都需要根据我在编译时的理解明确了解类型。 显然,在这种情况下我不能这样做,而且我绝对不会在所有 DTO 对象上执行 switch 语句。
那么,你们能提出一个策略或指出我做错了什么吗? 我试图使其尽可能通用,以便它可以适合我的所有对象和任何新对象以及代码可移植到其他项目。
提前致谢!
Platform: C# 2.0 WinForms
I have a factory class that provides an instantiation of a particular data mapper depending on the type that I send it, the code is as such:
public static IDataMapper<T> GetMapper<T>() where T: IDto
{
Type mapperType = MapperLocator.GetMapper(typeof(T));
return (IDataMapper<T>)mapperType.Assembly.CreateInstance(mapperType.FullName);
}
I am using DynamicProxy2 to intercept method calls to my DTO objects. In my intercept method I am trying to call the above factory using the type from Invocation.TargetType. However this comes back with the exception:
The type or namespace name 'invocation' could not be found.
Obviously this is because any calls to a generic method need to know the type explicitly from what I understand at compile time. Obviously I can't do that in this case and I definitely am not going to do a switch statement across all of my DTO objects.
So, can you guys suggest a strategy or point out what I am doing wrong? I am trying to make this as generic as possible so that it could fit across all of my objects and any new ones as well as code portability to other projects.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 DTO 不够熟悉,不知道这里是否有足够的信息来提供完整的解决方案。 也就是说,另一个答案 大部分是正确的; C# 编译器在编译时需要类型信息。
然而,有一种方法可以解决这个问题:反射。 System.Reflection(特别是 < a href="http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo%28VS.80%29.aspx" rel="nofollow noreferrer">MethodInfo 在你的情况下我think)将允许您编写一个完全通用的解决方案。
如果我正确理解了这个问题,您要做的就是获取该工厂函数的 MethodInfo,用 MakeGenericMethod,然后调用它。
I'm not familiar enough with DTO to know whether or not there's enough information here for a full solution. That said, another answer is mostly correct; the C# compiler needs type information at compile time.
There is, however, a way around this: reflection. System.Reflection (particularly MethodInfo in your case I think) will allow you to write a fully generic solution.
If I've understood the question correctly, what you'd do is get the MethodInfo for that factory function, substitute in the type with MakeGenericMethod, and then Invoke that.
不幸的是,没有办法解决这个问题; .NET C# 编译器需要在编译时知道泛型的类型; 你必须找到另一种方法。
您也许可以使用装箱/拆箱和非泛型,但我不太了解您将如何实现这一点。 或者,您可以使用
System.Reflection
。 同样,我不知道该怎么做。There is no way out of this, unfortunately; the .NET C# compiler needs to know the Type at compile time for generics; you'll have to find another method.
You could perhaps use boxing/unboxing and non-generics, but I do not know much about how you would implement this. Alternatively, you could use
System.Reflection
. Again, I do not know how to do this.