是否可以覆盖dll文件中的类中的方法? [英] Is it possible to override a method in a class in a dll file?

查看:110
本文介绍了是否可以覆盖dll文件中的类中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows窗体项目中包含了一个dll文件. 1.是否有可能完全覆盖特定的类? 2.是否可以向dll文件中的特定类添加新方法? 3.是否可以覆盖dll中的类中的方法?

I've included a dll file into my windows form project. 1. Is it possible to override a particular class entirely? 2. Is it possible to add a new method to a particular class in the dll file? 3. Is it possible to override a method in a class in the dll?

替代方案我希望避免: 我知道我可以使用扩展方法来创建静态新方法. 我还可以从特定的类继承,然后将新方法添加到派生类.

Alternatives I would prefer to avoid: I know I can use extension methods to create static new methods. I can also inherit from a particular class and then add new methods to the derived class.

我要实现的目标: 我现在必须创建一个项目,并将其作为dll文件添加到更大的项目中. 但是我们被告知,下个月我们需要为原始项目添加更多功能.我正在尝试找出解决此问题的最佳方法. 较小的项目基于mvc设计.

What i'm trying to achieve: i have to create a project now and add it to a larger project as a dll file. but we've been told that we'll need to add more functionality to the original project next month. I'm trying to figure out the best way to go about this. the smaller project is based on mvc design.

推荐答案

您当然可以覆盖从类库中的类继承的 virtual 方法.例如,只要您覆盖object.ToString(),就可以执行此操作!

You can certainly override a virtual method which you're inheriting from a class in a class library. You do this any time you override object.ToString(), for example!

但是,您不能完全覆盖类"-我什至不知道那是什么意思.同样,您不能将方法添加到现有的类中,尽管您可以:

You can't "override a class entirely" though - I don't know what that would even mean. Likewise you can't add a method to an existing class although you can:

  • 使用扩展方法假装"添加其他方法(但没有状态,没有属性)
  • 从类派生(假设它不是密封的)并声明自己的额外方法

如果您可以告诉我们您要达到的目标,那么就建议如何进行将更容易.

If you could tell us what you're trying to achieve, it would be easier to advise you on how to proceed.

当您需要向原始项目中添加更多功能时,只需将其添加到原始项目中即可.有什么原因使您以后无法更改该项目?

When you need to add more functionality to the original project, just add it to the original project. Is there any reason you wouldn't be able to change that project later?

这篇关于是否可以覆盖dll文件中的类中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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