1

I have a c# dll from a supplier. I have extracted the source code, but it's not an option to modify it myself.

Supose the dll has these classes (in the same namespace):

public static class A {
   public static string Method1(this Helper helper, B options) {
      ...
   }
}

public class B {
   public int LoadingElementDuration {
      get;
      set;
   }

   public string Method2() {
      ...
   }
}

Class A is all fine. This the class that is used by us. Is it possible to modify class B? I want to add a property and override Method2 with my own code. Class A should then use my code instead of the default class B.

Thanks.

1
  • Take a look at the adapter-pattern.
    – Tomtom
    Aug 17, 2012 at 9:24

6 Answers 6

3

No, that's not possible because Method2 is sealed. You could have written a derived class from B and added a new property but you cannot override Method2. This would have been possible if Method2 was virtual.

1
  • 2
    Of course that it is sealed. class B doesn't implement any interface and the method is not marked with the virtual keyword. Aug 17, 2012 at 7:10
1

You can copy all contents of this dll and paste it to a new created class library with the same class names. Then you can modify it as you want.

0

No, you will only be able to extend the functionality and not change the existing.

0

If the ClassA->Method1 is called from you code, all you need to do is extend ClassA and ClassB and add the methods that you need.

1
  • no, he said that he is using Class A. Class A is all fine. This the class that is used by us
    – shajivk
    Aug 17, 2012 at 7:22
0

Why are this people saying that it is impossible? Am I missing something? Make a derived class, add property, and use "new" to override method

public class C : B
{
    //add property here

    public new string Method2()
    {

    }
}

So let's take a look what we are getting here:

If you don't need to use added property in Method1 (this moment isn't so clear from your question):

C options = new C();
//init here
helper.Method1(options); //this will cast your object of C class to object of B class and your added property will be inaccessible.

But if you need to change implementation of Method1 to use your property you could try to do this way:

public static class CHelper
{
     public static string Method1(this Helper helper, C options)
     {

     }
}

and write your own implementation of Method1

5
  • 1
    And Method1 won't call that because he accepts B as an argument and will ignore this pseudo-override.
    – Iarek
    Aug 17, 2012 at 7:09
  • How will it stop him from writing another extension method that using instance of his class as options?
    – dantix
    Aug 17, 2012 at 7:17
  • Well, as far as I see OP wants class A to use his version of class B, without actually implementing all that code on his own.
    – Iarek
    Aug 17, 2012 at 7:19
  • I don't know all background, OP doesn't show it, but if you adding some property - you want to use it. If this property is going to be used in Method2 - there is no need to change Method1. If not - he should write his own implementation that using this new property, otherwise I don't see need to add it.
    – dantix
    Aug 17, 2012 at 7:26
  • It's maybe beter to describe to backgound. the dll belongs to asp.net mvc. class A is AjaxExtensions and class B is AjaxOptions. I want to add a property to the ajaxoptions: private string _updateTargetSelector; then I want to override the method 'ToUnobtrusiveHtmlAttributes'. So I ultimately can call AjaxLink with my new ajaxoption
    – Bieters
    Aug 17, 2012 at 8:14
-1

Create a new Class, inherited from ClassB, create a new Method2 with key word new, that's it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.