0
我在運行時創建一個類並創建它的對象。動態創建代理類
之後,我有這個生成的類的幾個對象。
所以,我想代理這些對象:
IInterceptor[] interceptors = new IInterceptor[1];
interceptors[0] = new Interceptors.IEditableObjectInterceptor<object>();
return DynamicExtensions.proxyGenerator.CreateClassProxyWithTarget(baseType, target, options, interceptors);
當我執行CreateClassProxyWithTarget
它chrashes傾銷我:
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);
它的工作很好。所以我可以得到默認構造函數並實例化一個類對象。
問題在哪裏?
我測試了這個刪除'System.Reflection.BindingFlags.NonPublic',結果是一樣的。我嘗試過使用'getConstructors',它使用一個元素支持我一個數組。該元素是一個'Void .ctor()'。 – Jordi
如果它可以找到一個無參數的ctor(),你應該能夠實例化對象而不需要額外的數據。即使'Activator.CreateInstance(baseType,null)'也可以。這很奇怪。 –
這真的很奇怪。我測試過了,我可以創建這個類的一個實例。 – Jordi