SharpCrafters Forum – Plugin without Runtime-References

SharpCrafters Support Forum

        


Plugin without Runtime-References Expand / Collapse
Author
Message
Posted 12/15/2009 5:39:52 PM


Community Member
Hello,

I'd like to ask if it's possible (with PostSharp 2.0) to implement a PlugIn which just uses the SDK in the Weaver (on the Dev-Boxes), but in a way that the custom Attributes used must not derive from a PostSharp base-class or implement PostSharp interfaces?

Basically I would like to archive that the final Assemblies don't need to load and reference the PostSharp Assembly, and all the work is done in the PlugIn (on IL Basis).


Thanks!
GP
Post #653
Posted 12/17/2009 7:03:54 AM


Gael Fraiteur

SharpCrafters
Yes, for sure.
The only question you have to solve then is how PostSharp will know it has to kick in. Practically, it means you will have a PostSharp project file (*.psproj) with the same name than your C# project file in your C# project. And this PostSharp project file should include a reference to your plug-in and your task.
Post #654
Posted 12/17/2009 10:07:59 AM


Community Member
Hello,

thank you for your response.

I've already managed to get my PlugIn called:
[list]
[*]Created a Plug-In Assembly with a Plug-In Class[/*:m]
[*]Created the corresponding .psplugin file[/*:m]
[*]Put the [RequirePostSharp(...)] Attribute on my "Root-Attribute".[/*:m]
[*]Edited the .csproj File of the Application and included a PropertyGroup for the PostSharpSearchPath[/*:m][/list:u]

Where I have a problem is in the Code of the Plug-In. In the Samples (e.g. Librian), the PlugIn (in Weaver) uses "AddAspectWeaverFactory<AspectClass, AspectWeaver>. I first thought that the ImplementValidableAspectWeaver is the way to go, but then I've seen that the "FieldValidationAttribute" also has a Post-Sharp Interface and base-Class.

So the question is:
What to I have to call from the Plug-In so that I can implement something like:



// get a reference to the assembly and transform the IL and Metadata
void ProcessAssembly(assembly) {

foreach(var type in assembly.AllTypes){
 if(type has not AttributeX) continue;
 foreach(var method in type.Methods){
    MethodBodyDeclaration methodBody = ....:
    InstructionBlock ....
 }
}

}


Thank you very much.
Günter
Post #655
Posted 12/17/2009 12:04:39 PM


Gael Fraiteur

SharpCrafters
I would use the AnnotationRepositotyTask to get a list of types annotated by your custom attribute, then iterates methods.

You'll need to remove the custom attribute and the reference to PostSharp.*.dll.
Post #656
Posted 12/17/2009 1:49:35 PM


Community Member
Hello again,

thank you very much. I just got it work so far (have emitted fields and methods). But want I can't get to work is how I add blocks to existing methods. Everything I've tried so far eigher throws a NullRef, or an PostSharp Exception (related to the LinkedList), or the generated Method has no body at all ;-( I've also checked the samples but there seems to be no example of modifing existing methods.

Can you answer me how I would implement just to insert some nop's in a method (before and after the original code).

I've also tried MethodBodyRestructurer, but without success. Can you point me to any docs operating at this level?


Thanks very very much. I love PostSharp!
Günter
Post #657
Posted 12/17/2009 4:37:29 PM


Community Member
By using the <!-- m --><a class="postlink" href="http://labs.dropit.se/blogs/post/2009/10/14/Introduction-to-PostSharp-Part-2.aspx">http://labs.dropit.se/blogs/post/2009/1 ... art-2.aspx</a><!-- m --> Blog-Entry I've finally managed it. Thanks so far. I really learned a lot from this.

One last conceptual question:
What is the base way for Advices to communicate with each other? e.g. I have an Advice on Type-Level (weaver.AddTypeLevelAdvice) and an advice for Property-Getter Methods.

Are the Advices called in the order they are added to the Weaver so that I can pass objects around the instances? Or are there other methods?

Günter
Post #658
Posted 12/17/2009 4:50:29 PM


Gael Fraiteur

SharpCrafters
There's no recommendation on how advices should communicate. Basically, advices are plain objects so you can store references to other advices.

Advices are not guaranteed to be executed in any order (at least in 1.5).

-g
Post #659
« Prev Topic | Next Topic »


All times are GMT +1:00, Time now is 11:41am

Powered By InstantForum.NET v4.1.4 © 2010
Execution: 0.066. 7 queries. Compression Disabled.