Giter Club home page Giter Club logo

Comments (7)

liuhaoyang avatar liuhaoyang commented on August 29, 2024

欢迎提出问题,可以看到默认是根据MethodInfo类型决定使用call 还是callvirt。我开放了手动传参是因为要满足aop内部有call 虚方法的需求

from aspectcore-framework.

tangdf avatar tangdf commented on August 29, 2024

能举个例子吗,在什么场景下有这样的需求?

from aspectcore-framework.

liuhaoyang avatar liuhaoyang commented on August 29, 2024

用子类的实例调用父类的虚函数(如果在子类里被重写的话),需要用call指令,效果和base.xxx()类似

from aspectcore-framework.

tangdf avatar tangdf commented on August 29, 2024

我测试了,与你说的一致,但是我并不认为这是一个很优雅的设计。

        [Fact]
        public void Call()
        {
            Func<object, object> func = CreateDelegate(OpCodes.Call);

            var result = func.Invoke(new SealedClass());

            Assert.Equal(nameof(AbstractClass), result);
        }


        [Fact]

        public void Callvirt()
        {
            Func<object, object> func = CreateDelegate(OpCodes.Callvirt);

            var result = func.Invoke(new SealedClass());

            Assert.Equal(nameof(SealedClass), result);
        }

        public Func<object, object> CreateDelegate(OpCode opCode)
        {
            Type type = typeof(AbstractClass);

            MethodInfo methodInfo = type.GetMethod("GetName");

            DynamicMethod dynamicMethod =
                new DynamicMethod("invoker_GetName", typeof(object), new Type[] { typeof(object) }, methodInfo.Module, true);

            ILGenerator ilGenerator = dynamicMethod.GetILGenerator();

            ilGenerator.EmitLoadArg(0);
            ilGenerator.Emit(OpCodes.Castclass, type);

            ilGenerator.EmitCall(opCode, methodInfo, null);

            ilGenerator.Emit(OpCodes.Ret);
            return (Func<object, object>) dynamicMethod.CreateDelegate(typeof(Func<object, object>));

        }

    }

    public abstract class AbstractClass
    {
        public virtual String GetName()
        {
            return nameof(AbstractClass);
        }

    }

    public sealed class SealedClass : AbstractClass
    {
        public override String GetName()
        {
            return nameof(SealedClass);
        }

    }

from aspectcore-framework.

liuhaoyang avatar liuhaoyang commented on August 29, 2024

你有更好的方案吗~

from aspectcore-framework.

tangdf avatar tangdf commented on August 29, 2024

既然已经使用IL生成了代理类,那可以直接在IL中解决调用基类方法的问题,而不是在代理类的外部通过反射来调用基类方法。

from aspectcore-framework.

liuhaoyang avatar liuhaoyang commented on August 29, 2024

这样就写死啦。AspectCore的设计里需要解耦基类方法的调用

from aspectcore-framework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.