Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There would be a few issues with overloaded members, indexers, and explicit interface implementations.</p> <p>However, you could probably define the syntax in a way that allows you to resolve those problems.</p> <p>Interestingly, you can get pretty close to what you want with C# 3.0 by writing a library. Basically, you could do this:</p> <pre><code>Create&lt;IFoo&gt; ( new { Foo = "foo", Print = (Action)(() =&gt; Console.WriteLine(Foo)) } ); </code></pre> <p>Which is pretty close to what you want. The primary differences are a call to "Create" instead of the "new" keyword and the fact that you need to specify a delegate type.</p> <p>The declaration of "Create" would look like this:</p> <pre><code>T Create&lt;T&gt; (object o) { //... } </code></pre> <p>It would then use Reflection.Emit to generate an interface implementation dynamically at runtime.</p> <p>This syntax, however, does have problems with explicit interface implementations and overloaded members, that you couldn't resolve without changing the compiler.</p> <p>An alternative would be to use a collection initializer rather than an anonymous type. That would look like this:</p> <pre><code>Create { new Members&lt;IFoo&gt; { {"Print", ((IFoo @this)=&gt;Console.WriteLine(Foo))}, {"Foo", "foo"} } } </code></pre> <p>That would enable you to:</p> <ol> <li>Handle explicit interface implementation by specifying something like "IEnumerable.Current" for the string parameter.</li> <li>Define Members.Add so that you don't need to specify the delegate type in the initializer.</li> </ol> <p>You would need to do a few things to implement this:</p> <ol> <li>Writer a small parser for C# type names. This only requires ".", "[]", "&lt;>",ID, and the primitive type names, so you could probably do that in a few hours</li> <li>Implement a cache so that you only generate a single class for each unique interface</li> <li>Implement the Reflection.Emit code gen. This would probably take about 2 days at the most.</li> </ol>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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