Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In this part:</p> <blockquote> <p>When I publish <code>Publish&lt;DataChanged&lt;B&gt;&gt;(new DataChanged&lt;B&gt;(new B())</code>, the <code>Subscriber</code> should be notified with <code>DataChanged&lt;A&gt;</code> where <code>.Data</code> is the <code>B</code>-instance.</p> </blockquote> <p>I may not have understood you - I can't see what <code>.Data</code> refers to, for example, and I can only guess at the relationship between B and A. Do you mean that B is derived from A?</p> <p>If so, C# 4 will not necessarily make such a thing happen automatically. The types <code>X&lt;A&gt;</code> and <code>X&lt;B&gt;</code> are not compatible at all by default. If <code>X</code> is an <code>interface</code> and the type parameter is marked as <code>out</code>, then <code>X&lt;B&gt;</code> can be assigned to a variable of type <code>X&lt;A&gt;</code>. But note that this is for interfaces only, not concrete types (there is a similar provision for delegates, but that's all).</p> <p><strong>Edit:</strong> </p> <p>So therefore what you want to do is simulate the way that <code>X&lt;B&gt;</code> can be assigned to a variable of type <code>X&lt;A&gt;</code> in C#/CLR 4.0, where X is an interface.</p> <p>Suppose X is:</p> <pre><code>interface X&lt;T&gt; { T Foo(int arg); // Note: T may only appear as an output, so this is illegal: // void Foo(T arg); } </code></pre> <p>You have an <code>X&lt;B&gt;</code>, you need an <code>X&lt;A&gt;</code>. You know that <code>B</code> is assignable to <code>A</code>. So you need the following adaptor:</p> <pre><code>class WrapX_A_B : X&lt;A&gt; { public X&lt;B&gt; Impl { get; set; } public A Foo(int arg) { return Impl.Foo(arg); } } </code></pre> <p>You just forward each method on to the real implementation.</p> <p>However, you would need such a wrapper class for every possible combination of generic outer classes and pairs of generic parameters related by inheritance. It would be a tedious, error-prone and never-complete task to write them all by hand and maintain a big lookup to pick the right one for a given situation.</p> <p>So now you're into code generation to manufacture the wrapper classes at runtime.</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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      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