Castle.DynamicProxy2 并在运行时添加属性
我正在使用 Castle.DynamicProxy2 并且实例化我的代理,如下所示:
private static T GenerateProxy()
{
ArrayList addtlInterfaces = new ArrayList();
addtlInterfaces.Add(typeof (INotifyPropertyChanged));
addtlInterfaces.Add(typeof (EntityStatus));
object entityProxy = ProxyGenerator.CreateClassProxy(typeof(T),
addtlInterfaces.ToArray(typeof(Type)) as Type[],
ProxyGenerationOptions.Default,
new IInterceptor[] { new LazyInterceptor() });
return (T)entityProxy;
}
我的 IEntityStatus 接口如下所示:
public interface IEntityStatus
{
bool IsDirty
{ get; set;}
}
我需要能够在运行时使用该属性,以便当我的 DTO 有属性更改事件时,该事件可以设置DTO 脏了。 然而,因为它是一个接口并且没有明确的实现,所以我不知道如何做到这一点。 为 get 和 set 方法创建委托是我想避免的一个选项。 那么还有其他方法可以实现我想要实现的目标吗?
我意识到我可以设置所有活动 DTO 的集合,当属性更改事件在其中一个 DTO 上触发时,我可以更新该集合以显示该特定 DTO 是脏的,但我真的希望此信息成为代理 DTO 的一部分,以简化语法。
期待回复!
I am using Castle.DynamicProxy2 and I am instantiating my proxy as such:
private static T GenerateProxy()
{
ArrayList addtlInterfaces = new ArrayList();
addtlInterfaces.Add(typeof (INotifyPropertyChanged));
addtlInterfaces.Add(typeof (EntityStatus));
object entityProxy = ProxyGenerator.CreateClassProxy(typeof(T),
addtlInterfaces.ToArray(typeof(Type)) as Type[],
ProxyGenerationOptions.Default,
new IInterceptor[] { new LazyInterceptor() });
return (T)entityProxy;
}
My interface of IEntityStatus looks like such:
public interface IEntityStatus
{
bool IsDirty
{ get; set;}
}
I need to be able to use that property during run time so that when my DTO has a property changed event the event could set the DTO to dirty. However because it is an interface and has no explicit implementation I am at a loss as to how to do this. Creating a delegate for the get and set method is an option I would like to avoid. So is there another way to achieve what I am looking to achieve?
I realize I could set up a collection of all my active DTOs and when the property changed event fires on one of the DTOs I could update that collection to show that this particular DTO is dirty, but I would really like for this information to be a part of the proxied DTO for pure syntactic ease.
Look forward to responses!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道你如何处理 INotifyPropertyChanged 但我会为两个接口使用 mixin,让一个接口订阅另一个接口的事件。 这是一个可行的解决方案吗?
I dont know how you handle the INotifyPropertyChanged but I'd use mixin for both interfaces, having one subscribed to another's event. Is that a viable solution?