转换为类型未知的泛型 [英] Cast to generic where type is unknown
问题描述
以下代码是我所拥有的简化版本:
The following code is a simplified version what I have:
public class Message
{
public int Prop1 { get; set; }
public string Prop2 { get; set; }
}
public class ExtendedMessage<TExtension> : Message
{
public TExtension Extension { get; set; }
}
public class Processor<T> where T : Message
{
public void Process(T message)
{
}
}
我将有许多继承自Message或ExtendedMessage的类型.我希望能够使用Processor处理从ExtendedMessage<>以及Message继承的内容.但是,这涉及对具有扩展属性的人进行操作.
I will have many types that inherit from either Message or ExtendedMessage. I would like to be able to use Processor to process those that inherit from ExtendedMessage<> as well as Message. However that involves manipulating the Extension property for those that have it.
为此,似乎我需要将message参数转换为ExtendedMessage<>的处理方法,如果它属于该类型.我尝试使用以下方法做到这一点:
In order to do that it seems I would need to cast the message parameter to the process method to ExtendedMessage<>, if it is of that type. I have tried to do that using the following:
if (IsInstanceOfGenericType(typeof(JsonModel<>), model))
{
var dataType = message.GetType().GetGenericArguments()[0];
Type type = typeof(ExtendedMessage<>).MakeGenericType(dataType);
var extendedMsg = Activator.CreateInstance(type);
//Processing using extendedMsg.Extension
}
IsInstanceOfGenericType来自以下答案:测试是否对象在C#中是通用类型
Where IsInstanceOfGenericType is from this answer: Testing if object is of generic type in C#
但是,该方法不起作用,显然该属性不可用.是否可以用相同的方法处理两种类型?我首先要以正确的方式解决这个问题吗?如果可能,我想避免扩展Processor来创建单独的ExtendedMessageProcessor.谢谢.
However that does not work, obviously the property is not available. Is it possible to process both types in the same method? Am I going about this the right way in the first place? I would like to avoid extending Processor to create a separate ExtendedMessageProcessor if possible. Thanks.
推荐答案
简单但错误的答案是使用动力学:
The easy-but-wrong answer would be to use dynamics:
var extension = ((dynamic)message).Extension
一个更好的答案是让您的扩展从基础继承
A better answer would be to have your extensions inherit from a base
public class ExtendedMessage : Message
{
public ExtensionBase Extension { get; set; }
}
public abstract class ExtensionBase
{
}
...
var extendedMessage = message as ExtendedMessage;
if (extendedMessage != null)
{
//process
}
如果这样的事情是不可能的,并且您被反射所困扰,则您不想创建该消息的新实例.您已经有该消息,并且该消息具有所需的属性.您应该使用 Type.GetProperties()
和 Type.GetMethods()
.
If something like this isn't possible and you're stuck with reflection, you don't want to create a new instance of the message. You already have the message and it has the properties on it you need. You should be using Type.GetProperties()
and Type.GetMethods()
.
这篇关于转换为类型未知的泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!