Interviews - C# Corner

Prasad Raveendran
Create an extension method using a given third-party DLL. Say, I get a dll from a third party and it has only one method. Now, I want to add a new method to it but I can't get the dll's source code. How?
By Prasad Raveendran in C# onApr 26 2019
  • Kalyan Mondal
    Jul, 2019 16

    Yes you can, that is the only reason we have extention method.
    I have added a third party dll — here is the command
    “Install-Package Calculator -Version 1.0.0.1”

    That has only one method “Add”

    I have added DevideBy10 as extention method.

    See this below code.

    1. using System;
    2. using Tranform.Try;
    3. namespace test
    4. {
    5. static class Test
    6. {
    7. static Calculator calculator = new Calculator();
    8. public static string DevideBy10 (this Calculator calc, int number)
    9. {
    10. return "= " +(number/10).ToString();
    11. }
    12. public static void Main()
    13. {
    14. Console.WriteLine(calculator.Add(5, 5));
    15. Console.WriteLine(calculator.DevideBy10(50));
    16. Console.Read();
    17. }
    18. }
    19. }

    • 3
  • Ramana Kumar
    Jun, 2019 8

    Extension methods are additional custom methods which were originally not included with the class. It is a mechanism of adding methods into an existing class or structure also without modifying the source code of original type. Below is an example adding a method to the int class:namespace ExtensionMethods {public static class IntExtensions{public static bool IsGreaterThan(this int i, int value){return i > value;}} }

    • 3
  • Prasad Raveendran
    Apr, 2019 26

    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;}} }Note: Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

    • 3
  • Ishoo Anyal
    Oct, 2019 1

    You cannot get the DLLs code unless you are using Code reflector.
    Well if dll has only one method and you want to add more methods to it then why not inherit the dll class in your own class and then do whatever you want in your class. If not then extension methods are the best

    1. public static class Extension
    2. {
    3. public static void CallBy(this Car obj1)
    4. {
    5. //You logic here;
    6. }
    7. }
    • This Car is class in a dll
    • Extension methods are always static
      Now I can use it my program like

      1. static void Main(string[] args)
      2. {
      3. CrossProjectDemo.Car obj = new Car();
      4. obj.AsParallel();
      5. obj.CallBy();
      6. }

      As simple as that.

    • There are lot of extension methods in .Net framework. AsParallel() is on of them.

    • Just go to ParallelEnumerable and you’ll find so many extension methods

    Hope this helps you.

    • 0
  • Cindy Nelson
    Sep, 2019 9

    Add using ExtensionMethods.Extensions.Basic;to the name space. Then apply the extended method to a precompiled class

    • 0

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS