110,536
社区成员
发帖
与我相关
我的任务
分享
class A
{
void method1() { } //这是一个方法
int method3(int p) => 0;
void method2()//通过此方法传递method1
{
B b = new B();
b.method(new Action(method1));
b.method(new Func<int,int>(method3),1);
}
}
class B
{
public void method(params object[] args) //method1传入 然后调用
{
if (args?.Length > 0 && args[0] is Delegate del)
{
if(args.Length > 1)
{
del.DynamicInvoke(args[1]);
}
else
{
del.DynamicInvoke(null);
}
}
}
}
public class A
{
/// <summary>
/// 被调用的方法
/// </summary>
private void MethodA()
{
Console.WriteLine("MethodA");
}
/// <summary>
/// 给B传入方法
/// </summary>
public void MethodB()
{
B.RunMethod run = MethodA;
var b = new B();
b.Method(run);
}
}
public class B
{
/// <summary>
/// 声明委托
/// </summary>
public delegate void RunMethod();
/// <summary>
/// 执行的方法
/// </summary>
/// <param name="args"></param>
public void Method(params object[] args)
{
// 假设第一个参数就是委托就执行委托的内容
if (args != null && args[0] != null && args[0] is RunMethod)
{
(args[0] as RunMethod)();
}
}
}
static void aaa()
{
Console.WriteLine("aaa");
}
static void bbb(params object[] obj)
{
foreach (object o in obj)
{
if (o is Delegate)
{
((Delegate) o).Method.Invoke(null, null);
}
}
}
static void Main(string[] args)
{
Action a = aaa;
bbb(a, a);
Console.ReadLine();
}
static void bbb(params object[] obj)
{
foreach (object o in obj)
{
if (o is Delegate)
{
((Delegate) o).Method.Invoke(null, null);
}
}
}
static void Main(string[] args)
{
Action a = ()=> {
Console.WriteLine("aaa");
};
bbb(a, a);
Console.ReadLine();
}
static void Main(string[] args)
{
B.TestB(A.TestA, 1,2,3,"hello");
Console.ReadLine();
}
public class A
{
public static void TestA(object[] t)
{
Console.WriteLine(string.Join(",",t));
}
}
public class B
{
public static void TestB(Action<object[]> a, params object[] args)
{
a(args);
}
}
class B
{
method(params object[] args) //method1传入 然后调用
{
args[0] 怎么调用????
}
method(Func func,params object[] args)
{
}
}