Phi*_*hil 0 database asp.net fxcop declaration code-sharing
我一直在使用以下代码共享数据库变量:
Namespace DataAccessVariables
Public Class Vars
Public Shared s As String
Public Shared con As String = WebConfigurationManager.ConnectionStrings("Dev").ToString()
Public Shared c As New SqlConnection(con)
Public Shared x As New SqlCommand(s, c)
End Class
End Namespace
Run Code Online (Sandbox Code Playgroud)
然后我将其导入我的项目,如下所示:
Imports DataAccessVariables.Vars
Run Code Online (Sandbox Code Playgroud)
当我用FXCop检查网站时,我收到以下消息:
Error, Certainty 90, for StaticHolderTypesShouldNotHaveConstructors
{
Target : DBVars (IntrospectionTargetType)
Resolution : "Remove the public constructors from 'Vars'."
Help : http://msdn2.microsoft.com/library/ms182169(VS.90).aspx (String)
Category : Microsoft.Design (String)
CheckId : CA1053 (String)
RuleFile : Design Rules (String)
Info : "Instances of types that define only static members
do not need to be created. Many compilers will automatically
add a public default constructor if no constructor
is specified. To prevent this, adding an empty private
constructor may be required."
Created : 2010/04/20 01:25:16 PM (DateTime)
LastSeen : 2010/04/21 07:17:46 AM (DateTime)
Status : Active (MessageStatus)
Fix Category : Breaking (FixCategories)
}
Run Code Online (Sandbox Code Playgroud)
如果我从声明中删除"公共共享",则不会在我的页面中拾取变量.谁能告诉我正确的分享方式?
非常感谢,菲尔.
小智 5
此错误不会告诉您删除公共共享变量.相反,它让你知道可以创建你的Vars类的新实例,即使它只包含Shared成员.要解决此问题,请定义私有构造函数:
Private Sub New()
End Sub
Run Code Online (Sandbox Code Playgroud)
这将阻止任何代码Vars在类本身之外创建类的实例.