Re: Strange behavior or where is SecurityException?



I'm guessing that this is running against fx 2.0. If so, it might help to
be know that ReflectionPermission no longer protects access to non-public
member metadata. However, it does still protect use of non-public members.
If you were to try to read the value of the private field from an instance
of the type, you should see the SecurityException you expect.

HTH,
Nicole


"Sneil" <at@xxxxxxx> wrote in message
news:ugl0c%233jGHA.2004@xxxxxxxxxxxxxxxxxxxxxxx
Just simple code:

using System;
using System.Reflection;
using System.Security.Permissions;

[assembly : ReflectionPermission(SecurityAction.RequestRefuse,
Flags=ReflectionPermissionFlag.AllFlags)]

namespace UseReflect
{
public class Program
{
public static void Main(string[] args)
{
Assembly ClientAssembly =
Assembly.LoadFrom(@"c:\VSprojects\secur\JustLib\JustLib\bin\Debug\JustLi
b.dll");
Type myT = ClientAssembly.GetType("JustLib.MyLib");
// Accessing private members requires Reflection Permission
FieldInfo myFI = myT.GetField("f1", BindingFlags.NonPublic
| BindingFlags.Instance);
if(myFI != null)
Console.WriteLine("Field: {0} Type: {1}", myFI.Name,
myFI.FieldType);
else
Console.WriteLine("You don't have enough rights!");
Console.ReadKey();
}
}
}

And it's work without problem - no exceptions! But why? I
reject(RequestRefuse) all reflection activity for this assembly, but is
still work. Draw attention to - I don't want prevent other(third-party)
assembly to reflect my code. I only want prevent MY OWN code(because of
bug, for example) do reflect things. Am I missing something?


.



Relevant Pages

  • Re: Using built in dialog boxes - how to determine arguments?
    ... > argument lists of a specific dialog with reflection? ... "Interactions with built-in Word dialog boxes are through late binding, ... You must use the Reflection libraries to ... access dialog box members." ...
    (microsoft.public.vsnet.vstools.office)
  • Re: How to access private members of a base class
    ... private members, and so on, and so on... ... > I already use reflection, ... But I CANNOT obtain the PRIVATE members of the base ... >>> members of its base classes.(I already have the ReflectionPermission) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: comparing object values
    ... Please suggest whether reflection will work in this case? ... participate in comparisons and some members that should not. ... Each unless a subclass needs to ... comparison and then include their own for their added data members, ...
    (microsoft.public.dotnet.framework)
  • Re: comparing object values
    ... But using reflection will not necessarily always produce the correct results either. ... For most classes that have a need to be comparable, they contain some members that should participate in comparisons and some members that should not. ... Each unless a subclass needs to insert one of its own data members in the middle of the order of comparison for the parent class, all they need to do is do the parent's comparison and then include their own for their added data members, if any. ...
    (microsoft.public.dotnet.framework)