CA1047 'Make member raise private, public, or internal' and C++/CLI events

99封情书 提交于 2020-01-06 08:27:09

问题


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


回答1:


I'll document the question better. 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

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:

.method family hidebysig specialname instance void 
        raise_blah(object value0,
                   class [mscorlib]System.EventArgs value1) cil managed
{
    // etc..
}

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:

using namespace System::Runtime::CompilerServices;

ref class Test sealed {
private:
    EventHandler^ foo;
public:
    event EventHandler^ blah {
        [MethodImpl(MethodImplOptions::Synchronized)]
        void add(EventHandler^ d) { foo += d; }
        [MethodImpl(MethodImplOptions::Synchronized)]
        void remove(EventHandler^ d) { foo -= d; }
    private:
        void raise(Object^ sender, EventArgs^ e) { 
            EventHandler^ handler = foo;
            if (handler != nullptr) handler(sender, e);
        };
    }
};

Well, that certainly suppresses the warning. I recommend you use the [SuppressMessage] attribute if that doesn't spin your propeller.



来源:https://stackoverflow.com/questions/4336790/ca1047-make-member-raise-private-public-or-internal-and-c-cli-events

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!