[英]Pass Method as Parameter using C# with Parameters - Using the Command Pattern
我是传递委托的新手,想知道以下代码是否可行?
CommandQueue.AddCommand(
new CommandItem(
group.MyGroupActionMovement(
new Vector3(-1000,-1000,-1000))));
好的,详细说明我将对我在命令模式上的错误尝试进行代码转储...
所以我试图创建一个命令队列。 仅从移动命令开始。 它需要一个 vector3 作为参数。 其他命令将具有不同的参数,如枚举和游戏对象。
这应该是我的命令/操作界面,但是,我一直在弄乱它,看看我能做什么。
public class IGroupAction: MonoBehaviour
{
public delegate bool Del(Vector3 destination);
public virtual bool Execute()
{
return true;
}
}
这应该是命令/动作单元/项目
public class GroupActionItem
{
public IGroupAction MyGroupAction;
public GroupActionItem(IGroupAction.Del action)
{
}
public bool PerformAction()
{
return MyGroupAction.Execute();
}
}
这是我迄今为止完成的命令模式
public class GroupAction
{
public List<GroupActionItem> Actions;
public GroupAction()
{
Actions = new List<GroupActionItem>();
}
public void AddAction(GroupActionItem groupActionItem)
{
Actions.Add(groupActionItem);
}
public void ClearActions()
{
Actions.Clear();
}
public void PerformActions()
{
if (Actions.Count >= 1)
{
if(Actions[0].PerformAction())
{
Actions.Remove(Actions[0]);
}
}
}
}
这是我执行移动命令的地方
public class GroupActionMovement : IGroupAction
{
public override bool Execute()
{
return Movement();
}
public bool Movement(Vector3 destination)
{
return true;
}
}
public class Group : MonoBehaviour
{
public GroupActionMovement MyGroupActionMovement;
}
这是编码行为的执行。
public class Player : MonoBehaviour
{
public Dictionary<Group,GroupAction> PlayerGroupsActions;
public List<Group> Groups;
void Start()
{
PlayerGroupsActions = new Dictionary<Group, GroupAction>();
Groups = GetComponentsInChildren<Group>().ToList();
foreach (Group group in Groups)
{
GroupAction playerGroupActions = new GroupAction();
PlayerGroupsActions.Add(group,playerGroupActions);
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
//remove the foreach
foreach (Group group in Groups)
{
//directly apply the group
PlayerGroupsActions.TryGetValue(group, out GroupAction playerGroupActions);
if (playerGroupActions != null)
{
playerGroupActions.AddAction(new GroupActionItem(group.MyGroupActionMovement.Movement));
}
}
}
foreach (Group group in Groups)
{
PlayerGroupsActions.TryGetValue(group, out GroupAction playerFormationActions);
if (playerFormationActions != null)
{
if (playerFormationActions.Actions.Count != 0)
{
playerFormationActions.PerformActions();
}
}
}
}
}
如果这不是正在发生的事情的最佳解释,或者代码转储不是解释我在做什么的最佳方式,我很抱歉。
不要将它作为方法传递,使用这样的东西
CommandQueue.Command += doSomething;
void doSomething()
{
//doSomething
}
因此,每当函数CommandQueue.Command
在单独的类中运行时,它也会在主类中运行doSomething()
void
委托不是方法调用,而是它自己的方法,接收者可以在需要时使用它想要的参数调用它。
下面是一个例子:
public delegate int MyDelegate(Vector3 vec);
public class MyCallingClass
{
public void DoSomething(MyDelegate method)
{
// Do something.
int result = method(new Vector3(-1000, -1000, -1000));
// Do something else.
}
}
public class MyClass
{
public static int MyStaticMethod(Vector3 arg)
{
// Do something with arg.
return x;
}
int MyMethod(Vector3 arg)
{
// Do something with arg.
return x;
}
private void DelegateWork()
{
MyCallingClass c = // Get an instance of MyCallingClass.
c.DoSomething(this.MyMethod);
c.DoSomething(MyClass.MyStaticMethod);
}
}
这里有很多事情要理解:
在c.DoSomething(this.MyMethod)
, this
被捕获到您传递的委托中,这意味着当MyCallingClass.DoSomething
将调用委托时,它将在MyClass
的实例上调用。 行c.DoSomething(MyClass.MyStaticMethod)
将在没有实例的情况下调用它,因为它是一个静态方法。
这是MyCallingClass.DoSomething
方法决定传递哪些参数,而不是提供委托的方法。
如果你需要调用者提供参数,而调用部分只决定何时调用而不决定传递什么参数,那么你可以提前捕获参数,并传递一个不带参数的委托,如下所示。
public delegate int MyDelegate(); // No arguments anymore.
public class MyCallingClass
{
public void DoSomething(MyDelegate method)
{
// Do something.
int result = method(); // Not passing arguments anymore.
// Do something else.
}
}
public class MyClass
{
// ...
private void DelegateWork()
{
MyCallingClass c = // Get an instance of MyCallingClass.
c.DoSomething(() => this.MyMethod(new Vector3(-1000, -1000, -1000)));
c.DoSomething(() => MyClass.MyStaticMethod(new Vector3(-1000, -1000, -1000));
}
}
如果您不熟悉语法() => ...
,这是一个 lambda 表达式,您可以将其视为即时创建的匿名函数。 它仍然尊重返回int
且不带任何参数的原型。 lambda 表达式现在捕获Vector3
实例的构造,因此在调用 lambda 时将使用该值。 要理解的一个非常重要的方面是 lambda 表达式中的值是在调用 lambda 时计算的,而不是在创建时(惰性求值)。
delegate
,而是可以使用Func<Vector3, int>
,如下所示:// Not actually needed.
// public delegate int MyDelegate(Vector3 vec);
public class MyCallingClass
{
public void DoSomething(Func<Vector3, int> method)
{
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.