C#的功能要求:实施匿名类型接口 [英] C# feature request: implement interfaces on anonymous types

查看:147
本文介绍了C#的功能要求:实施匿名类型接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道怎样才能做这样的事情的工作:

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);
    }
}

有什么理由,编译器将无法做这样的事情?即使对于有参数的编译器应该能够从接口声明推断类型的非void的方法或方法

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.

推荐答案

有将与重载成员,索引器和显式接口实现的几个问题。

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.

有趣的是,你可以通过编写一个库相当接近你想用C#3.0的东西。基本上,你可以这样做:

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)
{
//...
}

然后,它将使用Reflection.Emit的在运行时动态生成一个接口实现。

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"}
    }
}

这将使您:


  1. 通过指定类似IEnumerable.Current为字符串参数处理显式接口实现。

  2. 定义Members.Add让你不吨需要指定初始化委托类型

您需要做的几件事情来实现这一点:

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


  1. 作家小解析器C#类型名称。这只需要,[],<>,ID和原始类型的名称,所以你很可能做到这一点在几个小时

  2. 实施缓存所以你只为每一个独特的界面单班

  3. 实施Reflection.Emit的代码生成。这可能将需要大约2天在最。

这篇关于C#的功能要求:实施匿名类型接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆