Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Call private methods on implementation of the abstract class. This could be handy for the abstract Aggregate class which can get the event stream and populate the aggregate.
using System;
using System.Linq;
using System.Reflection;
namespace abstract_impl_reflect
{
public class Foo : FooBase
{
public Foo() : base()
{
Console.WriteLine("Foo ctor...");
Console.WriteLine(this.GetType().FullName);
}
private void CallMe()
{
Console.WriteLine("Boom!");
}
}
public abstract class FooBase
{
public FooBase()
{
Console.WriteLine("FooBase ctor...");
Console.WriteLine(this.GetType().FullName);
var method = this.GetType().GetMethod("CallMe", BindingFlags.NonPublic | BindingFlags.Instance);
Console.WriteLine(method.Name);
method.Invoke(this, null);
}
}
class Program
{
static void Main(string[] args)
{
var foo = new Foo();
}
}
}
@tugberkugurlu

This comment has been minimized.

Copy link
Owner Author

commented Jan 14, 2019

Output:

➜  abstract-impl-reflect dotnet run
FooBase ctor...
abstract_impl_reflect.Foo
CallMe
Boom!
Foo ctor...
abstract_impl_reflect.Foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.