title: Generic type conversion in generic method
body: <p>I have the following classes (some of them are in the PRISM framework and cannot be changed):</p>
  
  <pre><code>public abstract class NetworkEventBase&lt;T&gt; : CompositePresentationEvent&lt;T&gt; where T : NetworkEventPayload { }
  public class NetworkEventPayload { }
  public class TestEvent : NetworkEventBase&lt;TestPayload&gt; { }
  public class TestPayload : NetworkEventPayload { }
  
  // the following classes are PRISM classes:
  public class CompositePresentationEvent&lt;TPayload&gt; : EventBase { }
  public abstract class EventBase { }
  </code></pre>
  
  <p>Now I need to convert an instance of TestEvent to its base class NetworkEventBase inside a decorator for IEventAggregator. IEventAggregator looks like:</p>
  
  <pre><code>public interface IEventAggregator
  {
      TEventType GetEvent&lt;TEventType&gt;() where TEventType : EventBase, new();
  }
  </code></pre>
  
  <p>Now in my decorator I try to convert like this:</p>
  
  <pre><code>public class MessageBusAdapterInjectorDecorator : IEventAggregator {
      ...
  
      public TEventType GetEvent&lt;TEventType&gt;() where TEventType : EventBase, new()
      {
          var aggregatedEvent = this.eventAggregator.GetEvent&lt;TEventType&gt;();
          var networkEvent = aggregatedEvent as NetworkEventBase&lt;NetworkEventPayload&gt;;
  
          if (networkEvent != null)
          {
              networkEvent.MessageBusAdapter = this.messageBusAdapter;
          }
  
          return aggregatedEvent;
      }
  }
  </code></pre>
  
  <p>However, networkEvent is always null, even when the runtime type of aggregatedEvent is TestEvent.</p>