Why does relection of event have SpecialName attribute true?
-
Monday, September 10, 2012 6:46 PM
I am using C++/CL to interface between managed and un-managed code, but have narrowed down my problem to a minimum code using just a C++/CLR class library (dll) and C# program that uses reflection on this dll.
The C++/CLR test code looks like:
namespace Interop { public delegate void testDelegate(); public ref class DelegateTest { public: static event testDelegate^ TestDelegate { void add(testDelegate^ value) { } void remove(testDelegate^ value) { } }; };The C# test program has a single button to test this (after including a reference to the C++/CLR dll).
private void button1_Click(object sender, EventArgs e) { Type type = typeof(Interop.DelegateTest); EventInfo[] events = type.GetEvents(BindingFlags.Static | BindingFlags.Public); for (int i = 0; i < events.Length; i++) { EventInfo eventInfo = events[i]; bool IsSpecialName = eventInfo.IsSpecialName; textBox1.Text = IsSpecialName.ToString(); } }This always shows that that SpecialName is set to true.
This attribute is not set if the dll is created in C#, and also not true for non-event methods using the C++/CLR.
Why is this? Is there any compiler setting or something else to not have this attribute set?
I am using Express version 2010 for both C++/CLR and C# and targeting .Net 3.5 for both, and x64 platform in C++/CLR and AnyCPU in C#.
- Edited by litdevMicrosoft Community Contributor Monday, September 10, 2012 6:59 PM typo
All Replies
-
Monday, September 10, 2012 7:19 PM
"Why is this?"
The CLR specification says that the add/remove methods shall have the special name attribute set. However it doesn't say anything about the event itself, the special name attribute is not required nor disallowed on events.
"Is there any compiler setting or something else to not have this attribute set?"
Nope. The best thing to do is not to depend on this attribute because the specification doesn't give it a precise definitions.
- Marked As Answer by litdevMicrosoft Community Contributor Monday, September 10, 2012 7:26 PM
-
Monday, September 10, 2012 7:26 PM
Thanks,
The issue for me is that the app that does the reflection (which I don't don't have control over) does test if this attribute is set and fails if it is.
I guess I can create the event in C# dll and pass through C++/CLR dll then to native C++. I was hoping to not have the extra C# dll layer.
-
Monday, September 10, 2012 7:38 PM
"The issue for me is that the app that does the reflection (which I don't don't have control over) does test if this attribute is set and fails if it is."
That's weird, normally only methods have special names: .ctor, add_x, remove_x, get_x, set_x, op_x...
"I guess I can create the event in C# dll and pass through C++/CLR dll then to native C++. I was hoping to not have the extra C# dll layer."
Another solution would be to use tools like ccimetadata or ildasm/ilasm to 'fix' those C++ events.
-
Monday, September 10, 2012 7:53 PM
The app that does the reflection has code like:
private bool CanAddEvent(EventInfo eventInfo) { return !eventInfo.IsSpecialName; }These tools are new to me, I will look into them (fun to learn new things). I usually like the simplest approach which may still be a C# dll layer (added advantage - I get intellisense).
Cheers.

