简体   繁体   English

写c#实现抽象类内联?

[英]write c# implementation of abstract class inline?

I found a great example of code written in java for the use of a timer / timertask classes... 我找到了一个很好的用java编写的代码示例,用于使用timer / timertask类...

I am currently turning this into c# for a 'Mono for Android' implementation, but having trouble with converting the inline implementation of the abstract class TimerTask 我目前正在将其转换为c#用于'Mono for Android'实现,但是在转换抽象类TimerTask的内联实现方面遇到了麻烦

// Here's the original java code

private void resetMapChangeTimer()    
{        
mChangeDelayTimer.schedule(
new TimerTask()        
    {            
      @Override            
      public void run()            
      {                
        // Some code to run           
      }        
    }, longTenSeconds);    
}

When I implement this in c#, I would like to create an inherited class from TimerTask (abstract class) and the run method etc in-line without having to create a seperate class (extending TimerTask) as the java code above does. 当我在c#中实现它时,我想在线创建一个继承自TimerTask(抽象类)和run方法等的类,而不必像上面的java代码那样创建一个单独的类(扩展TimerTask)。

Can anyone advise me on the c# syntax to create this abstract class inline instead of creating a seperate class just to inherit and implement TimerTask? 任何人都可以建议我使用c#语法来创建这个抽象类内联而不是创建一个单独的类来继承和实现TimerTask吗?

C# doesn't support inline classes as in Java. C#不支持Java中的内联类。 You could define anonymous methods but not entire classes. 您可以定义匿名方法,但不能定义整个类。 So you will have to define a separate class that implements the abstract class. 因此,您必须定义一个实现抽象类的单独类。

As Darin says, an anonymous function is the way to go here. 正如达林所说,匿名功能是前往这里的方式。 You're only trying to specify one bit of behaviour, so just make the method accept an Action and then the caller can use an anonymous function (either an anonymous method or a lambda expression). 您只是尝试指定一点行为,因此只需让方法接受一个Action ,然后调用者就可以使用匿名函数(匿名方法或lambda表达式)。 The calling code would look like this: 调用代码如下所示:

changeDelayTimer.Schedule(() => {
    // Code to run
}, TimeSpan.FromSeconds(10));

Or if you had a lot of code, you'd want to use a method, with a method group conversion: 或者如果你有很多代码,你想要使用方法组转换:

changeDelayTimer.Schedule(MethodContainingCode, TimeSpan.FromSeconds(10));

In Java I rarely find I want to do more than override a single method within an anonymous inner class anyway - so anonymous functions in C# almost always work just as well here. 在Java中,我很少发现我想做的事情不仅仅是覆盖匿名内部类中的单个方法 - 所以C#中的匿名函数几乎总是在这里工作。

When porting code from Java to C#, it's important not to try to preserve Java idioms - preserve the same goals , but porting to a .NET idiom, which in this case is using a delegate to represent a single piece of behaviour in an abstract way. 将代码从Java移植到C#时,重要的是不要试图保留Java惯用语 - 保留相同的目标 ,而是移植到.NET习惯用法,在这种情况下,使用委托以抽象方式表示单个行为。

If you want to use/extend existing Java classes you could implement a generic helper class: 如果要使用/扩展现有Java类,可以实现通用帮助程序类:

class MyTimerTask: TimerTask
{
    private Action action;

    public MyTimerTask(Action action)
    {
        this.action = action;
    }

    public override void Run()
    {
        if(action != null)
            action();
    }
}

You can then use anonymous methods: 然后,您可以使用匿名方法:

mChangeDelayTimer.Schedule(new MyTimerTask(() =>
{
    //Do something

}), longTenSeconds);

To have the best C# experience in your project, you could further add an extension method. 要在项目中获得最佳C#体验,您可以进一步添加扩展方法。 Create a public static class somewhere in your project: 在项目的某处创建一个公共静态类:

public static class TimerHelper
{
    public static void Schedule(this DelayTimer /*or whatever your timer class is*/ timer, Action action, int delay)
    {
        if(timer == null)
            throw new ArgumentNullException("timer"); //null check needed because this method is not really an instance method

        timer.Schedule(new MyTimerTask(action), delay);
    }
}

Then you can use the following code: (The TimerHelper class must be "visible" to the executing context for the extension method to show up, therefore it is public) 然后你可以使用下面的代码:( TimerHelper类必须对执行上下文“可见”,才能显示扩展方法,因此它是公共的)

mChangeDelayTimer.Schedule(() =>
{
    //Do something

}, longTenSeconds);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM