Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法中的临时方法?_C#_C# 4.0_Design Patterns_Delegates_Func - Fatal编程技术网

C# 方法中的临时方法?

C# 方法中的临时方法?,c#,c#-4.0,design-patterns,delegates,func,C#,C# 4.0,Design Patterns,Delegates,Func,在下面的方法中,有三个条件。我想用一个方法替换它们并传入条件 此外,条件体几乎是重复的。是否可以创建仅在MyMethod()中本地存在的方法?因此,下面的代码简化为如下内容: public ClassZ MyMethod (Class1 class1Var, Class2 class2Var) { Func<bool, bool, string, ClassZ> myFunc = (predicate, prop1Value, string1Val

在下面的方法中,有三个条件。我想用一个方法替换它们并传入条件

此外,条件体几乎是重复的。是否可以创建仅在MyMethod()中本地存在的方法?因此,下面的代码简化为如下内容:

public ClassZ MyMethod (Class1 class1Var, Class2 class2Var)
{
    Func<bool, bool, string, ClassZ> myFunc 
             = (predicate, prop1Value, string1Value) => 
                      predicate 
                      ? new ClassZ { Property1 = prop1Value, String1 = string1Value }
                      : null;
    return myFunc((class1Var.SomeBool && !class2Var.SomeBool), false, "This is string1")
           ?? myFunc((class1Var.SomeBool && !class2Var.IsMatch(class2Var)), true)
           ?? myFunc((class1Var.SomeProperty.HasValue && !class2Var.SomeBool), false, "This is string2");
}
//简化码

public ClassZ MyMethod (Class1 class1Var, Class2 class2Var)
{
 return localMethod((class1Var.SomeBool && !class2Var.SomeBool), false, "This is string1");
 return localMethod((class1Var.SomeBool && !class2Var.IsMatch(class2Var)), true);
 return localMethod((class1Var.SomeProperty.HasValue && !class2Var.SomeBool), false, "This is string2");

//...localMethod() defined here...
}
但在上面,只有一个应该返回

//原始代码

public ClassZ MyMethod (Class1 class1Var, Class2 class2Var)
{

 if(class1Var.SomeBool && !class2Var.SomeBool)
 {
   return new ClassZ
   {
     Property1 = false,
     String1 = "This is string1"
   };
 }

 if(class1Var.SomeBool && !class2Var.IsMatch(class2Var))
 {
   return new ClassZ
   {
     Property1 = true,
   };
 }
 if(class1Var.SomeProperty.HasValue && !class2Var.SomeBool)
 {
   return new ClassZ
   {
     Property1 = false,
     String1 = "This is string2"
   };
 }

}

基本上,我想在一个方法中创建一个临时方法。

您可以使用
Func
表示法<代码>函数封装委托方法。阅读
Funcs
的文档。对于具有
n
通用参数的
Func
,第一个
n-1
是方法的输入,最后一个参数是返回类型。试着这样做:

public ClassZ MyMethod (Class1 class1Var, Class2 class2Var)
{
    Func<bool, bool, string, ClassZ> myFunc 
             = (predicate, prop1Value, string1Value) => 
                      predicate 
                      ? new ClassZ { Property1 = prop1Value, String1 = string1Value }
                      : null;
    return myFunc((class1Var.SomeBool && !class2Var.SomeBool), false, "This is string1")
           ?? myFunc((class1Var.SomeBool && !class2Var.IsMatch(class2Var)), true)
           ?? myFunc((class1Var.SomeProperty.HasValue && !class2Var.SomeBool), false, "This is string2");
}

您可以为此使用
Func
符号<代码>函数封装委托方法。阅读
Funcs
的文档。对于具有
n
通用参数的
Func
,第一个
n-1
是方法的输入,最后一个参数是返回类型。试着这样做:

public ClassZ MyMethod (Class1 class1Var, Class2 class2Var)
{
    Func<bool, bool, string, ClassZ> myFunc 
             = (predicate, prop1Value, string1Value) => 
                      predicate 
                      ? new ClassZ { Property1 = prop1Value, String1 = string1Value }
                      : null;
    return myFunc((class1Var.SomeBool && !class2Var.SomeBool), false, "This is string1")
           ?? myFunc((class1Var.SomeBool && !class2Var.IsMatch(class2Var)), true)
           ?? myFunc((class1Var.SomeProperty.HasValue && !class2Var.SomeBool), false, "This is string2");
}

不确定您到底想要什么,但您的本地方法肯定可以只是一个私有方法?它可以。我宁愿尝试将它保存在MyMethod()中,因为它不需要存在于外部。我认为这是不可能的,如果您真的担心意外调用它,您可以将整个过程放入一个嵌套类中,但您真正关心的是什么?如果它是私有的,它甚至不能被派生类调用,因此如果它被清楚地记录为不可使用,那么问题是什么?不确定您到底想要什么,但确定您的本地方法可能只是一个私有方法?它可以。我宁愿尝试将它保存在MyMethod()中,因为它不需要存在于外部。我认为这是不可能的,如果您真的担心意外调用它,您可以将整个过程放入一个嵌套类中,但您真正关心的是什么?如果它是私有的,它甚至不能被派生类调用,所以如果它被清楚地记录为不可使用,那么问题是什么?