C
C#2y ago
Kleidukos

❔ Call base method in multiple inheritance

Example of what i have:
public class A
{
public virtual void Foo()
{
Console.Write("Hello ");
}
}

public class B : A
{
public override void Foo()
{
Console.Write("World");
base.Foo();
}
}

public class C : B
{
public override void Foo()
{
Console.Write("!");
base.Foo();
}
}
public class A
{
public virtual void Foo()
{
Console.Write("Hello ");
}
}

public class B : A
{
public override void Foo()
{
Console.Write("World");
base.Foo();
}
}

public class C : B
{
public override void Foo()
{
Console.Write("!");
base.Foo();
}
}
When i call C#Foo() i get "Hello World!" but i only want to call the base method of A so my console result would be "Hello !" I hope you guys can understand what i want. How can i doing this
35 Replies
Kouhai
Kouhai2y ago
You can't do that
Kleidukos
KleidukosOP2y ago
Okay
Kouhai
Kouhai2y ago
What you basically want is something like base.base.Foo(), right? Unfortunately it's not possible
Kleidukos
KleidukosOP2y ago
Yeah, i mean something like that Maybe with reflection ?
Kouhai
Kouhai2y ago
It's possible with reflection, but at this point the question would be why do that?
cap5lut
cap5lut2y ago
the only thing that comes to mind would be to extract it as protected non virtual method to call it later in C
public class A
{
public virtual void Foo()
{
FooCore();
}

protected FooCore()
{
Console.Write("Hellow");
}
}
// B stays as in ur example
public class C : B
{
public override void Foo()
{
FooCore();
}
}
public class A
{
public virtual void Foo()
{
FooCore();
}

protected FooCore()
{
Console.Write("Hellow");
}
}
// B stays as in ur example
public class C : B
{
public override void Foo()
{
FooCore();
}
}
Kleidukos
KleidukosOP2y ago
I want to override OnClick() from WPF Button which is already overrwriten in a framework i use, but i have to use the button because its the NavigationViewItem from WPF UI Sorry i cant do that because i have no access to the framework
DeliBey
DeliBey2y ago
You need to pass as a parent
Kouhai
Kouhai2y ago
OnClick? Why do you want to override it?
Kleidukos
KleidukosOP2y ago
I dont want to use the WPF UI navigation, i want to use my own, because wpf ui dont support multiple pages of the same type with there same instance
Kouhai
Kouhai2y ago
I guess at this point your only solution is reflection, or extending WPF without the additional framework on top of it
Kleidukos
KleidukosOP2y ago
private void OnClickReflection()
{
var type = typeof(ButtonBase);

const BindingFlags flags = BindingFlags.Instance | BindingFlags.InvokeMethod;

var method = type.GetMethod("OnClick", flags);

method?.Invoke(this, null);
}
private void OnClickReflection()
{
var type = typeof(ButtonBase);

const BindingFlags flags = BindingFlags.Instance | BindingFlags.InvokeMethod;

var method = type.GetMethod("OnClick", flags);

method?.Invoke(this, null);
}
Works perfectly
Aaron
Aaron2y ago
that still invokes it virtually
Kleidukos
KleidukosOP2y ago
What does that mean ?
MODiX
MODiX2y ago
Aaron
REPL Result: Success
public class A
{
public virtual void Test()
{
Console.WriteLine("base class");
}
}

public class B : A
{
public override void Test()
{
Console.WriteLine("derived class");
}
}

typeof(A).GetMethod("Test").Invoke(new B(), null);
public class A
{
public virtual void Test()
{
Console.WriteLine("base class");
}
}

public class B : A
{
public override void Test()
{
Console.WriteLine("derived class");
}
}

typeof(A).GetMethod("Test").Invoke(new B(), null);
Console Output
derived class
derived class
Quoted by
<@270457259065081856> from #void-spam (click here)
Compile: 499.563ms | Execution: 96.666ms | React with ❌ to remove this embed.
Aaron
Aaron2y ago
it still calls the version in the derived class
Kleidukos
KleidukosOP2y ago
But it works I think this is because i use the type of ButtonBase and NavigationViewItem extends from it.
Aaron
Aaron2y ago
I did the same thing there I used A as the type for reflection, which B derives from
Kleidukos
KleidukosOP2y ago
How can i use MODiX ?
Aaron
Aaron2y ago
$eval
MODiX
MODiX2y ago
To compile C# code in Discord, use !eval Example:
!eval for(int i = 0; i < 4; i++) Console.WriteLine(i);
!eval for(int i = 0; i < 4; i++) Console.WriteLine(i);
Please don't try breaking the REPL service. Also, remember to do so in #bot-spam!
MODiX
MODiX2y ago
Kleidukos
REPL Result: Success
public class A
{
public virtual void Foo()
{
Console.Write("Hello ");
}
}

public class B : A
{
public override void Foo()
{
Console.Write("World");
base.Foo();
}
}

public class C : B
{
public override void Foo()
{
Console.Write("!");
}
}

var type = typeof(A);

const BindingFlags flags = BindingFlags.Instance | BindingFlags.InvokeMethod;

var method = type.GetMethod("Foo", flags);

method?.Invoke(new C(), null);
public class A
{
public virtual void Foo()
{
Console.Write("Hello ");
}
}

public class B : A
{
public override void Foo()
{
Console.Write("World");
base.Foo();
}
}

public class C : B
{
public override void Foo()
{
Console.Write("!");
}
}

var type = typeof(A);

const BindingFlags flags = BindingFlags.Instance | BindingFlags.InvokeMethod;

var method = type.GetMethod("Foo", flags);

method?.Invoke(new C(), null);
Compile: 722.838ms | Execution: 98.919ms | React with ❌ to remove this embed.
Kleidukos
KleidukosOP2y ago
Oh, i forgot the result...
Kouhai
Kouhai2y ago
This won't even work, you need BindingFlags.Public as well and as antiwraith said it's still a virtual call I believe you'd need to get a function pointer to it typeof(A).GetMethod("Foo").MethodHandle.GetFunctionPointer(); something like that
Aaron
Aaron2y ago
spinthink
Kouhai
Kouhai2y ago
Need the public bindingflag BindingFlags.Public
Aaron
Aaron2y ago
oh I added Instance again lmao
MODiX
MODiX2y ago
Aaron
REPL Result: Success
public class A
{
public virtual void Foo()
{
Console.WriteLine("Hello ");
}
}

public class B : A
{
public override void Foo()
{
Console.WriteLine("World");
base.Foo();
}
}

public class C : B
{
public override void Foo()
{
Console.WriteLine("!");
}
}

var type = typeof(A);

const BindingFlags flags = BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public;

var method = type.GetMethod("Foo", flags);

method?.Invoke(new C(), null);
public class A
{
public virtual void Foo()
{
Console.WriteLine("Hello ");
}
}

public class B : A
{
public override void Foo()
{
Console.WriteLine("World");
base.Foo();
}
}

public class C : B
{
public override void Foo()
{
Console.WriteLine("!");
}
}

var type = typeof(A);

const BindingFlags flags = BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public;

var method = type.GetMethod("Foo", flags);

method?.Invoke(new C(), null);
Console Output
!
!
Compile: 718.116ms | Execution: 87.091ms | React with ❌ to remove this embed.
Aaron
Aaron2y ago
yeah there
DeliBey
DeliBey2y ago
Are we trying to output "!" ?
Aaron
Aaron2y ago
trying to get Hello
Kleidukos
KleidukosOP2y ago
In my app it works, why not here
Kleidukos
KleidukosOP2y ago
I dont understand why it works in my project (Not the example) Here a screen from my code
Kleidukos
KleidukosOP2y ago
And i checked, method is not null
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?