3

I have some class Foo with many properties:

public class Foo
{
    public int Property1 { get; set; }

    public int Property2 { get; set; }

    public int Property3 { get; set; }
}

In other class I have some method, e.g.

public void SomeMethod()
{
    //...
}

How to inject this method to every setter of properties in the class Foo? I use .Net Framework 2.0

Anton Kandybo
  • 3,678
  • 4
  • 23
  • 31
  • Does this answer your question? [Programmatically insert a method call on each property of a class](https://stackoverflow.com/questions/7569223/programmatically-insert-a-method-call-on-each-property-of-a-class) – StayOnTarget Aug 06 '22 at 21:36

3 Answers3

4

I don't think there is a way to do that at runtime with reflection. What you would probably want to do is use an AOP (aspect-oriented) approach, but that too isn't really supported by the .NET framework. You could use PostSharp to do it, if you don't mind using a compiler extension, or look into using Unity2 to do AOP.

Edit: You could also consider Castle DynamicProxy. Or, if you have a firm grasp of DynamicMethods and IL code, you could make your own proxy generator class.

However, I think in most cases, you will have to code the rest of your application appropriately to handle the proxies. In other words, instead of doing:

Foo f = new Foo();
f.Property1 = 123;

You would have to do something like:

Foo f = Generator.GetProxy<Foo>(); // this code is fake. just indicates that you need to get an instance of Foo from a proxy generator, be it DynamicProxy or Unity or whatever.
f.Property1 = 123;
CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138
  • Postsharp is probably the best solution here unless you want to make structural changes to your application to support this requirement. – tom.dietrich Dec 28 '10 at 15:18
  • @tom - I tend to agree, PostSharp will require the smallest change to already-existing code, since it has the advantage of doing the proxy generation and injection at build-time instead of at run-time. Some people don't like to use a compiler extension though, so I threw in the other options. Side-note; MS should just buy the rights to PostSharp and build it into the standard compiler! :) – CodingWithSpike Dec 28 '10 at 16:04
  • With PostSharp was a problem. It doesn't want to interact with System.Data.SQLite: said that assembly is not compatible. – Anton Kandybo Dec 29 '10 at 12:38
0

You can use an extension of the int class here. Or whatever data type your getter/setter properties are.

For example

public class Foo
{
    public int Property1 { get; set; }    
    public int Property2 { get; set; }    
    public int Property3 { get; set; }
}

The extension method would look like this

public static class IntExtension
{
    public static void SomeMethod(this int property)
    {
        // ...
    }
}

See the following article to use it with .NET 2.0. Requires that you use a VisualStudio that supports C# 3.0 but it will still work with the output framework as C# 2.0

Extension Method in C# 2.0

Community
  • 1
  • 1
Brian
  • 163
  • 1
  • 7
0

I found easy way to do this. I use EasyProp, which uses Castle DynamicProxy:

My class:

[AfterPropertySetFilter(typeof(NotifyPropertyChanged))]
public class Foo : INotifyPropertyChanged
{
    public virtual int Property1 { get; set; }

    public virtual int Property2 { get; set; }

    public virtual int Property3 { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
}

Example of use:

    EasyPropBuilder epb=new EasyPropBuilder();
    Foo foo = epb.Build<Foo>();
    foo.Property1 = 1;
    foo.PropertyChanged += OnPropertyChanged;
    foo.Property1 = 2;

Also you need to add such method:

public static void OnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
{
    Console.WriteLine("Property Changed: " + propertyChangedEventArgs.PropertyName);
}
Anton Kandybo
  • 3,678
  • 4
  • 23
  • 31