When I declare a public event in a sealed C++/CLI class, I get Code Analysis warning CA1047. The warning seems to come from auto-generated protected member functions. How can I fix this warning?
Here's an example. This code
ref class Test sealed {
public:
event EventHandler^ blah;
};
generates:
warning: CA1047 : Microsoft.Design : Make member 'Test::blah::raise(Object^, EventArgs^)' private, public, or internal
I'll document the question better. This code
generates:
Yes, when you don't specify the event accessors yourself then the compiler will generate them for you. It auto-generates the add, remove and raise accessors. The latter one looks like this when you look with ildasm.exe:
The family attribute is what causes the code analysis warning. The auto-generated add and remove accessors are of course public. Writing them yourself is a questionable workaround, you really only want to do this if you have a real reason to implement custom accessors. The boilerplate version would look like this:
Well, that certainly suppresses the warning. I recommend you use the [SuppressMessage] attribute if that doesn't spin your propeller.