Compile-time information lost with conversion to object, cannot cast to generic interface<T>
<p>I have a class implementing a concrete typed version of a generic interface. I have found that if I pass an object in to my function (even though it might be the correct object) at compile time it is still considered an object and thus failing at runtime with the error:</p>

<p><code>Unable to cast object of type 'TestEventHandler' to type 'IDomainEventHandler '1[System.Object]'.</code></p>

<p>I am deserializing messages from a bus (which products objects, which should have an associated <code>DomainEventHandler&lt;of_deserialized_type&gt;</code> associated with the message.</p>

<p>In summary, the problem I believe is <code>IDomainEventHandler&lt;T&gt;</code> not casting from <code>`IDomainEventHandler&lt;object&gt;</code>, I would appreciate guidance on how best to solve this issue and still maintain the generic <code>IDomainEventHandler&lt;T&gt;</code> interface even with objects being passed in to <code>Publish()</code>.</p>

<pre><code>[TestClass]
public class InternalMessageHandlerTests
{
    class TestEvent
    {
    }

    class TestEventHandler : IDomainEventHandler&lt;TestEvent&gt;
    {
        public void HandleEvent(TestEvent domainEvent)
        {
        }
    }

    [TestMethod]
    public void Test()
    {
        TestEvent testEvent = new TestEvent();
        object testEventAsObject = testEvent; // compile time type information lost 
        Publish(testEvent); // this is OK :)
        Publish(testEventAsObject); // this fails :(

    }

    public void Publish&lt;T&gt;(T eventToPublish) where T : class
    {
        var handlerInstance = new TestEventHandler();
        IDomainEventHandler&lt;T&gt; converted = (IDomainEventHandler&lt;T&gt;)handlerInstance;
        converted.HandleEvent(eventToPublish);
    }
}
</code></pre>
