Method Creation at Build Time
16 posts • Page 1 of 2 • 1, 2
Method Creation at Build Time
Hi,
Is there anyway to use PostSharp as a platform for adding methods to a class at build time?
For example, when I mark a class with a certain attribute, I would like to add a method at build time to it.
It is not important to be able to see the added method at development time (IntelliSense etc.) - Only that the resulting assembly class will include the added method code at run time.
The method created needs to be marked with an attribute as well (if this has any other implications on anyone's answer).
Thanks.
Yoav Sion
- yoavsion
- Posts: 12
- Joined: Sat Jan 12, 2008 4:55 pm
- Full Name: Yoav Sion
Re: Method Creation at Build Time
P.S.
Removing a method is also an option.
The class we are discussing is only loaded by reflection, is not called at compile time by any other method and does not call any other method.
Specifically, I wish to create a base class with many service methods, that are all marked with a specific attribute, and many subclasses. Each of these sub-classes requires a specific set of methods from the base class - The rest of the methods are obsolete.
The methods are recognized by the "loading" class by their attributes (something like the [Test] attribute in NUnit).
Each sub-class is marked with attributes, that define which of the parent class's methods are required for it (The set of required methods changes between each sub-class).
Any ideas would be most welcome.
Yoav Sion
- yoavsion
- Posts: 12
- Joined: Sat Jan 12, 2008 4:55 pm
- Full Name: Yoav Sion
Re: Method Creation at Build Time
Yes, it is possible, since PostSharp allows to edit everything in an assembly. Actually PostSharp Laos adds methods to classes. Removing is not a problem if one carefully removes calls to this method.
Using PostSharp Laos, you can use the CompositionAspect to add whole interfaces.
If you want to do more specific things, you should use PostSharp Core and work directly with the PostSharp.CodeModel namespace.
Using PostSharp Laos, you can use the CompositionAspect to add whole interfaces.
If you want to do more specific things, you should use PostSharp Core and work directly with the PostSharp.CodeModel namespace.
Gael Fraiteur, project leader
got good support? consider donating to the project.
got good support? consider donating to the project.
- gfraiteur
- Site Admin
- Posts: 673
- Joined: Tue Dec 18, 2007 3:09 pm
- Full Name: Gael Fraiteur
- Company: postsharp.org
Re: Method Creation at Build Time
Great!
That was very helpful, and after a few hours of play-time with the Composition sample, I have a few questions and suggestions:
- [*]I have noticed that the Composition sample code uses the type's
FullName property to keep the implementation / interface types. It
would be better if you used the AssemblyQualifiedName - That way, we
will be able to use interfaces and implementations outside the assembly
where the composition attribute is defined.[*]My implementation type has Custom Attributes over some of the implementation methods. These attributes need to be placed over the "added" methods as well in the sub-class implementation, because the loading assembly looks for these attributes. Is there any way of guiding the composition aspect to also copy the implementation method's attributes?
Thank you very much for your quick responses.
Yoav Sion
- yoavsion
- Posts: 12
- Joined: Sat Jan 12, 2008 4:55 pm
- Full Name: Yoav Sion
Re: Method Creation at Build Time
Yeah, sorry -- notifications in this forum does not work perfectly
.
AssemblyQualifiedName: you are right!
Copying CA during composition: no, there is no way. Actually, PostSharp Laos with generate a private implementation of the interface, so I'm not sure it would make sense to copy custom attributes.
Gael
.
AssemblyQualifiedName: you are right!
Copying CA during composition: no, there is no way. Actually, PostSharp Laos with generate a private implementation of the interface, so I'm not sure it would make sense to copy custom attributes.
Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
got good support? consider donating to the project.
- gfraiteur
- Site Admin
- Posts: 673
- Joined: Tue Dec 18, 2007 3:09 pm
- Full Name: Gael Fraiteur
- Company: postsharp.org
Re: Method Creation at Build Time
Is there any way I could enhance the composition attribute in order to achieve this goal?
Copying the "real" implementation method's attributes is crucial for us.
Editing the PostSharp.CodeModel is also an option, if you could direct me to the right area in the code that is related to the solution - If it works, hopefully it will be integrated in some future release.
Thanks,
Yoav.
Yoav Sion
- yoavsion
- Posts: 12
- Joined: Sat Jan 12, 2008 4:55 pm
- Full Name: Yoav Sion
Re: Method Creation at Build Time
Yoav,
I ack it is a good idea, but can I promise you it will *not* be implemented during next 3 months.
You can, if you wish, implement your own variant CompositionAttribute by starting from existing implementation of this attribute.
Remember however to release your work as GPL, LGPL or MPL.
Gael
I ack it is a good idea, but can I promise you it will *not* be implemented during next 3 months.
You can, if you wish, implement your own variant CompositionAttribute by starting from existing implementation of this attribute.
Remember however to release your work as GPL, LGPL or MPL.
Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
got good support? consider donating to the project.
- gfraiteur
- Site Admin
- Posts: 673
- Joined: Tue Dec 18, 2007 3:09 pm
- Full Name: Gael Fraiteur
- Company: postsharp.org
Re: Method Creation at Build Time
Gael,
Thanks for your response.
This is exactly what I'm trying to do. I have started from an existing implementation of the composition attribute, and was left stuck in the part where the composed method's attributes need to be copied into the target method.
If you could, please, direct me to the area in the CompositionAspect attribute, in order to achieve that goal.
Time is not an issue. The project I'm involved in is in it's earliest stages (pre-pre-Alpha, I'd say), so when I will complete this task, I will be more than willing to contribute to the PostSharp community - By then, hopefully, it will be possible to integrate it with an official PostSharp release.
Any guidance will be much appreciated. I have downloaded the PostSharp source-code and have been trying to pin-point the location at which the method is composed into the target type, in order to add the custom attributes from the source implementing type. How to define a custom attribute using PostSharp.Laos is another obscurity I'm trying to handle.
Thank you very much for your patience,
Yoav Sion.
Previously Gael Fraiteur wrote:
Yoav,
I ack it is a good idea, but can I promise you it will *not* be implemented during next 3 months.
You can, if you wish, implement your own variant CompositionAttribute by starting from existing implementation of this attribute.
Remember however to release your work as GPL, LGPL or MPL.
Gael
Yoav Sion
- yoavsion
- Posts: 12
- Joined: Sat Jan 12, 2008 4:55 pm
- Full Name: Yoav Sion
Re: Method Creation at Build Time
I think all you need is to modify the region of the file CompositionAspectWeaver.cs after the comment "Define the new method" (line 516).
You will maybe want to make the method public, so:
newMethod.Name = targetMethodDef.Name;
newMethod.Attributes = MethodAttributes.Public | MethodAttributes.HideBySig |
MethodAttributes.NewSlot | MethodAttributes.Virtual | MethodAttributes.Final;
and if you want to clone custom attributes:
newMethod.CustomAttributes.AddCloneRange(targetMethodDef.CustomAttributes);
I think that's all. Note that you may have naming conflicts in case of implementation of multiple interfaces.
Gael
You will maybe want to make the method public, so:
newMethod.Name = targetMethodDef.Name;
newMethod.Attributes = MethodAttributes.Public | MethodAttributes.HideBySig |
MethodAttributes.NewSlot | MethodAttributes.Virtual | MethodAttributes.Final;
and if you want to clone custom attributes:
newMethod.CustomAttributes.AddCloneRange(targetMethodDef.CustomAttributes);
I think that's all. Note that you may have naming conflicts in case of implementation of multiple interfaces.
Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
got good support? consider donating to the project.
- gfraiteur
- Site Admin
- Posts: 673
- Joined: Tue Dec 18, 2007 3:09 pm
- Full Name: Gael Fraiteur
- Company: postsharp.org
16 posts • Page 1 of 2 • 1, 2