Home All Groups Group Topic Archive Search About

Private member access.

Author
19 May 2005 9:24 AM
SimonJClarke
Hi,

I've got a collection of classes that encapsulate data records. Each of
these classes, through a common base class, can copy itself (This is
not a straightforward memberwise clone, but rather the ability for
object A to copy itself to an existing object B in a specific manner).
This functionality is implemented through reflection and the use of
FieldInfo / PropertyInfo .GetValue and .SetValue where the fields and
properties may be public or private. This works absolutly fine, but is
somewhat slow.

To try to improve performance I have written a function that generates
the required MSIL in a dynamic assembly to perform the copy operation
(i.e. one function is generated for each type that needs to be copied,
so that the copy operation now takes the form of a series of direct
assignments rather than having to loop over the runtime type
information during each copy). However - this only works on public
members (The speed increase is fantastic though) and if attempted on a
private member then a FieldAccessException is thrown.

Now I would have though that such a strategy is possible - after all
FieldInfo.SetValue() works perfectly well on private members. So could
someone please tell me what needs to be done to grant a dynamic
assembly access to private members.

Just to elaborate - the types that are being copied are all in
assemblies that I have control over - so they can be modified if
required to grant access (making the members public is a very last
resort though).

Any help would be very much appreciated!

Best Regards,
Simon.

Author
19 May 2005 11:40 AM
Nicole Calinoiu
Are the problem fields marked as readonly/init-only?  At the time reflection
is used, is all code on the call stack fully trusted?

BTW, I'm not quite seeing how the reflection issue arises if type-specific
copying methods have been generated prior to runtime.  If the answers to
both of the above questions are "no", might you be willing to post a sample
of the method from which the FieldAccessException is thrown?



<SimonJCla***@gmail.com> wrote in message
Show quoteHide quote
news:1116494674.945150.123040@g47g2000cwa.googlegroups.com...
> Hi,
>
> I've got a collection of classes that encapsulate data records. Each of
> these classes, through a common base class, can copy itself (This is
> not a straightforward memberwise clone, but rather the ability for
> object A to copy itself to an existing object B in a specific manner).
> This functionality is implemented through reflection and the use of
> FieldInfo / PropertyInfo .GetValue and .SetValue where the fields and
> properties may be public or private. This works absolutly fine, but is
> somewhat slow.
>
> To try to improve performance I have written a function that generates
> the required MSIL in a dynamic assembly to perform the copy operation
> (i.e. one function is generated for each type that needs to be copied,
> so that the copy operation now takes the form of a series of direct
> assignments rather than having to loop over the runtime type
> information during each copy). However - this only works on public
> members (The speed increase is fantastic though) and if attempted on a
> private member then a FieldAccessException is thrown.
>
> Now I would have though that such a strategy is possible - after all
> FieldInfo.SetValue() works perfectly well on private members. So could
> someone please tell me what needs to be done to grant a dynamic
> assembly access to private members.
>
> Just to elaborate - the types that are being copied are all in
> assemblies that I have control over - so they can be modified if
> required to grant access (making the members public is a very last
> resort though).
>
> Any help would be very much appreciated!
>
> Best Regards,
> Simon.
>
Author
19 May 2005 2:47 PM
SimonJClarke
The fields are not readonly, changing the modifier from private to
public allows the code to run ok.

The type-specific methods are being generated on-demand at runtime. The
point is that it is undesirable (given the number of classes invloved)
to have to maintain this function (and several others like it) for each
class.

I don't know if all the code of the stack is fully trusted or not (How
do you tell? I've never looked at anything like this before) - but on
the basis that the assembly that generates the dynamic assembly can
acheive the desired result using GetValue and SetValue, and that a
dynamic assembly, by default, inherits it's creator's permission set -
then I would assume that the dynamic assembly has sufficient
permission.

I'll knock up a little example and post it shortly.

Thanks for your help.
Author
19 May 2005 7:01 PM
Nicole Calinoiu
I think I'm going to need to need to see a sample.  Runtime generation of
code that uses reflection to set the values just isn't making that much
sense to me at the moment...


<SimonJCla***@gmail.com> wrote in message
Show quoteHide quote
news:1116514052.039584.314670@z14g2000cwz.googlegroups.com...
> The fields are not readonly, changing the modifier from private to
> public allows the code to run ok.
>
> The type-specific methods are being generated on-demand at runtime. The
> point is that it is undesirable (given the number of classes invloved)
> to have to maintain this function (and several others like it) for each
> class.
>
> I don't know if all the code of the stack is fully trusted or not (How
> do you tell? I've never looked at anything like this before) - but on
> the basis that the assembly that generates the dynamic assembly can
> acheive the desired result using GetValue and SetValue, and that a
> dynamic assembly, by default, inherits it's creator's permission set -
> then I would assume that the dynamic assembly has sufficient
> permission.
>
> I'll knock up a little example and post it shortly.
>
> Thanks for your help.
>