我有一个具有以下属性的类:
public Action<bool> Action { get; private set; }
Run Code Online (Sandbox Code Playgroud)
我有一个构造函数Action<bool> 作为参数.
现在我想添加另一个接受类型对象的构造函数Action.我怎样才能转换Action成Action<bool>?在这种情况下,bool参数应该为true.
Dar*_*rov 13
public class Foo
{
public Foo(Action<bool> action)
{
// Some existing constructor
}
public Foo(Action action): this(x => action())
{
// Your custom constructor taking an Action and
// calling the existing constructor
}
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以通过两种方式实例化此类,具体取决于您要调用的2个构造函数中的哪一个:
var foo = new Foo(x => { Console.WriteLine("Hello"); }); (称第一个ctor)var foo = new Foo(() => { Console.WriteLine("Hello"); }); (打电话给第二个ctor)Action a = () => aboolaction(true);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3135 次 |
| 最近记录: |