开发者

initialize a class from an existing instance of a base class with minimum amount of code

I would like to create a wrapper class that replaces a specific method or property of an existing class with a minimum amount of code

I could use the decorator pattern but it's not very clean. I would end up having to basically re-declare every method and property in the wrapped class even though I'm only overriding one or two of them and additionally would have to maintain the interface.

I could use extension methods but I want to override an existing method or property so that won't work and even if an extension method could override an existing method, I wouldn't want it to override the method for every instance.

The best I can come up with is something like

    public MyDerivedClass : BaseClass
    {
      MyDerivedClass(BaseClass obj)
      {
       this.x = obj.x; this.y=obj.y etc.....
      }

     public override string BaseMethod()
     {
     }

}

what I really want is something like

    public MyDerivedClass:BaseClass
    {
      MyDerivedClass(BaseClass obj)
 开发者_StackOverflow社区    {
       base = obj;
     }

     public override string BaseMethod()
     {
     }

   }


See the answers to my question. I really liked the RealProxy solution.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜