Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created March 29, 2024 10:20
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 karenpayneoregon/be4aac59c57a652feb31574a5d64e2dc to your computer and use it in GitHub Desktop.
Save karenpayneoregon/be4aac59c57a652feb31574a5d64e2dc to your computer and use it in GitHub Desktop.
Twitter
public interface ISample
{
public void AcceptCallMethod();
void CallerMethod() { }
}
public class Sample : ISample
{
void ISample.AcceptCallMethod()
{
}
public void CallerMethod()
{
}
}
@gr1d99
Copy link

gr1d99 commented Mar 29, 2024

public interface ISample
{
    public void AcceptCallMethod();
    void CallerMethod() { }
}

public class Sample : ISample
{
    void ISample.AcceptCallMethod()
        {
             Console.WriteLine("Called");
        }
        
        public void CallerMethod()
        {
             // 1
             AcceptCallMethod();
             // 2 ** same as above
             this.CallerMethod();
             // 3
             Sample inner = new Sample();
             inner.CallerMethod();
        }
}

// missing the 4th one

@Eagl61
Copy link

Eagl61 commented Mar 29, 2024

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()
    {
    }
}

@VanKrock
Copy link

VanKrock commented Apr 4, 2024

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