CreateInstance without ReflectionPermission

E

Edwin

I would like to dynamically create an object, using a non-public
constructor and without ReflectionPermission.

At the place where I want to do this, I could access the internal
constructor non-dynamically (because it's the same assembly), so
theoretically, I should have enough rights to do this with reflection
also...

But if I use:

st = (StructureType)
Activator.CreateInstance(typeLookup[objectname]
, BindingFlags.Instance | BindingFlags.NonPublic
, null
, new object[] {objectparams, objectcomment, str}
, null);

I will have to have ReflectionPermission, because the constructor is
internal, and thus I'm using BindingFlags.NonPublic. Is there a way to
dynamicaly invoke an internal constructor without
ReflectionPermission?

Of course, making the internal constructor public is not an option and
it must remain dynamic, thus instantiating based on the textstring
containing the classname.

Best regards,

Edwin.
 
N

Nicholas Paldino [.NET/C# MVP]

Edwin,

That's the thing, reflection permission is separate from visibility and
the ability to create a type. Just because you can create a type (it is
internal, and you are in the same assembly), don't assume you have
reflection permission. Since it has been cut off somewhere by you (you must
have set the reflection permission off somewhere), that's it.

The only way I can think to get around this would be to have a separate
factory for each instance you want to create, which will call the internal
constructor normally, and return the instance of the type.

Either that, or ease up the reflection permission.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top