Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.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# 代码分析警告2214-如何最好地修复?_C# - Fatal编程技术网

C# 代码分析警告2214-如何最好地修复?

C# 代码分析警告2214-如何最好地修复?,c#,C#,我有以下代码: public partial class AuditLog : IBusinessEntity { public BusinessEntityType EntityType { get { return BusinessEntityType.AuditLog; } } /// <summary> /// Constructor accepting parameter initialization arg

我有以下代码:

    public partial class AuditLog : IBusinessEntity
    {
        public BusinessEntityType EntityType { get { return BusinessEntityType.AuditLog; } }

        /// <summary>
        /// Constructor accepting parameter initialization arguments
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="entity"></param>
        /// <param name="command"></param>
        /// <param name="commandText"></param>
        public AuditLog(string userName, BusinessEntityType entity, AuditLogCommand command, string commandText)
        {
            this.Timestamp = DateTime.Now;
            this.UserName = userName;
            this.Entity = entity.ToString();
            this.Command = command.ToString();
            this.CommandText = commandText;
        }
    }
公共部分类审核日志:IBusinessEntity
{
public BusinessEntityType EntityType{get{return BusinessEntityType.AuditLog;}}
/// 
///接受参数初始化参数的构造函数
/// 
/// 
/// 
/// 
/// 
公共审核日志(字符串用户名、BusinessEntityType实体、AuditLogCommand命令、字符串commandText)
{
this.Timestamp=DateTime.Now;
this.UserName=用户名;
this.Entity=Entity.ToString();
this.Command=Command.ToString();
this.CommandText=CommandText;
}
}
这将生成CA2214警告。
BusinessEntityType
AuditLogCommand
方法参数都是枚举。我不知道这里有什么问题,因此不确定如何满足警告


谢谢。

您的一个或多个财产是虚拟的吗?这就是为什么,因为警告

以下是MSDN规则的推理:

当调用虚方法时 执行该方法的实际类型 直到运行时才选择。当 构造函数调用一个虚拟方法,它 构造函数是否可能 调用该方法的实例 没有执行

这意味着,如果有人从您的类继承,并重写在您的构造函数中访问的方法或属性,则重写实现将在继承类的构造函数运行之前命中。如果重写实现依赖于构造函数中的状态集,这可能会导致问题


为了满足警告,您需要将构造函数中访问的属性和方法设置为非虚拟的(如果合适,您可以将类型设置为密封的)。

因为“this.Entity”行有一个调用虚拟方法的setter。我想这就是问题所在。是的,如果静态分析器可以看到在ctor期间调用了任何虚拟方法,则会发出警告。