我可以施放一个动作< T>一个动作? [英] Can I cast an action<T> to an action?

查看:112
本文介绍了我可以施放一个动作< T>一个动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我必须举手并承认我犯了复制代码(匆忙)而不理解它。



我复制的代码是用于实现 System.Windows.Input.ICommand 接口。我找不到我复制的代码的来源,但现在回顾一下,我觉得它对我来说很奇怪。



这是关注的代码我。



Okay, I have to put up my hands and admit I am guilty of copying code (in a hurry) without understanding it.

The code I copied was for an implementation of the System.Windows.Input.ICommand interface. I can't find the source of the code I copied, but reviewing it now, I've decided it looks odd to me.

Here is the code that concerns me.

public class Command : ICommand
{
    public Command( Action<object> execute )
    {
        _execute = execute;
    }

	public Command( Action execute ) : this( (Action<object>)( o => execute() ) )
	{
	}

	private readonly Action<object> _execute = null;
}





它似乎正在进行构造函数链接以存储类型的参数在类型为 Action< object> 的变量中。它编译并且以下测试似乎表明它工作正常:





It appears to be doing constructor chaining in order to store a parameter of type Action in a variable of type Action<object>. It compiles and the following tests seem to indicate it is working fine:

[TestClass]
public class UTSupportCommand
{
    [TestMethod]
    public void Command_with_Action_parameter_executes()
    {
        Command command = new Command( ActionWithObjectParameter );
        command.Execute( "A" );
        Assert.IsTrue( _t1 == "A" );
    }

    [TestMethod]
    public void Command_with_no_parameter_executes()
    {
        Command command = new Command( ActionWithNoParameter );
        command.Execute( "X" );
        Assert.IsTrue( _t2 == "B" );
    }

    private void ActionWithObjectParameter( object s )
    {
        _t1 = (string)s;
    }

    private void ActionWithNoParameter()
    {
        _t2 = "B";
    }

    private string _t1 = "Z";
    private string _t2 = "Z";
}





让我感到困惑的是如何将参数传递给 Action ,不带参数。它会发生什么变化?



我对这段代码是否有任何问题以及它究竟是做什么表示赞赏。



亲切的祝福~Patrick



我的尝试:



我已经尝试过上面显示的测试,看起来运行正常。



What really puzzles me is how I can pass a parameter into the Action that takes no parameter. What happens to it?

I'd appreciate any comments on whether there is anything wrong with this code and what exactly it is doing.

Kind wishes ~ Patrick

What I have tried:

I have tried the tests shown above, which seem to run fine.

推荐答案

这很好,因为你实际上是在包装使用Action< t>进行非通用Action(不带参数)。



这样考虑一下:当你将一个参数传递给一个方法时,是否要求使用该参数?考虑:



This is just fine, because you are in fact wrapping the non-generic Action (takes no parameters) with Action<t>.

Think about it this way: when you pass an argument to a method, is there a requirement that parameter be used? Consider:

public bool DoSomething(int useless)
{
   return true;
}





现在,Command构造函数按照您的暗示工作:如果它获得非泛型Action,它将包装它在Action< object>中。包装的命令对object参数没有任何作用,就像上面的例子对它没有任何作用,但现在它将执行并匹配预期的签名。



Now, the Command constructor works as you implied: if it gets a non-generic Action it will wrap it in Action<object>. The wrapped command does nothing with the object argument, just like the example above does nothing with it, but it will now execute and match the expected signature.


这篇关于我可以施放一个动作&lt; T&gt;一个动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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