C# 作为代码段/模板的内联函数

C# 作为代码段/模板的内联函数,c#,.net-4.5,C#,.net 4.5,编辑: 基本上,我希望能够使用以下机制来回答我的原始问题: void MethodA() { MethodB(); //This code will never be reached because 'return' from the inline MethodB is executed. //... More code here } [Attribute(Inline)] ReturnValue MethodB() { return; } 当然,上面的例

编辑: 基本上,我希望能够使用以下机制来回答我的原始问题:

void MethodA()
{
    MethodB();

    //This code will never be reached because 'return' from the inline MethodB is executed.

    //... More code here
}

[Attribute(Inline)]
ReturnValue MethodB()
{
    return;
}
当然,上面的例子本身是无用的,更多的逻辑将放在方法B中,当然,但为了简洁起见,我省略了它

原始问题: 在一个已经开发了1年的项目中,选择执行以下操作。每个函数不抛出异常,而是返回一个
ReturnValue
,其中有一个
issucessful
布尔标志。它是这样使用的

var methodResult = MyMethod();
if (!methodResult .IsSuccesful)
{
    return new ReturnValue<Type>(methodResult .ThrownException);
}
var value= methodResult.Value;
编译器,或者某个插件,或者其他什么,将这个预编译时间转换为前面显示的完整模式。我的观点是,如果!IsSuccessFul函数应返回。我可以编写一个预编译时间命令来实现这一点,但我希望VisualStudio本身能够采用某种片段/模板/内联方式

谢谢。

应该尽可能简单(对于模式来说,-这当然是毫无用处的,因为它基本上是身份,只不过它会重新创建另一个
返回值
-因此不建议):

等等

让它成为一元的

总体上,您的设计可能不坏,但是您应该考虑添加常用的函件映射和MundAddiDand操作(甚至可以在您使用< LINQ > SelectMany < /COD> >时使用LINQ到您的代码> ReutalValue/Copy>类型:

public static ReturnValue<B> Map<A,B>(this ReturnValue<a> v, Func<A,B> f)
{
    if (v.IsSuccesful) return new ReturnValue<B>(f(v.Value));
    return new ReturnValue<B>(v.ThrownException);
}

public static ReturnValue<B> Bind<A,B>(this ReturnValue<a> v, Func<A,ReturnValue<B>> f)
{
    if (v.IsSuccesful) return f(v.Value);
    return new ReturnValue<B>(v.ThrownException);
}

我认为您的方法是正确的,但我发现您的代码中有一个问题:
Func方法
需要重构为
Func方法
。否则,编译器将不知道
method()
results确实有一个名为
IsSuccesful
的属性,在
时如何退出调用方的作用域!IsSuccessful
使用您的建议。也许我没有把我的问题说得像我希望的那样清楚。我猜有点像C宏。像这样重新引用不是一个好主意,因为原始异常的堆栈跟踪被吹走了。包装可能是解决方案,但是最初的代码似乎太长了,以避免一开始就抛出。@mikez真的很难用我在
ReturnValue
后面看到的设计来改变它,你不能完全做到这一点-但实际上我给你的
Bind
是一个语法稍微复杂一点的东西-你用bubblinig返回尝试的不是在C#中可能(这正是
抛出
尝试
捕获
,…的目的)
public static ReturnValue<T> TryMethod<T>(Func<ReturnValue<T>> method)
{
   var methodResult = method();
   if (!methodResult .IsSuccesful)
       return new ReturnValue<T>(methodResult.ThrownException);

   // I can only guess at this:
   return methodResult;
}
public static T TryMethod<T>(Func<ReturnValue<T>> method)
{
   var methodResult = method();
   if (!methodResult .IsSuccesful)
       throw methodResult.ThrownException;

   return methodResult.Value;
}
public static T TryMethod<T,P>(Func<P,ReturnValue<T>> method, P p)
{
   var methodResult = method(p);
   // ...
public static T TryMethod<T>(this Func<ReturnValue<T>> method)
var value = MyMethod.TryMethod();
public static ReturnValue<B> Map<A,B>(this ReturnValue<a> v, Func<A,B> f)
{
    if (v.IsSuccesful) return new ReturnValue<B>(f(v.Value));
    return new ReturnValue<B>(v.ThrownException);
}

public static ReturnValue<B> Bind<A,B>(this ReturnValue<a> v, Func<A,ReturnValue<B>> f)
{
    if (v.IsSuccesful) return f(v.Value);
    return new ReturnValue<B>(v.ThrownException);
}
method().Bind(
    value => { 
        // ... whatever you want to do with value - just return a ReturnValue<> at the end 
    });