0

Let say I have the following classes,

 Public Class Base

      Public Event PropertyChanged()

      Sub New()
           .....
      End Sub
 End Class

 Public Class Other
      Inherits Base

      Public Property X As String
 End Class

If I wanted to say, alter the code definition of the automatic property X (at runtime, perhaps in Sub New), such that I could throw the PropertyChanged event, how could I go about doing that?

Edit: Its seems that the Postsharp is an available tool that does the functionality I'm exploring, but I wanted my question to be more academic than practical. So, how could I implement the code injection at compile time or runtime myself?

Kratz
  • 4,280
  • 3
  • 32
  • 55
  • I don't think you can use automatic properties if you need to call the PropertyChanged event from it. – LarsTech May 15 '12 at 20:15
  • @LarsTech, since the compiler generates the code for the property I would have to somehow alter the code at runtime to do it. I assume what the compiler creates would be the same as if I typed out an explicit property definition. An example of what I'm trying to get at might be adding the event to all properties in all classes that inherit `Base` without changing the actual source code of the inheriting classes. – Kratz May 15 '12 at 20:20
  • Not sure I quite understand. Instead of an automatic property, I think you have to write out the full property with Get and Set methods and in the Set method, you would `RaiseEvent PropertyChanged()`. – LarsTech May 15 '12 at 20:31
  • I'm not talking modifying the source code, I wan't to compile the property as is, and using reflection and built in .Net code generation, add the code to the property at runtime, via the base class. – Kratz May 15 '12 at 20:32
  • This is similar to http://stackoverflow.com/questions/4330368/c-sharp-property-override-set-method – Mike Guthrie May 15 '12 at 20:40

2 Answers2

2

I guess you are looking for AOP. A very nice technology that IMHO isn't mature on the dotnet platform. I believe, correct me if I am wrong, that Postsharp is the best known AOP framework. It isn't gratis though for production; installing and playing (and possibly F/OSS) is gratis. Also check this post for more info.

Community
  • 1
  • 1
LosManos
  • 7,195
  • 6
  • 56
  • 107
  • Good point, I hadn't read anything about AOP before. Thanks for the Postsharp suggestion, but i guess my question was more academic than practical. – Kratz May 16 '12 at 13:00
2

The answer is simple: you can't. Once a type is loaded, you can't change its code.

If you want to implement INotifyPropertyChanged without writing the same code for each property, there are several ways.

One of them is making the property MustOverride (abstract in C#) and then creating another class at runtime which implements it at runtime (for example using Castle DynamicProxy).

Another one is using AOP to rewrite the code after compilation (but before it's run) using something like PostSharp.

Also have a look at Implementing INotifyPropertyChanged - does a better way exist?, for an overview of other options.

Community
  • 1
  • 1
svick
  • 236,525
  • 50
  • 385
  • 514