views:

67

answers:

3

I have various projects that use reflection to access and invoke private or internal members in some of the framework classes.

These projects all work fine on my machine (running visual basic express 2008), but are they guaranteed to work on another machine, or, say, over a network?

I've had a look at ReflectionPermission, but I'm not sure if I need that granting to me or if I grant it to my project!

I'm not as stupid as this question sounds, honest!

+1  A: 

The computer where the program runs needs to grant ReflectionPermission to the program.

This will be no problem if the user runs your program from the local filesystem. It may be a problem if the user runs your program from a network share, an intranet, the Internet, etc. depending on the policies of the computer where the program runs. Policies may also consider factors such as the originating URL or network share, whether the program is signed, etc. For example, if this is an intranet app, the network admins may allow high trust to programs that originate from http://our%5Fsite/approved%5Fapps/ and are signed, but not to programs that originate from other parts of the intranet or are unsigned.

In any case, it is outside your control: your assembly can state that it needs ReflectionPermission, but whether it is granted is up to the configuration of the target system. (But again, if your program runs from the local file system, it shouldn't be a problem because it will then run with full trust unless someone has really locked down the machine.)

itowlson
+1  A: 

If you can execute the code then you should be able use reflection to interrogate it.

From my reading of the ReflectionPermission MSDN page it seems that you need to use this to gain access to all members of objects:

Without ReflectionPermission, code can use reflection to access only the public members of objects. Code with ReflectionPermission and the appropriate ReflectionPermissionFlag flags can access the protected and private members of objects.

ChrisF
+1  A: 

They should work fine on any machine as long as it is run from a local drive. By default running the executable from a network share on .NET up to 3.5SP1 would run you in a reduced trust mode (either Internet or Intranet) and you would not have access to the ReflectionPermission which means you will not be able to access protected or private members.

From 3.5SP1 onwards they have changed the operation of executables to give them full trust if run directly (as opposed to be loaded into another process via Assembly.Load and similar).

You can also grant specific permissions to an assembly using the caspol tool which is part of the framework. See this for more info about caspol.

tyranid