如何在运行时将基类转换为派生类。我想要做的是:我需要一个系统来保存订阅,其中某种类型的消息有一个指定的订阅者。当收到一条消息时,它会双向转发给这个系统,它会查找是否有任何订阅者订阅这种消息,如果有,则将消息转发给订阅者。我有一个名为 Response 的抽象基类,然后是一个抽象层,例如。AFResponse :响应,然后是实际(具体)实现,例如。响应(抽象):AFResponse(抽象):AFIncommingMessage(具体)。
收到数据包后,它会转到 MessageBuilder,它会解析消息类型并构建它,如下所示:
public static T Build<T>(Packet packet) where T : Response
{
Type resolvedType;
if (!dependencyMap.TryGetValue(packet.MessageId, out resolvedType))
{
var str = String.Format("Could not resolve message. Message info: CMD0: {0}, CMD1: {1}, MessageID: {2}",
packet.Cmd0, packet.Cmd1, packet.MessageId);
Debug.WriteLine(str);
throw new ResolutionFailedException(str);
}
ConstructorInfo firstConstructor = resolvedType.GetConstructors().First();
return (T) firstConstructor.Invoke(new object[] {packet});
}
然后如果消息是异步的,它会被转发到 MessageBinder,它持有消息类型的订阅。
private void OnAsyncResponseReceived(Response response)
{
messageBinder.Forward(response);
}
MessageBinder 类实现如下:
public class MessageBinder
{
private class Subscriber<T> : IEquatable<Subscriber<T>> where T : Response
{
...
}
private readonly Dictionary<Type, IEnumerable> bindings;
public MessageBinder()
{
this.bindings = new Dictionary<Type, IEnumerable>();
}
public void Bind<TResponse>(ushort shortAddress, Action<ZigbeeAsyncResponse<TResponse>> callback)
where TResponse : Response
{
HashSet<Subscriber<TResponse>> subscribers = this.GetSubscribers<TResponse>();
if (subscribers != null)
{
subscribers.Add(new Subscriber<TResponse>(shortAddress, callback));
}
else
{
var subscriber = new Subscriber<TResponse>(shortAddress, callback);
this.bindings.Add(typeof(TResponse), new HashSet<Subscriber<TResponse>> { subscriber });
}
}
public void Forward<TResponse>(TResponse response)
where TResponse : Response
{
var subscribers = this.GetSubscribers<TResponse>();
if (subscribers != null)
{
Subscriber<TResponse> subscriber;
var afResponse = response as AFResponse;
if (afResponse != null)
{
subscriber = subscribers.SingleOrDefault(s => s.ShortAddress == afResponse.ShortAddress);
}
else
{
subscriber = subscribers.FirstOrDefault();
}
if (subscriber != null)
{
Debug.WriteLine("Forwarding received async response of type " + response.GetType().Name);
subscriber.Forward(response);
}
}
}
private HashSet<Subscriber<TResponse>> GetSubscribers<TResponse>() where TResponse : Response
{
IEnumerable subscribers;
this.bindings.TryGetValue(typeof(TResponse), out subscribers);
return (HashSet<Subscriber<TResponse>>)subscribers;
}
}
当调用 MessageBinder 类的 GetSubscribers() 方法时会出现问题,因为 TResponse 的类型是基类的类型,即 Response,因此找不到订阅者。所以我认为我需要做的是以某种方式将消息的实际类型传递给 Forward 方法,这样该类型将是实际的响应类型。
我改变了这样的方法:
private void OnAsyncResponseReceived(Response response)
{
messageBinder.Forward((dynamic)response);
}
它确实有效,但不知何故我觉得有更好的(?)方法可以做到这一点......或者这是实际且唯一的解决方案?也许我应该改变整体设计,我不知道......我第一次面临这种类型的问题,这就是我想出的。
我愿意接受建议、批评,当然还有最好的解决方案 :) 我很好奇,如果它与我的解决方案不同且更有效,你将如何实现它。感谢您的任何帮助!