我需要用一些代码包装一些函数,这些代码将预处理这些函数的输入数据(“包装器”)。这是使用委托编写的代码(该代码已尽可能简化,同时仍可正常运行且可立即编译):namespace ConsoleApp{ class ClientContext { public int i = 1; } class SPRemoteEventProperties { } class Program { public delegate void Del(ClientContext clientContext, SPRemoteEventProperties properties); public static void Wrapper(Del f, ClientContext clientContext, SPRemoteEventProperties properties) { ClientContext newClientContext = new ClientContext { i = clientContext.i * 2 }; f(newClientContext, properties); } static void Main(string[] args) { Function1(new ClientContext(), new SPRemoteEventProperties()); Function2(new ClientContext(), new SPRemoteEventProperties()); Wrapper(Function1, new ClientContext(), new SPRemoteEventProperties()); Wrapper(Function2, new ClientContext(), new SPRemoteEventProperties()); Console.Read(); } static void Function1(ClientContext clientContext, SPRemoteEventProperties properties) { Console.WriteLine("Function1: " + clientContext.i); } static void Function2(ClientContext clientContext, SPRemoteEventProperties properties) { Console.WriteLine("Function2: " + clientContext.i); } }}现在,为了简化代码,我想使用Action 语法重写它并内联函数的代码。这是我尝试执行的操作,但是我无法编写正确且有效的程序:using System;namespace ConsoleApp{ class ClientContext { public int i = 1; } class SPRemoteEventProperties { } class Program { Action<ClientContext, SPRemoteEventProperties> Act; // error in next line => public static void Wrapper(Act f, ClientContext clientContext, SPRemoteEventProperties properties) { ClientContext newClientContext = new ClientContext { i = clientContext.i * 2 }; f(newClientContext, properties); } static void Main(string[] args) { Function1(new ClientContext(), new SPRemoteEventProperties()); Function2(new ClientContext(), new SPRemoteEventProperties()); // error in next 2 lines => Wrapper((new ClientContext(), new SPRemoteEventProperties()) => Console.WriteLine("Function1: " + clientContext.i)); Wrapper((new ClientContext(), new SPRemoteEventProperties()) => Console.WriteLine("Function2: " + clientContext.i)); Console.Read(); } static void Function1(ClientContext clientContext, SPRemoteEventProperties properties) { Console.WriteLine("Function1: " + clientContext.i); } static void Function2(ClientContext clientContext, SPRemoteEventProperties properties) { Console.WriteLine("Function2: " + clientContext.i); } }}您能帮我更正此代码吗? (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 我不确定我是否完全了解您要做什么,我是否会关闭?public static void Wrapper(ClientContext clientContext, SPRemoteEventProperties properties, Action<ClientContext, SPRemoteEventProperties> action){ action(newClientContext, properties);}...Wrapper( new ClientContext(), new SPRemoteEventProperties(),(context, properties) => Console.WriteLine("Function1: " + context.i)); (adsbygoogle = window.adsbygoogle || []).push({}); 09-25 22:04