1

I would like to override all properties with custom method calls using reflection.

The only way is by using PropertyBuilder and ILGenerator? I just found documentation about creating a new assembly and new class, but none about override a property in a defined class.

This is a sample class with int only properties.

public class Foo : MyContext
{
    public int Bar { get; set; }
    public int Baz { get; set; }
}

public class MyContext
{
    public MyContext()
    {
        var props = this.GetType().GetProperties();
        foreach(var p in props)
        {
            //...
        }
    }

    protected void CustomSet(string propName, int value)
    {
        //...
    }

    protected int CustomGet(string propName)
    {
        //...
    }
}

And that's de expected behaviour.

public class FooReflected : MyContext
{
    public int Bar 
    {
        get { return CustomGet("Bar"); }
        set { CustomSet("Bar", value); }
    }

    public int Baz
    {
        get { return CustomGet("Baz"); }
        set { CustomSet("Baz", value); }
    }
}
Max Bündchen
  • 1,283
  • 17
  • 38
  • *The only way is by using PropertyBuilder and ILGenerator* citation or it isn't true – xanatos May 25 '18 at 12:03
  • 1
    In general you'll need to use a post-compilation step to do it, like using Fody, that is a program for modifying a compiled assembly post-compilation. For example, implementing `INotifyPropertyChaged` automtically: https://github.com/Fody/PropertyChanged – xanatos May 25 '18 at 12:04
  • 3
    If you don't own that class source code, and want to do that at runtime - here is a duplicate: https://stackoverflow.com/q/7299097/5311735 – Evk May 25 '18 at 12:06
  • @Evk Wow... Fascinating... I didn't know... Gives "playing with fire" a new meaning... more like "playing with napalm" :-) – xanatos May 25 '18 at 12:08
  • 1
    @xanatos the real thing is a bit buried in ton of answers to that question: https://github.com/pardeike/Harmony – Evk May 25 '18 at 12:15
  • Another alternative - alot slower, though - would be to inherit from `ContextBoundObject` and decorate with a `ProxyAttribute`. You can then capture all method calls (including properties) and even the constructor. – C.Evenhuis May 25 '18 at 13:06
  • @Evk the linked post doesn't work without the debugger attached (tested on VS 2017, .NET 4.7)... And Harmony doesn't have a nuget, and I'm too lazy to compile it myself :-) – xanatos May 27 '18 at 19:09
  • @xanatos linked post has a lot of solutions (not sure which you mean). As for Harmony - you can download it from release page, no compilation: https://github.com/pardeike/Harmony/releases/tag/v1.1.0. I did not test on this specific case, but I did on others and Harmony worked well for me. Here is also one answer with sample usage: https://stackoverflow.com/a/50529922/5311735 – Evk May 27 '18 at 19:27
  • @evk was speaking of the Logman solution... that code works only with debugger on my pc... Microsoft must have changed something in the runtime – xanatos May 27 '18 at 20:18
  • @xanatos well not surprising at all, given the nature of that solution... Harmony is much more reliable (and is actually used in production by several projects, mostly games). – Evk May 27 '18 at 20:19

0 Answers0