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


when declare public event in sealed c++/cli class, code analysis warning ca1047. warning seems come auto-generated protected member functions. how can fix warning?

here's example. 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 question better. 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 don't specify event accessors compiler generate them you. auto-generates add, remove , raise accessors. latter 1 looks when ildasm.exe:

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

the family attribute causes code analysis warning. auto-generated add , remove accessors of course public. writing them questionable workaround, want if have real reason implement custom accessors. boilerplate version 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, suppresses warning. recommend use [suppressmessage] attribute if doesn't spin propeller.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -