public class ExecutionUtility
{
public static TIn2 ExecutionHelper<TIn1, TIn2>(Func<TIn1, out TIn2, int> function, TIn1 input)
{
TIn2 output = default(TIn2);
int result = function(input, out output); // the out keyworkd is not accepted apparently
if (result == 0)
{
return output;
}
else
{
throw new FunctionExecutionException(result);
}
}
}
只是一些例外代码:
public class FunctionExecutionException : Exception
{
public int ErrorCode { get; }
public FunctionExecutionException(int errorCode)
: base($"Function execution failed with error code: {errorCode}")
{
ErrorCode = errorCode;
}
}
返回 int 结果和字符串输出参数的静态函数示例
public static int myfunction(int input, out string output)
{
if (input > 0)
{
output = "OUTPUT_VALUE_1";
return 0;
}
else
{
output = "OUTPUT_VALUE_2";
return -1;
}
}
现在使用执行助手功能。由于某种原因,out 参数似乎未获得授权,并且我的代码无法编译。
return ExecutionUtility.API_VERIFY<int, string>(PRD_GCAPI.myfunction, this.Index);
您可以定义委托类型:
public delegate int FuncHelper<TIn1, TIn2>(TIn1 a, out TIn2 b);
public static TIn2 ExecutionHelper<TIn1, TIn2>(FuncHelper<TIn1, TIn2> function, TIn1 input)