DynamicProxy2 和代理链
我需要代理代理的属性类型。所以情况是:
我有接口 IMyInterface:
public interface IMyInterface
{
public String Name {get; set;}
public Int Id {get;set;}
}
我可以很好地模拟接口,但我希望能够模拟例如 Name 属性。我意识到 String 不能被嘲笑,因为它是密封的。我希望看到的功能是:
IMyInterfaceMock.Name.Equals()
应该由拦截器处理。我无法想象这在现有框架中是可能的,因为我将更改属性的类型,但我想知道是否有一种聪明的方法来实现这一点。有什么方法可以插入代理生成并修改代理属性的返回类型吗?
我认为 DynamicProxy2 不可能实现,但我想知道是否有人知道一些魔法。
I have the need to proxy the property types of a proxy. So the case would be:
I have interface IMyInterface:
public interface IMyInterface
{
public String Name {get; set;}
public Int Id {get;set;}
}
I can mock the interface just fine but I want to be able to mock, for instance, the Name property. I realize that String cannot be mocked because it is sealed. The functionality that I would like to see would be:
IMyInterfaceMock.Name.Equals()
should be handled by an Interceptor. I can't envision that this is even possible with the existing framework because I would be changing the type of the property but I was wondering if there was a clever way to achieve this. Is there any way I could interject into the proxy generation and modify the proxy's property's return type?
I don't think it's possible with DynamicProxy2 as it stands but I was wondering if anyone knew some magic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个。您无法修改返回的类型,因为这意味着无效的覆盖。
b.即使可以,您也无法覆盖字符串上的任何内容,因为字符串是密封的,所以不 - 在 CLR 上使用普通的强类型编程语言是不可能的。
a. you can't modify the type returned because that would mean invalid override.
b. even if you could, you can't override anything on string, which is sealed, so no - it is not possible on the CLR using normal strongly typed programming language.
我意识到该类型将是无效的覆盖。我真正寻找的是一种生成动态类型的方法。我使用 System.Reflection.Emit 类完成了此任务。
我创建了一个动态类型,其中属性 Types 是我可以拦截的众所周知的类型。
我应该解释一下,我正在将代理对象写入 PowerShell 管道,因此并不真正关心发出的类型。我只需要一种方法来评估比较运算符。
I realize the type is going to be an invalid override. What I was really looking for was a way to generate a dynamic type. I accomplished this using the System.Reflection.Emit classes.
I created a dynamic type where the property Types were that of a well-known type that I could then Intercept.
I should have explained that I was writing the proxy object to a PowerShell pipeline and thus didn't really care about the type that was emitted. I just need a way to evaluate on the comparison operators.