【发布时间】:2015-12-29 09:01:28
【问题描述】:
我正在运行时构建一个类并创建它的对象。
在那之后,我有了这个生成的类的几个对象。
所以,我想代理这些对象:
IInterceptor[] interceptors = new IInterceptor[1];
interceptors[0] = new Interceptors.IEditableObjectInterceptor<object>();
return DynamicExtensions.proxyGenerator.CreateClassProxyWithTarget(baseType, target, options, interceptors);
当我执行CreateClassProxyWithTarget 时,它会撞倒我:
Can not instantiate proxy of class: DynamicDigitalInput.
Could not find a parameterless constructor.
所以,信息很清楚。但是,我尝试了下一个:
System.Reflection.ConstructorInfo cInfo = baseType.GetConstructor(new Type[] { });
Assert.That(cInfo != null);
var constructor = baseType.GetConstructor(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic,null, Type.EmptyTypes, null);
Assert.That(constructor != null);
object d = Activator.CreateInstance(baseType, new object[] {});
Assert.That(d != null);
而且效果很好。所以我可以获取默认构造函数并实例化一个DynamicDigitalInput 类对象。
问题出在哪里?
【问题讨论】:
标签: c# reflection castle-dynamicproxy