开发者

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?


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

Here are some links to start with:

  • Introduction to Aspect Oriented Programming
  • And some examples


What will IoC or DI do in your case?

i think you just want a generic method in base class and call thios method from getters or setters


If you want to keep your refresh logic at one place, and call it differently from your properties, consider implementing INotifyPropertyChanged and handling the PropertyChanged event in your entity class itself and implement the refresh logic there.

However, More important question is why do you want to refresh the contents when a property is set or get? It might help to understand if you provide some real property names.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜