Created
March 29, 2024 10:20
-
-
Save karenpayneoregon/be4aac59c57a652feb31574a5d64e2dc to your computer and use it in GitHub Desktop.
Twitter
This file contains 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
public interface ISample | |
{ | |
public void AcceptCallMethod(); | |
void CallerMethod() { } | |
} | |
public class Sample : ISample | |
{ | |
void ISample.AcceptCallMethod() | |
{ | |
} | |
public void CallerMethod() | |
{ | |
} | |
} |
At least like that:
public interface ISample
{
public void AcceptCallMethod();
void CallerMethod()
{
}
}
public class Sample : ISample
{
public void CallerMethod()
{
// 1
((ISample)this).AcceptCallMethod();
// 2
(this as ISample).AcceptCallMethod();
// 3
if (this is ISample sample)
{
sample.AcceptCallMethod();
}
// 4
((ISample)new Sample()).AcceptCallMethod();
// 5
typeof(ISample)
.GetMethod(nameof(ISample.AcceptCallMethod))
.Invoke(this, null);
// 6
var dynamicMethod = new DynamicMethod("CallerOfAcceptCallMethod", typeof(void), [typeof(Sample)], typeof(Sample));
var generator = dynamicMethod.GetILGenerator();
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Callvirt, typeof(ISample).GetMethod(nameof(ISample.AcceptCallMethod)));
generator.Emit(OpCodes.Ret);
var callerOfAcceptCallMethod = (Action<Sample>)dynamicMethod.CreateDelegate(typeof(Action<Sample>));
callerOfAcceptCallMethod(this);
}
void ISample.AcceptCallMethod()
{
}
}
var sample = new Sample();
sample.CallerMethod();
public interface ISample
{
public void AcceptCallMethod();
void CallerMethod();
}
public class Sample : ISample
{
void ISample.AcceptCallMethod()
{
Console.WriteLine("ISample AcceptCallMethod");
}
void AcceptCallMethod()
{
Console.WriteLine("Sample AcceptCallMethod");
}
public void CallerMethod()
{
// 1 reflection
typeof(ISample).GetMethod(nameof(ISample.AcceptCallMethod))?.Invoke(this, null);
// 2 create delegate
var action = Delegate.CreateDelegate(typeof(Action), this,
typeof(ISample).GetMethod(nameof(ISample.AcceptCallMethod)) ?? throw new NotImplementedException());
action.DynamicInvoke();
// 3 expressions
Expression<Action> expression = Expression.Lambda<Action>(Expression
.Call(Expression.Constant(this), typeof(ISample).GetMethod(nameof(ISample.AcceptCallMethod)) ?? throw new NotImplementedException()));
expression.Compile().Invoke();
// 4 IL
var dynamicMethod = new DynamicMethod("DynamicCall", typeof(void), new [] {typeof(Sample)}, typeof(Sample));
var generator = dynamicMethod.GetILGenerator();
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Callvirt, typeof(ISample).GetMethod(nameof(ISample.AcceptCallMethod)) ?? throw new NotImplementedException());
generator.Emit(OpCodes.Ret);
dynamicMethod.CreateDelegate<Action<Sample>>().Invoke(this);
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// missing the 4th one