我希望这段代码能够说明我的问题。我需要像注释掉的那一行一样调用CallEvent
方法。
我无法访问ThirdParty
或AnotherThirdParty
类。
以下是我目前所做的:
public class ThirdParty
{
private struct MsgType
{ }
private static void AnotherFunc(MsgType msg)
{ }
}
public class AnotherThirdParty
{
public static void CallEvent<T>(Func<int, Action<T>> action, T arg)
{ }
}
public class MyClass
{
public static void Main()
{
Type MsgType = typeof(ThirdParty).GetNestedType(
"MsgType", BindingFlags.Instance | BindingFlags.NonPublic);
object msg = Activator.CreateInstance(MsgType);
MethodInfo CallEvent = typeof(AnotherThirdParty).GetMethod("CallEvent");
CallEvent = CallEvent.MakeGenericMethod(MsgType);
MethodInfo AnotherFunc = typeof(ThirdParty).GetMethod(
"AnotherFunc", BindingFlags.Static | BindingFlags.NonPublic);
CallEvent.Invoke(null, new object[] {???, msg});
//CallEvent<MsgType>((int x) => new Action<MsgType>(AnotherFunc), msg);
// I can't get my head around how to solve this (Action<msgtype>)
}
}
我也尝试过:
CallEvent.Invoke(null, new object[]
{
new Func<int, Action<object>>((int x) =>
new Action<object>((object y) =>
AnotherFunc.Invoke(null, new object[] { y }))),
msg
});
我遇到了以下异常:
我该怎么办?System.ArgumentException: 无法将类型为'System.Func2[System.Int32,System.Action1[System.Object]]'的对象转换为类型'System.Func2[System.Int32,System.Action1[ThirdParty+MsgType]]。
AnotherThirdParty.CallEvent
除了通过 action 发送出去,还会对T
参数做任何操作吗? - ClintRaiseEvent<T1, T2, T3>(T1 arg1, T3 arg3, Func<T1, Action<T2>> action, T2 arg2),其中 T1 : int,T3 : int
。 - timo