Search code examples
c#dependency-injectionioc-containerdynamic-method

Dynamically call method in c#


I've got a lot of entity classes, and now in all properties of the entity classes need to add new functionality (call some method) in the getters and setters. What i want to say looks like this:

public class PersistentClass : BaseClass {
private string attr1;
[Persistent]
public string Attr1 
{
    get
    {
        //need to call here base class method
        //refreshContents();
        return attr1;
    }
    set
    {
        //need to call here base class method
        //refreshContents();
        attr1 = value;
    }
}

private SomeObject attr2;
[Persistent]
public SomeObject Attr2
{
    get
    {
        //need to call here base class method
        //refreshContents();
        return attr2;
    }
    set
    {
        //need to call here base class method
        //refreshContents();
        attr2 = value;
    }
}

private List<SomeOtherObhect> details;
[Persistent]
pubic List<SomeOtherObject> Details
{
    get
    {
        //need to call here base class method
        //refreshContents("details");
        return details;
    }
    set 
    {
        //need to call here base class method
        //refreshContents("details");
        details = value;
    }
}

}

For different types of fields i need to call different methods e.g. refreshContents() and refreshContetns("fieldName"). I'm looking to solve problem with IoC and Dependency Injection. Could you help me please?


Solution

  • This seems like a use case for Aspect Oriented Programming (AOP).

    Here are some links to start with: