StyleCop issue needs resolving - contains a call chain that results in a call to a virtual method defined by the class

StackOverflow https://stackoverflow.com/questions/19586133

  •  01-07-2022
  •  | 
  •  

Domanda

I have been cleaning my project files with StyleCop and I have the following I can not resolve

PasswordPolicyCollection.cs (13): CA2214 : Microsoft.Usage : 'PasswordPolicyCollection.PasswordPolicyCollection()' contains a call chain that results in a call to a virtual method defined by the class. Review the following call stack for unintended consequences: PasswordPolicyCollection..ctor() ConfigurationElementCollection.CreateNewElement():ConfigurationElement PasswordPolicyCollection.Add(PasswordPolicy):Void ConfigurationElementCollection.BaseAdd(ConfigurationElement):Void

Here is the code:

using System.Configuration;

public class PasswordPolicyCollection : ConfigurationElementCollection
{
    public PasswordPolicyCollection()
    {
        var passwordPolicy = (PasswordPolicy)this.CreateNewElement();
        this.Add(passwordPolicy);
    }

    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.AddRemoveClearMap;
        }
    }

    public new PasswordPolicy this[string name]
    {
        get
        {
            return (PasswordPolicy)BaseGet(name);
        }
    }

    public PasswordPolicy this[int index]
    {
        get
        {
            return (PasswordPolicy)BaseGet(index);
        }

        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }

            BaseAdd(index, value);
        }
    }

    public void Add(PasswordPolicy passwordPolicy)
    {
        this.BaseAdd(passwordPolicy);
    }

    public int Indexof(PasswordPolicy policy)
    {
        return BaseIndexOf(policy);
    }

    public void Remove(PasswordPolicy url)
    {
        if (BaseIndexOf(url) >= 0)
            BaseRemove(url.Name);
    }

    public void RemoveAt(int index)
    {
        BaseRemoveAt(index);
    }

    public void Remove(string name)
    {
        BaseRemove(name);
    }

    public void Clear()
    {
        BaseClear();
    }

    protected override void BaseAdd(ConfigurationElement policy)
    {
        BaseAdd(policy, false);
    }

    protected override sealed ConfigurationElement CreateNewElement()
    {
        return new PasswordPolicy();
    }

    protected override object GetElementKey(ConfigurationElement policy)
    {
        return ((PasswordPolicy)policy).Name;
    }
}

Any ideas?

È stato utile?

Soluzione

Inside the constructor you call the Add method. Inside this method you call the non sealed BaseAdd method and that's why you get the warning. To get rid of this code analysis warning you have to sealed the BaseAdd method

protected override sealed void BaseAdd(ConfigurationElement policy)
{
    BaseAdd(policy, false);
}

Altri suggerimenti

Well actually it's not a stylecop warning but a Code Analysis (FxCop) warning.

You're calling a virtual method in your constructor (CreateNewElement you have overridden). Generally you shouldn't do this, because as how constructors work in C#, the base constructor (ConfigurationElementCollection.ctor) will be called first, but it'll execute CreateNewElement of the most derived class, i.e. the one you have overridden.

See Virtual member call in a constructor

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top