Tag: castle dynamicproxy

动态转换为generics类型

在周末开始之​​前,请快点… 我有一个带有以下签名的方法,我需要调用: public interface IObjectProvider { T Get(Predicate condition); } 这将为我提供符合谓词标准的任何来源的T 现在,必须从我所拥有的上下文调用以下内容: //the actual predicate that’s going to be evaluated var predicate = predicateProperty.GetValue(invocation.InvocationTarget, null); //The type that should go into the call as type param Type relationTargetType = relationDefinition.RelatedType; 正如您可能猜到的,编译器不会让我使用predicate变量作为参数。 我需要做的是将此对象转换为谓词,但通用类型参数必须是编译时常量,所以这不起作用。 我已经开始搞乱这个,但到目前为止没有成功: Type genericPredicateType = typeof(Predicate); Type specificPredicateType= genericPredicateType.MakeGenericType(relationTargetType); Convert.ChangeType(predicate, specificPredicateType) 我怎么能把它混在一起呢? 编辑:我认为这是一个与用例无关的问题,但显然我错了。 所以,既然对我所做的事情,我拥有的以及为什么以及诸如此类的事情有这样的大惊小怪,这里有更多的背景信息。 […]

Castle DynamicProxy Interceptor具有不同程序集的问题

我有这样的场景: 我正在使用拦截器来捕获对主程序引用的程序集内部类(我们称之为Feature)的类的调用。 assembly特征由NuGet安装(它不是公共的,而是我们的内部assembly)并且引用了另一个assembly(让我们称之为Core)。 主项目也引用了汇编Core。 Core包含类定义,该类定义用作截取方法之一的参数类型。 只要主项目和function引用相同版本的Core库,一切正常。 当此版本不同并且截获的方法使用Core中的类型作为方法参数时会出现问题。 在这种情况下,会抛出一个exception,指出A strongly-named assembly is required. : [FileLoadException: Could not load file or assembly ‘Core, Version=0.2.2.30, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)] Castle.Proxies.Invocations.IBasketService_Update.InvokeMethodOnTarget() +0 Castle.DynamicProxy.AbstractInvocation.Proceed() +116 Project.Basket.BasketServiceUpdatedInterceptor.Intercept(IInvocation invocation) in c:\(…)\Basket\BasketServiceUpdatedInterceptor.cs:20 Castle.DynamicProxy.AbstractInvocation.Proceed() +604 Castle.Proxies.IBasketServiceProxy.Update(ProductId productId, UInt16 quantity) +210 (…) 如果Core […]

如何在Castle.DynamicProxy中使用IInterceptor?

我写了一个这样的例子 简单计算器类: public class Calculator { public int Add(int a, int b) { return a + b; } } 实现了DynamicProxy提供的“IInterceptor” [Serializable] public abstract class Interceptor : IInterceptor { public void Intercept(IInvocation invocation) { ExecuteBefore(invocation); invocation.Proceed(); ExecuteAfter(invocation); } protected abstract void ExecuteAfter(IInvocation invocation); protected abstract void ExecuteBefore(IInvocation invocation); } 创建了一个Interceptor类,并inheritance自“Interceptor”类 public class CalculatorInterceptor : Interceptor { […]

获取代理对象的基础类型

我正在使用Castle DynamicProxy,我的ViewModel是一个代理,如下所示: 命名空间MyApplication.ViewModels { 公共类MyViewModel:BaseViewModel,IMyViewModel { } } 我的viewmodel的代理看起来像这样: {Name =“IRootViewModelProxyffecb133f590422098ca7c0ac13b8f98”FullName =“IRootViewModelProxyffecb133f590422098ca7c0ac13b8f98”} 我想获得被代理的实际类型的实际类型或命名空间。 有没有办法做到这一点? 我想要一些返回MyApplication.ViewModels.MyViewModel类型的东西。 如果我使用concreate类作为代理,BaseType将返回正在代理的实际类,但在使用该接口时,BaseType将返回System.Object。

城堡温莎拦截方法从class级内召唤

我们在Castle Windsor容器中注册了组件,就像这样 void RegisterComponent() { var component = Component.For().ImplementedBy(); component.Interceptors(); container.Register(component); } 然而,我们遇到的问题是,当我们从类中进行方法调用时,它不会被截获。 例如,我们有类似的组件 ServiceA : IService { public void MethodA1() { // do some stuff } public void MethodA2() { MethodA1(); } } 如果我们从其他类中调用MethodA2或MethodA1方法,则会截获它,但是当从MethodA2调用时, MethodA1显然不会被截获,因为调用来自类中。 我们发现类似的情况,解决方案Castle Dynamic Proxy在从类中调用时不拦截方法调用但是该解决方案使用new运算符来创建组件和代理,这在我们的情况下不适合,因为我们使用容器。 我们可以像上面那样使用此解决方案进行组件注册 还是有其他方法来解决这个问题?

Castle DynamicProxy:如何在代理接口时代理等于?

我需要使用Castle DynamicProxy通过向ProxyGenerator.CreateInterfaceProxyWithTarget提供它的实例来代理接口。 我还需要确保调用Equals,GetHashCode和ToString命中具体实例上的方法,我正在传递,而且我无法使其工作。 换句话说,我希望这个小样本打印True两次,而实际上它打印True,False : using System; using Castle.Core.Interceptor; using Castle.DynamicProxy; public interface IDummy { string Name { get; set; } } class Dummy : IDummy { public string Name { get; set; } public bool Equals(IDummy other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return Equals(other.Name, Name); } public override […]

应用AOP

我一直在使用一些基本的AOP风格解决方案来解决安全性,日志记录,validation等交叉问题。我的解决方案涉及Castle Windsor和DynamicProxy。 我走了这条路,因为我可以使用基于Boo的DSL来应用所有内容,并保持我的代码清除属性。 周末我被告知要看看PostSharp,因为它应该是一个“更好”的解决方案。 我已经快速浏览了PostSharp,但是我被属性使用所拖延了。 有没有人尝试过两种解决方案并愿意分享他们的经验?

使用具有Castle Proxy Interceptor的Simple Injector

我在我的asp.net mvc 4项目中使用Simple Injector。 我无法弄清楚如何使用带有城堡代理拦截器的Simple Injector。

Castle Dynamic Proxy在从类中调用时不拦截方法调用

使用Castle的动态代理时,我遇到了一些(我认为)奇怪的行为。 使用以下代码: class Program { static void Main(string[] args) { var c = new InterceptedClass(); var i = new Interceptor(); var cp = new ProxyGenerator().CreateClassProxyWithTarget(c, i); cp.Method1(); cp.Method2(); Console.ReadLine(); } } public class Interceptor : IInterceptor { public void Intercept(IInvocation invocation) { Console.WriteLine(string.Format(“Intercepted call to: ” + invocation.Method.Name)); invocation.Proceed(); } } public class InterceptedClass { […]

使用DynamicProxy拦截对异步方法的调用

下面是实现Castle动态代理库的IInterceptor的自定义类型上的Intercept方法的代码。 此代码段来自此处发布的基于AOP的日志记录概念validation控制台应用程序。 public void Intercept(IInvocation invocation) { if (Log.IsDebugEnabled) Log.Debug(CreateInvocationLogString(“Called”, invocation)); try { invocation.Proceed(); if (Log.IsDebugEnabled) if (invocation.Method.ReturnType != typeof(void)) Log.Debug(“Returning with: ” + invocation.ReturnValue); } catch (Exception ex) { if (Log.IsErrorEnabled) Log.Error(CreateInvocationLogString(“ERROR”, invocation), ex); throw; } } 这在常规方法调用中按预期工作,但在使用async方法(使用C#5.0中的async/await关键字)尝试时则不行。 我相信,我也理解这背后的原因。 为了使async/await工作,编译器将方法的function体添加到幕后的状态机中,一旦遇到无法同步完成的第一个awaitable表达式,控件将返回给调用者。 此外,我们可以询问返回类型并确定我们是否正在处理这样的async方法: if (invocation.Method.ReturnType == typeof(Task) || (invocation.Method.ReturnType.IsGenericType && invocation.Method.ReturnType.GetGenericTypeDefinition() == typeof(Task))) Log.Info(“Asynchronous method found…”); […]