Method Cloning

Do you program directly PostSharp.CodeModel or another low-level API and need help? Put your questions here and we will try to answer.

Method Cloning

Postby mike on Mon Apr 28, 2008 8:22 pm

I have just started working with postsharp, and really have a whole new way of thinking about code, thanks for the great work.

...on to the question...

Since IL is tricky shouldnt you be able to create a dummy method then copy the IL from that method in postsharp and emit it into a method or property. Here is an example.

CLONE used to grab the IL we will emit
--------------------------------------------
Code: Select all
public class Clone<T> : INotifyPropertyChanged where T : class
{
   public T PropertyName
   {
      get
      {
         return this._propertyName;
      }
      set
      {
         if (this._propertyName == value) return;
         this.OnChange("PropertyName");
      }
   }
   private T _propertyName;

   public void OnChange(string name)
   {
      if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(name));
   }

   public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
}



Here is the Aspect Attribute Applied to a Property
---------------------------------------------------------
Code: Select all
public class Person
{
   [INotifyPropertyChangedAspect]
   public string Name { get; set; }
}



I realize this is a basic example and the prototype or code to decomile and clone the IL may have to be broken into Get_ Set_ methods or have some other decorations, but this would seem to be a way to make emitting IL simpler, and perhaphs a facility already exists for this.

Any thoughts would be appreciated...

In the mean time, I am having a ball with my first Aspect, jumped right in with the test example as I want total control, and it actually took only about an hour to get it working.

Thanks,

Mike
mike
 
Posts: 2
Joined: Mon Apr 28, 2008 7:57 pm
Full Name: mike d

Re: Method Cloning

Postby gfraiteur on Mon Apr 28, 2008 9:10 pm

Mike,

You can achieve something similar using compound aspects. Please look at the sample PostSharp.Samples.Binding.

Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 674
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Method Cloning

Postby mike on Mon Apr 28, 2008 9:55 pm

Gael,

Thanks for the prompt response...

I looked at the bindings example.

I doing all of the weaving at the lo level, so when decompiled in reflector you have pure IL code without the Laos stuff, just like the trace sample.

One approach would be to write a real implementation, decompile and get the IL from reflector, I think thats a common practice when weaving. My though if possible would be to just supply a sample(prototype) method in the Task / Attribute / or even the class where you apply the AOP attribute.

Then read the IL make a couple of adjustments and write it back. When I started coding a sample, generics saved the type changes and it looked like you would be left with just updating the storage and property name.

I am loving POSTSHARP!!! It is so cool 8-)
mike
 
Posts: 2
Joined: Mon Apr 28, 2008 7:57 pm
Full Name: mike d

Re: Method Cloning

Postby cjhanson on Fri May 16, 2008 8:59 pm

Agreed.

PostSharp is to design what Generics are to C#
cjhanson
 
Posts: 6
Joined: Fri May 02, 2008 8:02 pm
Location: Madison, WI
Full Name: Christopher J. Hanson
Company: cjsbox.net

Re: Method Cloning

Postby gfraiteur on Sun May 18, 2008 5:19 pm

Sorry - I forgot to follow up this topic.

Cloning a method is indeed a little tricky, but of course perfectly possible. The problem is that I don't find a case where it is useful...

Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 674
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org


Return to PostSharp Core