SharpCrafters Forum – How to use MethodSemanticDeclaration

SharpCrafters Support Forum

        


How to use MethodSemanticDeclaration Expand / Collapse
Author
Message
Posted 5/17/2009 5:09:21 PM


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
Post #496
Posted 5/17/2009 6:00:19 PM


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
Post #497
Posted 5/17/2009 6:50:52 PM


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 ?
Post #498
Posted 5/17/2009 7:06:08 PM


Gael Fraiteur

SharpCrafters
I guess you did not set up the return parameter properly.
Post #499
Posted 5/17/2009 7:41:50 PM


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));
Post #500
Posted 5/17/2009 7:52:22 PM


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
Post #501
Posted 5/17/2009 8:29:51 PM


Community Member
Thanks Gael for your help. I'll debug further as you suggested.
Post #502
« Prev Topic | Next Topic »


All times are GMT +1:00, Time now is 9:55pm

Powered By InstantForum.NET v4.1.4 © 2010
Execution: 0.108. 9 queries. Compression Disabled.