Method Cloning
5 posts • Page 1 of 1
Method Cloning
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
--------------------------------------------
Here is the Aspect Attribute Applied to a Property
---------------------------------------------------------
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
...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
- First Name: mike
- Last Name: d
Re: Method Cloning
Mike,
You can achieve something similar using compound aspects. Please look at the sample PostSharp.Samples.Binding.
Gael
You can achieve something similar using compound aspects. Please look at the sample PostSharp.Samples.Binding.
Gael
Gael Fraiteur, project leader
- gfraiteur
- Site Admin
- Posts: 819
- Joined: Tue Dec 18, 2007 3:09 pm
- First Name: Gael
- Last Name: Fraiteur
- Company: Coding Glove
Re: Method Cloning
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
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
- mike
- Posts: 2
- Joined: Mon Apr 28, 2008 7:57 pm
- First Name: mike
- Last Name: d
Re: Method Cloning
Agreed.
PostSharp is to design what Generics are to C#
PostSharp is to design what Generics are to C#
- cjhanson
- Posts: 6
- Joined: Fri May 02, 2008 8:02 pm
- Location: Madison, WI
- First Name: Christopher
- Last Name: J. Hanson
- Company: cjsbox.net
Re: Method Cloning
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
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
- gfraiteur
- Site Admin
- Posts: 819
- Joined: Tue Dec 18, 2007 3:09 pm
- First Name: Gael
- Last Name: Fraiteur
- Company: Coding Glove
5 posts • Page 1 of 1
