Comments (7)
欢迎提出问题,可以看到默认是根据MethodInfo类型决定使用call 还是callvirt。我开放了手动传参是因为要满足aop内部有call 虚方法的需求
from aspectcore-framework.
能举个例子吗,在什么场景下有这样的需求?
from aspectcore-framework.
用子类的实例调用父类的虚函数(如果在子类里被重写的话),需要用call指令,效果和base.xxx()类似
from aspectcore-framework.
我测试了,与你说的一致,但是我并不认为这是一个很优雅的设计。
[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.
你有更好的方案吗~
from aspectcore-framework.
既然已经使用IL生成了代理类,那可以直接在IL中解决调用基类方法的问题,而不是在代理类的外部通过反射来调用基类方法。
from aspectcore-framework.
这样就写死啦。AspectCore的设计里需要解耦基类方法的调用
from aspectcore-framework.
Related Issues (20)
- .net 7.0 报错 HOT 1
- 项目上Autofac+aspectCore做ioc容器+aop,EnableParameterAspect之后Skywalking agent无法采集数据到服务器上
- Tars.Net link from README.md returns 404 Page Not Found
- Unable to cast object of type 'AspectCore.DynamicGenerated.MySqlStorageInitializer' to type 'DotNetCore.CAP.MySql.MySqlStorageInitializer'. HOT 1
- After introducing aop and calling the RegisterDynamicProxy method, execute the program again and find an exception HOT 3
- 特性无法拦截,是没考虑到这种场景,还是根本不支持呢? HOT 1
- 同一个接口下多个方法有拦截,相互调用只有第一个拦截生效 HOT 1
- 使用aspectCore做SqlSugar的事务拦截器,发现获取ISqlSugarClient对象变了 HOT 2
- 2.3.0版本升级后,发生System.NullReferenceException异常 HOT 4
- aop 能对属性进行拦截操作吗
- Bug:使用拦截器的时候从容器获取对象时调用的构造方法错误 HOT 3
- 启动性能问题 HOT 3
- Predicates.ForService 里面获取的类不全,导致无法aop HOT 5
- Bug-Report[Construct]doesnt support Parameters HOT 1
- Net8 兼容性错误 HOT 4
- 解惑下,以下代码在实现一个接口情况下可以 aop吗 HOT 1
- 对.net8 Framework有发布计划么? HOT 2
- WebApi架構下Controller跟Service放置不同專案時如何使用? HOT 1
- ServiceInterceptor 不支援 AllowMultiple
- Unhandled exception. System.InvalidOperationException: This service descriptor is keyed. Your service provider may not support keyed services. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aspectcore-framework.