Skip to content

Instantly share code, notes, and snippets.

@tugberkugurlu
Created January 14, 2019 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tugberkugurlu/8e1c78f162041bc514cd489a92032bb1 to your computer and use it in GitHub Desktop.
Save tugberkugurlu/8e1c78f162041bc514cd489a92032bb1 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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