Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# feature request: implement interfaces on anonymous types

I am wondering what it would take to make something like this work:

using System;

class Program
{
    static void Main()
    {
        var f = new IFoo { 
                    Foo = "foo",
                    Print = () => Console.WriteLine(Foo)
            };
    }
}

interface IFoo
{
    String Foo { get; set; }
    void Print();
}

The anonymous type created would look something like this:

internal sealed class <>f__AnonymousType0<<Foo>j__TPar> : IFoo
{
    readonly <Foo>j__TPar <Foo>i__Field;

    public <>f__AnonymousType0(<Foo>j__TPar Foo)
    {
        this.<Foo>i__Field = Foo;
    }

    public <Foo>j__TPar Foo
    {
        get { return this.<Foo>i__Field; }
    }

    public void Print()
    {
        Console.WriteLine(this.Foo);
    }
}

Is there any reason that the compiler would be unable to do something like this? Even for non-void methods or methods that take parameters the compiler should be able to infer the types from the interface declaration.

Disclaimer: While I do realize that this is not currently possible and it would make more sense to simply create a concrete class in this instance I am more interested in the theoretical aspects of this.

like image 366
Andrew Hare Avatar asked Feb 03 '09 20:02

Andrew Hare


1 Answers

There would be a few issues with overloaded members, indexers, and explicit interface implementations.

However, you could probably define the syntax in a way that allows you to resolve those problems.

Interestingly, you can get pretty close to what you want with C# 3.0 by writing a library. Basically, you could do this:

Create<IFoo>
(
    new
    {
        Foo = "foo",
        Print = (Action)(() => Console.WriteLine(Foo))
    }
);

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.

The declaration of "Create" would look like this:

T Create<T> (object o)
{
//...
}

It would then use Reflection.Emit to generate an interface implementation dynamically at runtime.

This syntax, however, does have problems with explicit interface implementations and overloaded members, that you couldn't resolve without changing the compiler.

An alternative would be to use a collection initializer rather than an anonymous type. That would look like this:

Create
{
    new Members<IFoo>
    {
        {"Print", ((IFoo @this)=>Console.WriteLine(Foo))},
        {"Foo", "foo"}
    }
}

That would enable you to:

  1. Handle explicit interface implementation by specifying something like "IEnumerable.Current" for the string parameter.
  2. Define Members.Add so that you don't need to specify the delegate type in the initializer.

You would need to do a few things to implement this:

  1. Writer a small parser for C# type names. This only requires ".", "[]", "<>",ID, and the primitive type names, so you could probably do that in a few hours
  2. Implement a cache so that you only generate a single class for each unique interface
  3. Implement the Reflection.Emit code gen. This would probably take about 2 days at the most.
like image 124
Scott Wisniewski Avatar answered Oct 24 '22 00:10

Scott Wisniewski