Created
January 14, 2019 01:17
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: