用单独的程序集扩展C#代码 [英] Extending c# code with separate assembly

查看:56
本文介绍了用单独的程序集扩展C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要添加一些额外的功能,其中包括向对象添加新属性以及在处理该对象的另一个类中扩展方法.我正在处理产品的源代码(在C#2.0中),我们使用的是我真的不想修改的产品,我只是想对其进行扩展.

I have some extra functionality i need to add which includes adding a new property to an object and extending methods in another class that handles this object. I'm dealing with source code of a product (in C# 2.0) we use which i really don't want to modify, i just want to extend on it.

理想情况下,我希望有一个单独的程序集来执行此操作,以便清楚地知道这是我们的代码.似乎部分类和委托可能是要走的路,但是我不确定是否可以这样做.有人做过类似的事情吗?或知道任何好的文章?

I'd ideally like to have a separate assembly to do this so that it's clear it's our code. It seems like partial classes and delegates may be the way to go, but I'm not sure if this is possible to do. Has anyone done something similar? or know any good articles?

推荐答案

查看装饰器模式.这正是您想要的.您可以继承现有的类,并使用任何其他逻辑对其进行扩展.如果您想隐藏一些基本功能,还可以使用代理模式.

Check out the Decorator pattern. This is exactly what you are looking for. You can inherit the exiting classes and extend it with any additional logic you have. There's also a Proxy pattern if you want hiding some base functionality.

这篇关于用单独的程序集扩展C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆