简体   繁体   中英

Dynamically created Proxy a class

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?

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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM