问题描述
我正在使用.net为SPA应用程序开发Web Api。当我在方法中使用实例化对象时收到CA2000警告。但是,当我在类级别声明同一对象时,CA2000警告消失。从下面看,示例1给出了CA2000警告,而示例2没有给出。为什么?
I am working on a Web Api for a SPA app using .net. I get CA2000 warnings when i use instantiate an object within a method. But when I declare the same object at the class level, the CA2000 warnings disappear. From below, example 1 gives the CA2000 warning while example 2 does not. Why?
示例1-
public class CodeGenAPIController : ApiResponseController
{
NextGenCodeGen.CodeGenerator getEndPoint(TokenManager.TokenData tokenData, int BranchId)
{
NextGenCodeGen.CodeGenerator ret = null;
lock (branchGenLock)
{
if (branchGenerators.ContainsKey(BranchId))
ret = branchGenerators[BranchId];
}
if (ret == null)
{
string services = ConfigurationValuesAPIController.GetBranchProperties(tokenData.DatabaseIdentifier, BranchId).FirstOrDefault(x => x.Key == "AvailableCodeGenServices").Value;
string[] endpoints = services.Split(new char[] { ' ', '.' }, StringSplitOptions.RemoveEmptyEntries);
if (endpoints.Length == 0)
throw new ArgumentOutOfRangeException("AvailableCodeGenServices",
string.Format("There appear to be no Code Generation Services configured for branch {0}", BranchId));
string endpoint = endpoints[0];
if (!endpoint.ToLower().EndsWith(".asmx"))
endpoint = endpoint + ".asmx";
//OBJECT INSTANTIATION INSIDE THE METHOD
ret = new My_API.NextGenCodeGen.CodeGenerator() { Url = endpoint, UseDefaultCredentials = true};**
lock(branchGenLock)
{
branchGenerators[BranchId] = ret;
}
}
return ret;
}
}
示例2
public class CodeGenAPIController : ApiResponseController
{
//OBJECT INSTANTIATION OUTSIDE THE METHOD AT THE CLASS LEVEL
NextGenCodeGen.ARGenTCodeGenerator retVal = new My_API.NextGenCodeGen.ARGenTCodeGenerator();
NextGenCodeGen.CodeGenerator getEndPoint(TokenManager.TokenData tokenData, int BranchId)
{
retVal = null;
lock (branchGenLock)
{
if (branchGenerators.ContainsKey(BranchId))
retVal = branchGenerators[BranchId];
}
if (retVal == null)
{
string services = ConfigurationValuesAPIController.GetBranchProperties(tokenData.DatabaseIdentifier, BranchId).FirstOrDefault(x => x.Key == "AvailableCodeGenServices").Value;
string[] endpoints = services.Split(new char[] { ' ', '.' }, StringSplitOptions.RemoveEmptyEntries);
if (endpoints.Length == 0)
throw new ArgumentOutOfRangeException("AvailableCodeGenServices",
string.Format("There appear to be no Code Generation Services configured for branch {0}", BranchId));
string endpoint = endpoints[0];
if (!endpoint.ToLower().EndsWith(".asmx"))
endpoint = endpoint + ".asmx";
retVal = new My_API.NextGenCodeGen.CodeGenerator();
retVal.Url = endpoint;
retVal.UseDefaultCredentials = true;
lock (branchGenLock)
{
branchGenerators[BranchId] = retVal;
}
}
return retVal;
}
}
推荐答案
在在类情况下,应该应该触发CA2213,而不是CA2000。
In the class case, it should trip CA2213 and not CA2000. https://msdn.microsoft.com/en-us/library/ms182328.aspx
似乎CA2213有一个局限性,即它无法弄清楚如何实现IDisposable的基类。在下面的示例中,FileIo继承自IoBase,并且不会引发警告。 FileIo2仅实现IDisposable,并且确实发出警告。
It looks like there's a limitation of CA2213 that it can't figure out what to do with base classes that implement IDisposable. In the example below, FileIo inherits from IoBase and doesn't raise the warning. FileIo2 only implements IDisposable and does raise the warning.
public class IoBase : IDisposable
{
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
}
}
public class FileIo: IoBase
{
private Stream io = new FileStream("c:\tmp\tmp.txt", FileMode.OpenOrCreate);
}
public class FileIo2 : IDisposable
{
private Stream io = new FileStream("c:\tmp\tmp.txt", FileMode.OpenOrCreate);
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
}
}
这篇关于CA2000警告的类级别变量和方法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!