本文介绍了匿名接口实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从来就读线程匿名实现一个接口。但我想,如果这个使用.NET 2.0(NO LINQ)使用委托或任何类似的做法也是可以的。

I´ve read 'C# anonymously implement interface (or abstract class)' thread for implementing an interface anonymously. But I wondered if this is also possible using .NET 2.0 (NO LINQ) using delegates or any similar approach. I know from JAVA the following possible:

MyClass m = MyMethod(new MyInterface() {
    @override
    public void doSomething() {...}
}

(我希望我记得很清楚,是一个很久以前,我用JAVA,但我想这是类似的东西),当一个方法需要一个接口的实例,被称为只有一次,所以没有必要创建一个新的类,这可能是有益的这种单一的方法。

(I hope I remember well, is a time ago that I used JAVA, but I suppose it was something similar). This might be helpful whenever a method needs an instance of an interface and is called only once so there is no need to create a new class for this single approach.

推荐答案

.NET 2.0也支持匿名委托,它只是语法更多的是一个有点冗长相比,lambda表达式和类型推断没有工作,而且有C#2.0中没有扩展方法(虽然你能够使用C#3.0和编译针对.NET 2.0),这是LINQ的基础,能够在接口上运行。

.NET 2.0 also supported anonymous delegates, it's just that the syntax was a bit more verbose compared to lambdas, and type inference didn't work. And there were no extension methods in C# 2.0 (although you were able to use C# 3.0 and compile against .NET 2.0), which are the basis of LINQ and being able to operate on interfaces.

比较:


  • .NET 2.0:委托( int i)以{回报(I< 5);}

  • .NET 3.5: I => I< 5

  • .NET 2.0: delegate(int i) { return (i < 5); }
  • .NET 3.5: i => i < 5

NET 2.0也缺乏共同的泛型委托签名(函数功能动作),但你也可以轻松地自己(对所有的参数组合,你喜欢)定义它们:

.NET 2.0 also lacks common generic delegate signatures (Func and Action), but you can also easily define them yourself (for all combinations of parameters you like):

public delegate void Action<T>(T item);
public delegate Tresult Func<T, Tresult>(T item);



所以,无论你的方式挂答案用来模仿匿名接口可以使用.NET 2.0的代表表示,在加入冗长的代价。让你问自己:这是真的的短写

[更新]

如果您的接口是一个方法的接口,如:

If your interface is a single method interface, like:

interface IFoo
{
    string Bar(int value);
}

class SomeOtherClass
{
    void DoSomething(IFoo foo);
}



,那么你可能会得到完全摆脱它,只是使用委托来代替:

then you might get rid of it entirely and simply use a delegate instead:

class SomeOtherClass
{
    void DoSomething(Func<int, string> bar);
}

new SomeOtherClass().DoSomething(delegate(int i) { return i.ToString(); });

如果你有,你要能够实现在许多不同的地方内嵌许多方法的接口,你可以使用这样的:

If you have an interface with many methods that you want to be able to implement inline in many different places, you can use something like this:

interface IFoo
{
    string GetSomething();
    void DoSomething(int value);
}

// conditional compile, only if .NET 2.0
#if NET_2_0
public delegate void Action<T>(T item);
public delegate Tresult Func<Tresult>();
#endif

class DelegatedFoo : IFoo
{
    private readonly Func<string> _get;
    private readonly Action<int> _do;

    public DelegatedFoo(Func<string> getStuff, Action<int> doStuff)
    {
        _get = getStuff;
        _do = doStuff;
    }

    #region IFoo members simply invoke private delegates

    public string GetSomething()
    { return _get(); }

    public void DoSomething(int value)
    { _do(value); }

    #endregion
}



这将让你通过委托给 DelegatedFoo 类在线:

var delegated = new DelegatedFoo(
    delegate() { return ""; }, // string GetSomething()
    delegate(int i) { }        // void DoSomething(int)
);

使用 .NET 4 的C#4.0的语法看起来会有点清洁由于lambda表达式的语法甜度和命名参数:

Using the C# 4.0 syntax it would look a bit cleaner due to syntactic sweetness of lambdas and named parameters:

var delegated = new DelegatedFoo(
    getStuff: () => "",
    doStuff:  i => { }
);

这篇关于匿名接口实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 03:04