C# feature request: implement interfaces on anonymous types

前端 未结 10 1554
走了就别回头了
走了就别回头了 2021-02-19 06:57

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

using System;

class Program
{
    static void Main()
    {
        var f = new IFoo { 
              


        
10条回答
  •  耶瑟儿~
    2021-02-19 07:15

    It requires c# 4, but the opensource framework impromptu interface can fake this out of the box using DLR proxies internally. The performance is good although not as good as if the change you proposed existed.

    using ImpromptuInterface.Dynamic;
    

    ...

    var f = ImpromptuGet.Create(new{ 
                    Foo = "foo",
                    Print = ReturnVoid.Arguments(() => Console.WriteLine(Foo))
                });
    

提交回复
热议问题