다음 클래스가 있습니다 (일부는 PRISM 프레임 워크에 있으며 변경할 수 없음).
public abstract class NetworkEventBase<T> : CompositePresentationEvent<T> where T : NetworkEventPayload { }
public class NetworkEventPayload { }
public class TestEvent : NetworkEventBase<TestPayload> { }
public class TestPayload : NetworkEventPayload { }
// the following classes are PRISM classes:
public class CompositePresentationEvent<TPayload> : EventBase { }
public abstract class EventBase { }
이제 TestEvent 인스턴스를 IEventAggregator의 데코레이터 내에서 기본 클래스 NetworkEventBase로 변환해야합니다. IEventAggregator는 다음과 같습니다.
public interface IEventAggregator
{
TEventType GetEvent<TEventType>() where TEventType : EventBase, new();
}
이제 내 데코레이터에서 다음과 같이 변환하려고합니다.
public class MessageBusAdapterInjectorDecorator : IEventAggregator {
...
public TEventType GetEvent<TEventType>() where TEventType : EventBase, new()
{
var aggregatedEvent = this.eventAggregator.GetEvent<TEventType>();
var networkEvent = aggregatedEvent as NetworkEventBase<NetworkEventPayload>;
if (networkEvent != null)
{
networkEvent.MessageBusAdapter = this.messageBusAdapter;
}
return aggregatedEvent;
}
}
그러나 aggregatedEvent의 런타임 유형이 TestEvent 인 경우에도 networkEvent는 항상 null입니다.
호출 된 클래스NetworkEventBase<T>
가에서 공변이 되기를 희망하는 것 같습니다 T
. 그러나 제네릭 클래스는 C #에서 공변 할 수 없습니다 (제네릭 인터페이스는 가능).
이 문제에 대한 다른 스레드를 참조하십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다