Domanda

I have this code (sample to reproduce):

public class ObjectBase<T>
{
}
public abstract class ExportBase
{
    public void ExportData<T>(string path, T data, string filename)
                where T : ObjectBase<T>
    {
        // Several verifications on data.
        // Example:
        if (data != null)
        {
            this.InnerExport(this.GetFileName<T>(path, filename), data);
        }
    }
    protected abstract void InnerExport<T>(string path, T data)
                where T : ObjectBase<T>;

    public string GetFileName<T>(string path, string filename)
    {
        // Code.
        return "TEST";
    }
}
internal sealed class XmlExport : ExportBase
{
    protected override void InnerExport<T>(string path, T data)
    {
        // Code.
    }
}

I don't want XmlExport visible (internal in my class library) nor inherits (sealed).

With FxCop 10.0, I've got an CA1047:DoNotDeclareProtectedMembersInSealedTypes:

Name: (FxCopCmd)
Do not declare protected members in sealed types.

Description: (FxCopCmd)
Sealed types cannot be extended, and protected members are only useful if you can extend the declaring type. Sealed types should not declare protected members.

How to fix: (FxCopCmd)
Make member 'XmlExport.InnerExport(string, T)' private, public, or internal (Friend in Visual Basic).

Info: (FxCop)
Sealed types cannot be extended, and protected members are only useful if you can extend the declaring type. Sealed types should not declare protected members.

But I can't change protected to private: virtual or abstract members can't be private. Nor public (does not make sense, here).

I know I can use a SuppressMessage, but I'm wondering if there is a better way (including a modification of the classes).

Thanks.

È stato utile?

Soluzione

This appears to be due to a bug in the rule, triggered by the generic constraint ("where T : ObjectBase") on the base InnerExport method declaration. You should suppress the violation as a false positive. If you're feeling particularly keen, you could also report the bug at https://connect.microsoft.com/visualstudio/.

Altri suggerimenti

You could change protected to internal if you only will use this in the library. But you would also have to change it to internal in XmlExport when you override.

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