C# 重构委托:应为方法名称

C# 重构委托:应为方法名称,c#,delegates,C#,Delegates,其中有以下代码段: behaviorManager1 = new DevExpress.Utils.Behaviors.BehaviorManager(); behaviorManager1.Attach<DevExpress.Utils.DragDrop.DragDropBehavior>(editor.GridView, behavior => { behavior.Properties.AllowDrop = true; behavior.Properti

其中有以下代码段:

behaviorManager1 = new DevExpress.Utils.Behaviors.BehaviorManager();
behaviorManager1.Attach<DevExpress.Utils.DragDrop.DragDropBehavior>(editor.GridView, behavior => {
    behavior.Properties.AllowDrop = true;
    behavior.Properties.InsertIndicatorVisible = true;
    behavior.Properties.PreviewVisible = true;
    behavior.DragOver += Behavior_DragOver;
    behavior.DragDrop += Behavior_DragDrop;
});
BehaviorManager=new DevExpress.Utils.Behaviors.BehaviorManager();
behaviorManager1.Attach(editor.GridView,behavior=>{
behavior.Properties.AllowDrop=true;
behavior.Properties.InsertIndicatorVisible=true;
behavior.Properties.PreviewVisible=true;
behavior.DragOver+=行为\u DragOver;
behavior.DragDrop+=行为\u DragDrop;
});
为了更好地理解,我希望重构代码,使“behavior=>”代码不一致

我试过:

var behaviour = new DragDropBehavior(ctrl.GetType());
behaviour.DragDrop += Behavior_DragDrop;
behaviour.DragOver += Behavior_DragOver;
var action = new Action<DragDropBehavior>(behaviour); // compile error
behaviorManager1.Attach(gridView1, action);
var behavior=newdragdropbehavior(ctrl.GetType());
Behavior.DragDrop+=行为\u DragDrop;
Behavior.DragOver+=行为\u DragOver;
var动作=新动作(行为);//编译错误
行为管理器1.Attach(gridView1,操作);
但是,我得到了一个编译错误

错误CS0149应为方法名称


初始代码的功能与新代码的功能有所不同。最初,您将传入一个匿名函数,调用该函数时将在提供的
DragDropBehavior
实例上设置一些属性。新代码显式地创建了
DragDropBehavior
的实例并将其填充

您还试图创建一个
操作
的实例,该实例需要一个委托,但将其传递给新创建的对象。这就是为什么编译器不喜欢它

您仍然可以将该参数提取到变量中,但应将其键入为
Action
,并且所有赋值应在匿名函数中:

behaviorManager1 = new DevExpress.Utils.Behaviors.BehaviorManager();
Action<DragDropBehavior> behaviorDelegate = behavior => {
    behavior.Properties.AllowDrop = true;
    behavior.Properties.InsertIndicatorVisible = true;
    behavior.Properties.PreviewVisible = true;
    behavior.DragOver += Behavior_DragOver;
    behavior.DragDrop += Behavior_DragDrop;
};
behaviorManager1.Attach<DevExpress.Utils.DragDrop.DragDropBehavior>(editor.GridView, behaviorDelegate);
BehaviorManager=new DevExpress.Utils.Behaviors.BehaviorManager();
Action behaviorDelegate=行为=>{
behavior.Properties.AllowDrop=true;
behavior.Properties.InsertIndicatorVisible=true;
behavior.Properties.PreviewVisible=true;
behavior.DragOver+=行为\u DragOver;
behavior.DragDrop+=行为\u DragDrop;
};
Attach(editor.GridView,behaviorDelegate);

以上是一个匿名函数。如果您不想使用该语法,当然必须使用命名函数。