SharpCrafters Forum – Real C# code compilation

SharpCrafters Support Forum

        


Real C# code compilation Expand / Collapse
Author
Message
Posted 3/15/2009 8:56:28 AM


Community Member
Hi!

After struggling a bit with IL development I'd like to say that

I have a dream <!-- s:) --> <!-- s:) -->

One day we'll be coding with Postsharp and will be able to do something like:

 RealCSharpCodeEmitter.EmitBeforePropertySet(
         "if (!Object.Equals("+property.Name+",value)
              Debug.WriteLine(\"Property "+property.Name+" changing\");"


I do not know if it's possible at all to do so, but that'll be great.
Post #3430
Posted 5/4/2009 6:09:29 AM


Community Member
Nixoay-

In fact you do not need PostSharp to compile C# code.

It is build into .Net

See http://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx

However, based on the code you provided, I do not think you need a low-level aspect which require custom weaving.  For your particular example, you should make an OnFieldAccessAspect derived aspect and override the OnSetValue method.  This, among others, is provided in the Laos assembly.  These aspects come with their own weavers, so all you need to do is derive from the abstract aspect class, populate it with the functionality you want to provide, and then apply functionality as desired.  Aspect classes are attributes, so just annotate the properties, or annotate at the class level to catch all the properties.

Please take a look at the XTrace sample, specifically the FieldAccessAttribute class.  This will be a good starting point.  Once you understand this you can look at functionality similar to what I think you're looking for in the PostSharp.Samples.Binding project.

Happy PostSharping!

-Chris
Post #3431
Posted 5/4/2009 7:12:35 AM


Gael Fraiteur

SharpCrafters
I think that what Nixoay means is that he wants to be able to emit C# code from inside PostSharp. This would be useful, indeed, to implement patterns like IClonable.

I have a good idea how to implement that - as usually, just no time and budget <!-- s;) --> <!-- s;) -->.
Post #3432
Posted 5/4/2009 8:04:27 AM


Community Member
Yes, exactly! <!-- s:) --> <!-- s:) -->

IL'ing is a bit painfull <!-- s:) --> <!-- s:) -->
Post #3433
Posted 5/4/2009 8:43:39 PM


Community Member
My bad.  I too am not as strong in the IL department as I'd like.
For now I just use Reflector with the language set to IL.
Post #3434
Posted 5/12/2009 7:56:29 PM


Community Member
I should also note that I found an add-in for Reflector called ReflexIL.

http://sebastien.lebreton.free.fr/reflexil/

The interface contains a means to create IL from C# in Reflector, useful for quickly comparing the generated IL of some bit of C#, as well as modifying the IL directly with useful descriptions of each instruction, as well as instruction composition validation, to ensure you are not trying to issue an operand that is not valid for the instruction, etc.

ldarg.0 - Loads the argument at index 0 onto the evaluation stack.

I find it helpful with my IL needs so I thought I'd share.

-Chris
Post #3435
Posted 5/17/2009 5:03:02 PM


Community Member
Thank you! Really neat tool. <!-- s:) --> <!-- s:) -->
Post #3436
« Prev Topic | Next Topic »


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

Powered By InstantForum.NET v4.1.4 © 2010
Execution: 0.152. 8 queries. Compression Disabled.