|
|
|
Community Member
|
|
Hi Gael, I have been trying to setup an EventDeclaration inside of a weaver but having difficulty setting up its methods: add_xxx and remove_xxx using the MethodSemanticDeclaration.
I created the add/remove methods using two instances of MethodSemanticDeclaration with the appropriate MethodSemantics. When I add the declarations to the event declaration, it throws a NullReferenceException:
at PostSharp.CodeModel.MetadataDeclaration.OnAddingToParent(Element parent, String role)
I think it happens because the "parent" parameter is null.
Can you advise on what I am doing wrong.
Thanks in advance, Pavan
|
|
|
|
|
Gael Fraiteur
SharpCrafters
|
|
A MethodSemantic binds an existing MethodDef to a Property or Event. A MethodSemantic has three properties: the MethodDef, the Property or Event, and the semantic (typically add, remove, set, get).
Here is how to add a getter to a property (supposing that property and getMethod exist and have been already added to their parent type):
property.Members.Add( new MethodSemanticDeclaration( MethodSemantics.Getter, getMethod ) );
Good luck!
-gael
|
|
|
|
|
Community Member
|
|
Gael, The mistake I was making was not adding the methods to the Type before adding the methods to the event declaration.
I am now adding to the type before setting up the methods for the event. However now I have a different error:
Error 2 [ILASM] : syntax error at token ''add_PropertyChanged'' in: .method privatescope void '<return>''add_PropertyChanged' ( class ['System' ]'System.ComponentModel.PropertyChangedEventHandler' 'handler' ) cil managed
Any hints ?
|
|
|
|
|
Gael Fraiteur
SharpCrafters
|
|
|
I guess you did not set up the return parameter properly.
|
|
|
|
|
Community Member
|
|
Gael, You were right about the return parameter, which I have now corrected to include the RetVal attribute. However ILASM still complains with an exit code of 1.
Here is the complete code for setting up the event. Hopefully you will be able to see the problem in this:
typeDef.InterfaceImplementations.Add(T(typeof(INotifyPropertyChanged)));
MethodDefDeclaration addMethod = new MethodDefDeclaration(); typeDef.Methods.Add(addMethod);
addMethod.Name = "add_PropertyChanged"; addMethod.Attributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName; addMethod.ImplementationAttributes = MethodImplAttributes.Synchronized; addMethod.MethodBody = new MethodBodyDeclaration(); addMethod.Parameters.Add(new ParameterDeclaration(0, "handler", T(typeof(PropertyChangedEventHandler)))); addMethod.ReturnParameter = new ParameterDeclaration(-1, "<return>", Project.Module.Cache.GetIntrinsic(IntrinsicType.Void)) {Attributes = ParameterAttributes.Retval};
MethodDefDeclaration remMethod = new MethodDefDeclaration(); typeDef.Methods.Add(remMethod); remMethod.Name = "remove_PropertyChanged"; remMethod.Attributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName; remMethod.ImplementationAttributes = MethodImplAttributes.Synchronized; remMethod.MethodBody = new MethodBodyDeclaration(); remMethod.Parameters.Add(new ParameterDeclaration(0, "handler", T(typeof(PropertyChangedEventHandler)))); remMethod.ReturnParameter = new ParameterDeclaration(-1, "<return>", Project.Module.Cache.GetIntrinsic(IntrinsicType.Void)) { Attributes = ParameterAttributes.Retval };
EventDeclaration eventDecl = new EventDeclaration(); typeDef.Events.Add(eventDecl); eventDecl.Name = "PropertyChanged"; eventDecl.EventType = T(typeof (PropertyChangedEventHandler)); eventDecl.Members.Add(new MethodSemanticDeclaration(MethodSemantics.AddOn, addMethod)); eventDecl.Members.Add(new MethodSemanticDeclaration(MethodSemantics.RemoveOn, remMethod));
|
|
|
|
|
Gael Fraiteur
SharpCrafters
|
|
I don't know; you should find out yourself:
- use the debug build of PostSharp - look at the generated MSIL file - look at full error messages - invoke ILASM manually if you don't see MSIL error messages.
-g
|
|
|
|
|
Community Member
|
|
|
Thanks Gael for your help. I'll debug further as you suggested.
|
|
|
|