C
C#11mo 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
Kouhai11mo ago
You can't do that
Kleidukos
Kleidukos11mo ago
Okay
Kouhai
Kouhai11mo ago
What you basically want is something like base.base.Foo(), right? Unfortunately it's not possible
Kleidukos
Kleidukos11mo ago
Yeah, i mean something like that Maybe with reflection ?
Kouhai
Kouhai11mo ago
It's possible with reflection, but at this point the question would be why do that?
cap5lut
cap5lut11mo 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
Kleidukos11mo 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
Servator
Servator11mo ago
You need to pass as a parent
Kouhai
Kouhai11mo ago
OnClick? Why do you want to override it?
Kleidukos
Kleidukos11mo 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
Kouhai11mo ago
I guess at this point your only solution is reflection, or extending WPF without the additional framework on top of it
Kleidukos
Kleidukos11mo 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
Aaron11mo ago
that still invokes it virtually
Kleidukos
Kleidukos11mo ago
What does that mean ?
MODiX
MODiX11mo 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
Aaron11mo ago
it still calls the version in the derived class
Kleidukos
Kleidukos11mo ago
But it works I think this is because i use the type of ButtonBase and NavigationViewItem extends from it.
Aaron
Aaron11mo ago
I did the same thing there I used A as the type for reflection, which B derives from
Kleidukos
Kleidukos11mo ago
How can i use MODiX ?
Aaron
Aaron11mo ago
$eval
MODiX
MODiX11mo 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
MODiX11mo 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
Kleidukos11mo ago
Oh, i forgot the result...
Kouhai
Kouhai11mo 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
Aaron11mo ago
spinthink
Kouhai
Kouhai11mo ago
Need the public bindingflag BindingFlags.Public
Aaron
Aaron11mo ago
oh I added Instance again lmao
MODiX
MODiX11mo 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
Aaron11mo ago
yeah there
Servator
Servator11mo ago
Are we trying to output "!" ?
Aaron
Aaron11mo ago
trying to get Hello
Kleidukos
Kleidukos11mo ago
In my app it works, why not here
Kleidukos
Kleidukos11mo ago
I dont understand why it works in my project (Not the example) Here a screen from my code
Kleidukos
Kleidukos11mo ago
And i checked, method is not null
Accord
Accord11mo 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.