Export (0) Print
Expand All

ReflectionPermission Class

Controls access to non-public types and members through the System.Reflection APIs. Controls some features of the System.Reflection.Emit APIs.

Namespace: System.Security.Permissions
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class ReflectionPermission : CodeAccessPermission, IUnrestrictedPermission
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class ReflectionPermission extends CodeAccessPermission implements IUnrestrictedPermission
SerializableAttribute 
ComVisibleAttribute(true) 
public final class ReflectionPermission extends CodeAccessPermission implements IUnrestrictedPermission
Not applicable.

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.

Caution noteCaution:

Because ReflectionPermission can provide access to non-public types and members, we recommend that you do not grant ReflectionPermission to Internet code, except with the System.Security.Permissions.ReflectionPermissionFlag.RestrictedMemberAccess flag. RestrictedMemberAccess allows access to non-public members, with the restriction that the grant set of the non-public members must be equal to, or a subset of, the grant set of the code that accesses the non-public members.

Certain features of reflection emit, such as emitting debug symbols, require ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag.

For more information, see the ReflectionPermissionFlag enumeration.

The following code example shows the behavior of the ReflectionPermission class methods.

NoteNote:

The code example is intended to show the behavior of the methods, not to demonstrate their use. In general, the methods of permission classes are used by the security infrastructure; they are not typically used in applications. Generally, only the constructors are used in application code. The created instance validates or controls resource access by using inherited CodeAccessPermission methods such as Demand.

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

public class ReflectionPermissionDemo
{
    // Demonstrate all methods.
    public static void Main(String[] args)
    {
        IsSubsetOfDemo();
        CopyDemo();
        UnionDemo();
        IntersectDemo();
        ToFromXmlDemo();

    }
    // IsSubsetOf determines whether the current permission is a subset of the specified permission.
    private static void IsSubsetOfDemo()
    {
        ReflectionPermission reflectionPerm1 = new ReflectionPermission(ReflectionPermissionFlag.AllFlags);
        ReflectionPermission reflectionPerm2 = new ReflectionPermission(ReflectionPermissionFlag.ReflectionEmit);

        if (reflectionPerm2.IsSubsetOf(reflectionPerm1))
        {
            Console.WriteLine(reflectionPerm2.Flags + " is a subset of " +
                reflectionPerm1.Flags);
        }
        else
        {
            Console.WriteLine(reflectionPerm2.Flags + " is not a subset of " +
                reflectionPerm1.Flags);
        }
    }
    // Union creates a new permission that is the union of the current permission and the specified permission.
    private static void UnionDemo()
    {
        ReflectionPermission reflectionPerm1 = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess);
        ReflectionPermission reflectionPerm2 = new ReflectionPermission(ReflectionPermissionFlag.ReflectionEmit);
        ReflectionPermission reflectionPerm3 = (ReflectionPermission)reflectionPerm1.Union(reflectionPerm2);
        if (reflectionPerm3 == null)
        {
            Console.WriteLine("The union of " + reflectionPerm1.Flags + " and " +
                reflectionPerm2.Flags + " is null.");
        }
        else
        {
            Console.WriteLine("The union of " + reflectionPerm1.Flags + " and " +
                reflectionPerm2.Flags + " = " +
                ((ReflectionPermission)reflectionPerm3).Flags.ToString());
        }

    }
    // Intersect creates and returns a new permission that is the intersection of the current
    // permission and the permission specified.
    private static void IntersectDemo()
    {
        ReflectionPermission reflectionPerm1 = new ReflectionPermission(ReflectionPermissionFlag.AllFlags);
        ReflectionPermission reflectionPerm2 = new ReflectionPermission(ReflectionPermissionFlag.ReflectionEmit);
        ReflectionPermission reflectionPerm3 = (ReflectionPermission)reflectionPerm1.Union(reflectionPerm2);
        if (reflectionPerm3 != null)
        {
            Console.WriteLine("The intersection of " + reflectionPerm1.Flags +
                " and " + reflectionPerm2.Flags + " = " +
                ((ReflectionPermission)reflectionPerm3).Flags.ToString());
        }
        else
        {
            Console.WriteLine("The intersection of " + reflectionPerm1.Flags + " and " +
                reflectionPerm2.Flags + " is null.");
        }

    }
    //Copy creates and returns an identical copy of the current permission.
    private static void CopyDemo()
    {
        ReflectionPermission reflectionPerm1 = new ReflectionPermission(ReflectionPermissionFlag.AllFlags);
        ReflectionPermission reflectionPerm2 = (ReflectionPermission)reflectionPerm1.Copy();
        Console.WriteLine("Result of copy = " + reflectionPerm2.ToString());
    }
    // ToXml creates an XML encoding of the permission and its current state;
    // FromXml reconstructs a permission with the specified state from the XML encoding.
    private static void ToFromXmlDemo()
    {
        ReflectionPermission reflectionPerm1 = new ReflectionPermission(ReflectionPermissionFlag.AllFlags);
        ReflectionPermission reflectionPerm2 = new ReflectionPermission(PermissionState.None);
        reflectionPerm2.FromXml(reflectionPerm1.ToXml());
        Console.WriteLine("Result of ToFromXml = " +
            reflectionPerm2.ToString());
    }
}


System.Object
   System.Security.CodeAccessPermission
    System.Security.Permissions.ReflectionPermission

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft