Tango_Chaser
Tango_Chaser

Reputation: 331

Is it possible to add a method/interface to a referenced DLL in C#?

Coding in C# with Visual Studio,

Is it possible to add a method or an interface to a referenced DLL that I added to my project?

Or the only way is to return to the source of the DLL, add the data & create a new DLL?

Upvotes: 0

Views: 1421

Answers (1)

Zein Makki
Zein Makki

Reputation: 30042

1- No you can't add anything to a referenced dll.

2- Yes, you need to update the source for that dll and generate a new copy.

if the dll is a class library in the same solution, you just need to add the method/interface and Rebuild.

Extension Methods:

You can always add an Extension Method to a class that you have no control over, whether it was a core .NET class or a referenced dll. Below is an example adding a method to the String class:

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ', '.', '?' }, 
                             StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }   
}

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

Upvotes: 2

Related Questions