Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert Class<T> Where T : IMySampleInterface to Class<IMySampleInterface >
    primarykey
    data
    text
    <p>Is it possible to convert <code>Class&lt;T&gt; Where T : IMySampleInterface</code> to <code>Class&lt;IMySampleInterface&gt;</code></p> <p>Example:</p> <pre><code>public abstract class AbstractCommunication: ICommunication { private ICommunicationCallback&lt;ICommunication&gt; callback = null; public void registerCommunicationCallback&lt;T&gt;(ICommunicationCallback&lt;T&gt; callback) where T : ICommunication { this.callback = (ICommunicationCallback&lt;ICommunication&gt;)callback; //&lt;--DOESNT WORK } } </code></pre> <p>In my Example the following exception occurs: <code>System.InvalidCastException</code></p> <p>Edit:</p> <pre><code>public interface ICommunicationCallback&lt;T&gt; where T : ICommunication { void onCommunicationCallback(T sender, String data); } </code></pre> <p>why do i use this way: If I have a base class that should implement for example two Callbacks than i could simply use the following:</p> <pre><code>public class TestClass : ICommunicationCallback&lt;TestClass1&gt;, ICommunicationCallback&lt;TestClass2&gt; </code></pre> <p>TestClass1:</p> <pre><code>public class TestClass1: AbstractCommunication { } </code></pre> <p>TestClass2:</p> <pre><code>public class TestClass2: AbstractCommunication { } </code></pre> <p>Edit: "if T is always an ICommunication, then why keep it generic? – Davin Tryon 20 mins ago" Im locking at this point</p> <p>Okay Without the generics i got the same error but different <code>System.InvalidCastException</code>: (That is why i used generics in the first place - i remember now)</p> <pre><code> public class DocumentTest : ICommunicationCallback&lt;GetDocumentData&gt; { public void callAsync() { CommunicationFactory comFactory = new CommunicationFactory(communicationServiceClient); GetDocumentData getDocumentData = (GetDocumentData)comFactory.createCommunicationObject(CommunicationFactory.ENTITY_TYPE.GET_DOCUMENT_DATA,(ICommunicationCallback&lt;ICommunication&gt; ) this); } } public interface ICommunicationCallback&lt;ICommunication&gt; { void onCommunicationCallback(ICommunication sender, String data); } </code></pre> <p>I Think using generics is the only solution in my case - see: "This feature works only for generic interfaces and delegates. If you implement a variant generic interface, the implementing class is still invariant. Classes and structs do not support variance in C# 4.0. So the following doesn’t compile: // List implements the covariant interface // IEnumerable. But classes are invariant. List list = new List(); // Compiler error here." (<a href="http://blogs.msdn.com/b/csharpfaq/archive/2010/02/16/covariance-and-contravariance-faq.aspx" rel="nofollow">http://blogs.msdn.com/b/csharpfaq/archive/2010/02/16/covariance-and-contravariance-faq.aspx</a>)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload