通过给定DLL创建扩展方法 [英] Create an extension method by given a DLL

查看:74
本文介绍了通过给定DLL创建扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我从第三方获得了一个dll。它只有一个方法

Say I get a dll from a third party. It only has one method

DisplayMethod(string result)





现在我想添加一个新方法 PrintMethod(字符串结果)但它无法获取dll的源代码。

所以我想我必须为它生成一个扩展方法。但是怎么样?



我尝试了什么:



我知道怎么做从头开始创建扩展方法。但不确定如何通过给定的dll来做到这一点。需要代码示例。谢谢



Now I want to add a new method PrintMethod(string result)to it but I can't get the dll's source code.
So I guess that I have to generate an extension method to it. But how?

What I have tried:

I know how to create an extension method from the scratch. But not sure how to do it by given dll. Need code example. Thanks

推荐答案

扩展方法适用于类型,而不是.DLL。扩展添加适用于类型的功能,如整数或字符串。



没有必要在.DLL中添加扩展名。您的代码无法将其转换为.DLL文件本身,因此您只需创建一个新的.DLL,它将与文件系统中现有的一致.DLL。你刚刚创建了一个新的类库项目,它将编译成另一个.DLL。



现在,这个新方法会做什么?您是否尝试更换DisplayMethod功能?原始方法是否可以覆盖?
Extension methods apply to types, not .DLL's. Extensions add functionality applicable to a type, like an integer or string.

There is no point to adding an "extension" to a .DLL. Your code cannot make it into the .DLL file itself, so you're just making a new .DLL that would be along side of the existing one in the file system. You're just making a new class library project which will compile into another .DLL.

Now, what would this new method do? Are you trying to replace the DisplayMethod functionality? Is the original method overridable?


这篇关于通过给定DLL创建扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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