Export (0) Print
Expand All
1 out of 4 rated this helpful - Rate this topic

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

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 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();
        Console.WriteLine("Press the Enter key to exit.");
        Console.ReadLine();
    }
    // IsSubsetOf determines whether the current permission is a subset of the specified permission. 
    private static void IsSubsetOfDemo()
    {
        ReflectionPermission memberAccessPerm = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess);
        ReflectionPermission restrictedMemberAccessPerm = new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess);

        if (restrictedMemberAccessPerm.IsSubsetOf(memberAccessPerm))
        {
            Console.WriteLine(restrictedMemberAccessPerm.Flags + " is a subset of " +
                memberAccessPerm.Flags);
        }
        else
        {
            Console.WriteLine(restrictedMemberAccessPerm.Flags + " is not a subset of " +
                memberAccessPerm.Flags);
        }
    }
    // Union creates a new permission that is the union of the current permission and the specified permission. 
    private static void UnionDemo()
    {
        ReflectionPermission memberAccessPerm = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess);
        ReflectionPermission restrictedMemberAccessPerm = new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess);
        ReflectionPermission reflectionPerm3 = (ReflectionPermission)memberAccessPerm.Union(restrictedMemberAccessPerm);
        if (reflectionPerm3 == null)
        {
            Console.WriteLine("The union of " + memberAccessPerm.Flags + " and " +
                restrictedMemberAccessPerm.Flags + " is null.");
        }
        else
        {
            Console.WriteLine("The union of " + memberAccessPerm.Flags + " and " +
                restrictedMemberAccessPerm.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 memberAccessPerm = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess);
        ReflectionPermission restrictedMemberAccessPerm = new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess);
        ReflectionPermission reflectionPerm3 = (ReflectionPermission)memberAccessPerm.Intersect(restrictedMemberAccessPerm);
        if (reflectionPerm3 != null)
        {
            Console.WriteLine("The intersection of " + memberAccessPerm.Flags +
                " and " + restrictedMemberAccessPerm.Flags + " = " +
                ((ReflectionPermission)reflectionPerm3).Flags.ToString());
        }
        else
        {
            Console.WriteLine("The intersection of " + memberAccessPerm.Flags + " and " +
                restrictedMemberAccessPerm.Flags + " is null.");
        }

    }
    //Copy creates and returns an identical copy of the current permission. 
    private static void CopyDemo()
    {
        ReflectionPermission memberAccessPerm = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess);
        ReflectionPermission restrictedMemberAccessPerm = (ReflectionPermission)memberAccessPerm.Copy();
        Console.WriteLine("Result of copy = " + restrictedMemberAccessPerm.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 memberAccessPerm = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess);
        ReflectionPermission restrictedMemberAccessPerm = new ReflectionPermission(PermissionState.None);
        restrictedMemberAccessPerm.FromXml(memberAccessPerm.ToXml());
        Console.WriteLine("Result of ToFromXml = " +
            restrictedMemberAccessPerm.ToString());
    }
}

// This code example creates the following output: 

//RestrictedMemberAccess is not a subset of MemberAccess 
//Result of copy = <IPermission class="System.Security.Permissions.ReflectionPermi 
//ssion, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e0 
//89" 
//version="1" 
//Flags="MemberAccess"/>

//The union of MemberAccess and RestrictedMemberAccess = MemberAccess, RestrictedM 
//emberAccess 
//The intersection of MemberAccess and RestrictedMemberAccess is null. 
//Result of ToFromXml = <IPermission class="System.Security.Permissions.Reflection 
//Permission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561 
//934e089" 
//version="1" 
//Flags="MemberAccess"/>

//Press the Enter key to exit.
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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.