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?
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.