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