简体   繁体   English

如何使用匿名方法或匿名类型来减少此代码?

[英]How to reduce this code using anonymous methods or anonymous types?

I have a web service and some methods and I do NOT want to use this code below, which works fine, because I intend to use this in a multi-threaded app where static (I have read) is not thread safe. 我有一个Web服务和一些方法,并且我不想在下面使用此代码,该代码工作正常,因为我打算在静态(我已阅读)不是线程安全的多线程应用程序中使用此代码。

So, what I have done now is simply repeat the code that is in the static method 'returnisTrue()' below, in the body of my web service method "myWebMethod", at points //1 of 3, //2 of 3, and //3 of 3. This works fine but results in bloated code. 因此,我现在要做的只是重复以下静态方法“ returnisTrue()”中位于我的Web服务方法“ myWebMethod”主体中的代码,在// 1的第3点,// 2的第3点,和// 3之3。这可以正常工作,但会导致代码肿。 Is there a more compact way, using functional programming or anonymous methods or what have you that I can use? 是否有使用函数式编程或匿名方法的更紧凑的方法,或者我可以使用什么?

Note the compiler choked when I tried to create a class inside the method...which would have solved my problem. 请注意,当我尝试在方法内部创建类时,编译器感到窒息……这将解决我的问题。

As I say the code I have now works fine but it is bloated, as I repeat the 'static' method 'returnisTrue' three times. 就像我说的那样,我现在拥有的代码可以正常工作,但是它膨胀了,因为我重复了三遍“静态”方法“ returnisTrue”。

Thank you. 谢谢。

EDIT: in response to some questions about making my static method thread safe, which I rather not bother with, but in the interest of getting a solution I include below. 编辑:回答一些有关使我的静态方法线程安全的问题,我宁愿不打扰,但为了获得解决方案,我在下面提供了此问题。

All this code is server side in a web service 所有这些代码都是Web服务中的服务器端

// I cannot unfortunately use this static method below--not thread safe
static private bool returnisTrue()
{
  bool isTrue = false;
  // do a bunch of stuff here
  return isTrue;
}

public  bool myWebMethod (string XYZ)
{
  //public Class myClassReturnisTrue  { … }  //will not compile

  bool isTrueOrNot1 = returnisTrue(); //1 of 3

  /// more code here

  bool  isTrueOrNot2 = returnisTrue(); //2 of 3

  ///more code here

  bool isTrueOrNot3 = returnisTrue(); //3 of 3

  return isTrueOrNot1 && isTrueOrNot2 && isTrueOrNot3;
}
 // here is the static method 'returnisTrue' it looks something like this:


 static private bool returnIsTrue(string A, string B)
 {
 if (A.Length() < B.Length())
     {
return true;
     }
else
return false;

 }

What are you actually doing within your static method? 您在静态方法中实际上在做什么? Are you modifying any global state or are you only working on local variables? 您要修改任何全局状态还是仅在局部变量上工作? There is nothing wrong with using static methods, or even variables, as long as you take proper measures to make sure that your code within is actually thread-safe. 只要采取适当的措施以确保其中的代码实际上是线程安全的,使用静态方法甚至变量都没有问题。

Both static methods and instance methods can potentially be unsafe depending on what you are doing within them. 静态方法和实例方法都可能不安全,具体取决于您在其中执行的操作。 The main question to ask is, are you accessing any data that is accessible between multiple threads within your method? 要问的主要问题是,您是否正在访问方法中多个线程之间可访问的任何数据? And if so, why not change your method to be thread-safe (through for example locking). 如果是这样,为什么不将您的方法更改为线程安全的(例如通过锁定)。

Take a look for example at the .NET framework itself, it contains various static thread-safe methods throughout. 以.NET框架为例,该框架始终包含各种静态线程安全方法。

Edit: 编辑:

Ok, now that we've seen your method - it is indeed thread-safe already. 好的,现在我们已经看到了您的方法-它确实已经是线程安全的。 The method is not accessing any shared data and strings are immutable and inherently thread-safe as a result . 该方法不访问任何共享数据,并且字符串是不可变的,因此具有固有的线程安全性

如果lock关键部分,则应该能够使用静态方法(您可能希望在msdn-> http://msdn.microsoft.com/zh-cn/library/c5kehkcz.aspx上查找它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM