-2

I didn't find the answer to my question, but it seems simple. The main big issue is that I bought some library and some functionality hardcoded inside the dll and I can't to recompile that dll without source code. So here is the school level issue:

We have 2 classes A and B

class A {
    public void Method1() {
        this.Method2 ();
    }

    private void Method2() {
        WriteLine  ("A");
    }
}

class B : A {
    private void Method2() {
        WriteLine ("B");
   }
}

If we call 'new B().Method1()', then we have the string line "A". We can't do anything with the class A, but we can change the class B. We should get the string "B".

I've tried to use 'new' modifier, but, as you know, it does not help. The answer "Just override Method1 also" is not the option, cause the real code is much bigger.

Any suggestions?

Mortaly
  • 28
  • 5
  • So you can't change class A and you also can't override A.Method1? – Steven B. May 08 '19 at 19:14
  • 1
    Without being able to modify `A` to make it `protected virtual void Method2()...`, you can't override it. `A` knows nothing about `B` so when you call `A.Method1()` it will always call *its* `Method2`, not a derived classes since they are distinctly different. – Ron Beyer May 08 '19 at 19:16
  • You can't override a private method. If you use `A`, you get what its private methods do. The only thing I can suggest is using a decompiler like Jetbrains DotPeek, which is free. It will decompile the dll and it can even create a project. The source code will look a little bit funny, but you can use it to create a different version of the class or just extract the parts you want to keep. – Scott Hannen May 08 '19 at 19:22
  • Possible duplicate of [Dynamically replace the contents of a C# method?](https://stackoverflow.com/questions/7299097/dynamically-replace-the-contents-of-a-c-sharp-method) – Rhaokiel May 08 '19 at 20:02
  • 1
    Question is not clear and doesn't seems explain real issue – RJN Nov 09 '20 at 07:58
  • @RJN Do you think so? That is exactly the problem I had. The real problem is too complex and I have an NDA around that. The answer below has helped me – Mortaly Feb 17 '21 at 19:51

1 Answers1

1

It sounds to me like you are trying to override Method2 in order to get Method1 to print "B". However, since Method2 is private and non-virtual in class A, this is impossible as defined by C#'s language.

If we can compromise to find a different approach to achieve your desired results, here are some suggestions.

Class composition as a modified boiler plate:

class B
{
    private A;
    public void Method1() {
        this.Method2 ();
    }
    private void Method2() {
        WriteLine("B");
    }
    public void KeptMethod() {
        a.KeptMethod();
    }
}

Reflection to call/modify private members:

typeof(A).GetField("privateValue", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(a, "injected string");
typeof(A).GetMethod("privateMethod", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(a, new object[0]);

I know that both of these have there draw backs and limitations, but since I don't know your actual goal, I'm finding it difficult to be more specific.

Rhaokiel
  • 813
  • 6
  • 17
  • The first way is like full method realization. I have at class A the next: private void Method() { m1(); m2(); m3(); m4(); m5(); ... } but one of this 'm' is not correct in my case. For example, i need to do some other things inside the m3() method. So I would like to override that functionality. Maybe there is any way to relink method calls using the reflection, like typeof(A).GetMethod("Method 2").RebindTo(typeof(B).GetMethod("CustomMethod"))? – Mortaly May 08 '19 at 19:56
  • My first instinct was to say this is also not possible. But lo, I've found somebody else's work that will do what you want: https://stackoverflow.com/questions/7299097/dynamically-replace-the-contents-of-a-c-sharp-method – Rhaokiel May 08 '19 at 20:00
  • Looks like you saved a lot of my time! Thank you a lot. I will try it later and let you know (and will mark as solved if that will work) – Mortaly May 08 '19 at 20:10
  • 1
    So... It actually works. I've tested than in MSVS project and it's ok. Also, I've found a good link, which may help me with the major issue https://github.com/jbevain/cecil . But the current issue is done. Thank you @Rhaokiel ! – Mortaly May 09 '19 at 17:22