0

I'm building a class at runtime and creating objects of that.

After that, I've several objects of this generated class.

So, I'd like to proxy these objects:

IInterceptor[] interceptors = new IInterceptor[1];
interceptors[0] = new Interceptors.IEditableObjectInterceptor<object>();

return DynamicExtensions.proxyGenerator.CreateClassProxyWithTarget(baseType, target, options, interceptors);

When I perform CreateClassProxyWithTarget it chrashes dumping me:

Can not instantiate proxy of class: DynamicDigitalInput.
Could not find a parameterless constructor.

So, the message is clear. However, I've tried the next:

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);

And it's work well. So I can get default constructors and instantiate a DynamicDigitalInput class object.

Where's the problem?

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Jordi
  • 20,868
  • 39
  • 149
  • 333

1 Answers1

0

In the second statement var constructor = you specify System.Reflection.BindingFlags.NonPublic meaning it should get private constructors as well, so maybe the original class had something like the code below which would explain your scenario.

public class Dynamic
{
    private Dynamic()
    {

    }

    public Dynamic(params object[] exactly)
    {

    }
}
  • I've tested this removing `System.Reflection.BindingFlags.NonPublic`, and the result is the same. I've tried with `getConstructors` and it backs me an array with one element. The element is a `Void .ctor()`. – Jordi Oct 01 '15 at 11:37
  • If it can find a parameter-less ctor() you should be able to instantiate the object without additional data. Even `Activator.CreateInstance(baseType, null)` would work then. That is strange. – Martin Swanepoel Oct 01 '15 at 11:47
  • It's really strange. I've tested that, and I can create an instance of this class. – Jordi Oct 01 '15 at 12:02
  • Some ideas in order to unblock this question? – Jordi Oct 02 '15 at 07:55
  • Honestly I have no Idea, hopefully someone else on here can figure it out. I gave you my opinion and I think that's the closest we might get now. – Martin Swanepoel Oct 02 '15 at 12:56
  • I've created an issue on github: [issue #112](https://github.com/castleproject/Core/issues/112) – Jordi Oct 02 '15 at 13:04
  • Super, Post back the answer here when they've figured it out; for future reference. – Martin Swanepoel Oct 02 '15 at 13:07