根据我对您的尝试的理解,这是我的建议。不幸的是,您必须使用很慢的DynamicInvoke,以便在调用通用Delegate 时在运行时检查类型。你不能使用void 作为泛型的类型参数,所以我创建了一个我专门处理的MyVoid 类。
我更新了我的答案以反映 System.Methods 是实际方法,而不是静态字段,因此在传递给 Wrapper<> 时必须强制转换它们 - C# 无法将方法转换为通用 Delegate .
我还添加了一个类型安全的版本,但这需要创建很多样板方法来处理参数的数量,但它确实消除了很多强制转换并且可以静态调用委托,这应该更有效。
我不小心让Method3 最初采用了两个字符串参数,并且非类型安全版本在运行时调用之前没有错误。类型安全的版本在编译时发现了错误。
public class MyVoid { }
public class ErrorWrapper<T> {
public T Result;
public bool ValidResult;
public Exception Exception;
public ErrorWrapper(T res) {
ValidResult = true;
Result = res;
}
public ErrorWrapper(Exception e) {
Exception = e;
ValidResult = false;
}
public ErrorWrapper() { // void
ValidResult = true;
}
}
public class MyClass {
public static ErrorWrapper<MyVoid> Method1(string s1, string s2) => Wrapper<MyVoid>((Action<string, string>)System.Method1, s1, s2);
public static ErrorWrapper<string> Method2(string s) => Wrapper<string>((Func<string, string>)System.Method2, s);
public static ErrorWrapper<MyOtherClass> Method3(string s, int i) => Wrapper<MyOtherClass>((Func<string, int, MyOtherClass>)System.Method3, s, i);
private static ErrorWrapper<T> Wrapper<T>(Delegate f, params object[] p) {
try {
switch (default(T)) {
case MyVoid _:
f.DynamicInvoke(p);
return new ErrorWrapper<T>();
default:
return new ErrorWrapper<T>((T)f.DynamicInvoke(p));
}
}
catch (Exception e) {
// Handle exceptions
return new ErrorWrapper<T>(e);
}
}
}
public static class ErrorWrapper {
public static ErrorWrapper<T> New<T>(T res) => new ErrorWrapper<T>(res);
}
public class MyTypeSafeClass {
public static ErrorWrapper<MyVoid> Method1(string s1, string s2) => WrapperAction(System.Method1, s1, s2);
public static ErrorWrapper<string> Method2(string s) => WrapperFunc(System.Method2, s);
public static ErrorWrapper<MyOtherClass> Method3(string s, int i) => WrapperFunc(System.Method3, s, i);
private static ErrorWrapper<MyVoid> WrapperAction<T1, T2>(Action<T1, T2> f, T1 p1, T2 p2) {
try {
f(p1, p2);
return ErrorWrapper.New(default(MyVoid));
}
catch (Exception e) {
// Handle exceptions
return new ErrorWrapper<MyVoid>(e);
}
}
private static ErrorWrapper<TRes> WrapperFunc<T1, TRes>(Func<T1, TRes> f, T1 p1) {
try {
return ErrorWrapper.New(f(p1));
}
catch (Exception e) {
// Handle exceptions
return new ErrorWrapper<TRes>(e);
}
}
private static ErrorWrapper<TRes> WrapperFunc<T1, T2, TRes>(Func<T1, T2, TRes> f, T1 p1, T2 p2) {
try {
return ErrorWrapper.New(f(p1, p2));
}
catch (Exception e) {
// Handle exceptions
return new ErrorWrapper<TRes>(e);
}
}
}