CreateInstance without ReflectionPermission

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Edwin

    #1

    CreateInstance without ReflectionPermission

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

    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.Creat eInstance(typeL ookup[objectname]
    , BindingFlags.In stance | BindingFlags.No nPublic
    , null
    , new object[] {objectparams, objectcomment, str}
    , null);

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

    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.

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: CreateInstance without ReflectionPermi ssion

    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.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Edwin" <edwinvandeburg t@gmail.comwrot e in message
    news:1192531762 .484442.131530@ q5g2000prf.goog legroups.com...
    >I would like to dynamically create an object, using a non-public
    constructor and without ReflectionPermi ssion.
    >
    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.Creat eInstance(typeL ookup[objectname]
    , BindingFlags.In stance | BindingFlags.No nPublic
    , null
    , new object[] {objectparams, objectcomment, str}
    , null);
    >
    I will have to have ReflectionPermi ssion, because the constructor is
    internal, and thus I'm using BindingFlags.No nPublic. Is there a way to
    dynamicaly invoke an internal constructor without
    ReflectionPermi ssion?
    >
    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.
    >

    Comment

    Working...