Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Say I get a dll from a third party. It only has one method
DisplayMethod(string result)


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
Posted
Updated 26-Aug-17 3:41am

1 solution

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?
 
Share this answer
 
Comments
Member 12658724 26-Aug-17 9:57am    
Yes. I was interviewed onsite a few weeks ago. One guy asked me the question, I was/am confused. Then I asked him in reply: why do you create a new dll to do it?
Dave Kreskowiak 26-Aug-17 10:17am    
You can't add your code to the original .DLL because, 1) it's a LOT of manual work just to poke it in there and 2) strong named assemblies can't be modified without breaking the security around them.

This is as far as I go because I'm not doing your interview homework for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900