为了账号安全,请及时绑定邮箱和手机立即绑定

C# 方法,无论输入/返回类型如何,都采用另一个方法作为参数

C# 方法,无论输入/返回类型如何,都采用另一个方法作为参数

C#
沧海一幻觉 2023-09-16 20:14:11
如果标题不清楚,我很抱歉。基本上,我想通过将大量重复的错误处理移到一个地方来干燥我的代码。我正在调用几种方法,它们都会抛出类似的错误。每个都采用不同的参数,并返回不同的类型。我希望能够做这样的事情:public class MyClass {    public static ErrorWrapper<Void> Method1(string s1, string s2) {        return Wrapper<Void>(System.Method1, s1, s2);    }    public static ErrorWrapper<string> Method2(string s) {        return Wrapper<string>(System.Method2, s);    }    public static ErrorWrapper<MyOtherClass> Method3(string s, int i) {        return Wrapper<MyOtherClass>(System.Method3, s, i)    }    private static ErrorWrapper<T> Wrapper<T>(Func f, /*parameters?*/) {        try {            return f(parameters);        } catch {            // Handle exceptions        }}我需要这样做,因为我正在为一种没有异常处理的语言编写绑定,因此使用错误包装类是安全调用标准库方法的唯一方法。
查看完整描述

2 回答

?
蝴蝶不菲

TA贡献1810条经验 获得超4个赞

除非我在这里遗漏了一些东西 -


private static ErrorWrapper<T> Wrapper<T>(Func<T> f) 

{

    // implementation

}

用法:


return Wrapper<string>(() => System.Method2(s));


return Wrapper<MyOtherClass>(() => System.Method3(s, I));


查看完整回答
反对 回复 2023-09-16
?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

这是我根据我对您正在尝试的事情的理解提出的建议。不幸的是,您必须使用速度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);

        }

    }

}



查看完整回答
反对 回复 2023-09-16
  • 2 回答
  • 0 关注
  • 51 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信