Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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)

'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class ReflectionPermission
	Inherits CodeAccessPermission
	Implements IUnrestrictedPermission
'Usage
Dim instance As ReflectionPermission

/** @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.

Imports System
Imports System.Security
Imports System.Security.Permissions

Public Class ReflectionPermissionDemo

    ' Demonstrate all methods.
    Public Shared Sub Main(ByVal args() As String)
        IsSubsetOfDemo()
        CopyDemo()
        UnionDemo()
        IntersectDemo()
        ToFromXmlDemo()

    End Sub 'Main

    ' IsSubsetOf determines whether the current permission is a subset of the specified permission.
    Private Shared Sub IsSubsetOfDemo()

        Dim reflectionPerm1 As New ReflectionPermission(ReflectionPermissionFlag.AllFlags)
        Dim reflectionPerm2 As New ReflectionPermission(ReflectionPermissionFlag.ReflectionEmit)

        If reflectionPerm2.IsSubsetOf(reflectionPerm1) Then
            Console.WriteLine(reflectionPerm2.Flags.ToString() + " is a subset of " + _
                reflectionPerm1.Flags.ToString())
        Else
            Console.WriteLine(reflectionPerm2.Flags.ToString() + " is not a subset of " + _
                reflectionPerm1.Flags.ToString())
        End If

    End Sub 'IsSubsetOfDemo

    ' Union creates a new permission that is the union of the current permission and the specified permission.
    Private Shared Sub UnionDemo()

        Dim reflectionPerm1 As New ReflectionPermission(ReflectionPermissionFlag.MemberAccess)
        Dim reflectionPerm2 As New ReflectionPermission(ReflectionPermissionFlag.ReflectionEmit)
        Dim reflectionPerm3 As ReflectionPermission = CType(reflectionPerm1.Union(reflectionPerm2), ReflectionPermission)
        If reflectionPerm3 Is Nothing Then
            Console.WriteLine("The union of " + reflectionPerm1.Flags.ToString() + " and " + _
                reflectionPerm2.Flags.ToString() + " is null.")
        Else
            Console.WriteLine("The union of " + reflectionPerm1.Flags.ToString() + " and " + _
                reflectionPerm2.Flags.ToString() + " = " + _
                CType(reflectionPerm3, ReflectionPermission).Flags.ToString())
        End If

    End Sub 'UnionDemo


    ' Intersect creates and returns a new permission that is the intersection of the current
    ' permission and the permission specified.
    Private Shared Sub IntersectDemo()

        Dim reflectionPerm1 As New ReflectionPermission(ReflectionPermissionFlag.AllFlags)
        Dim reflectionPerm2 As New ReflectionPermission(ReflectionPermissionFlag.ReflectionEmit)
        Dim reflectionPerm3 As ReflectionPermission = CType(reflectionPerm1.Union(reflectionPerm2), ReflectionPermission)
        If Not (reflectionPerm3 Is Nothing) Then
            Console.WriteLine("The intersection of " + reflectionPerm1.Flags.ToString() + _
                " and " + reflectionPerm2.Flags.ToString() + " = " + _
                CType(reflectionPerm3, ReflectionPermission).Flags.ToString().ToString())
        Else
            Console.WriteLine("The intersection of " + reflectionPerm1.Flags.ToString() + _
                " and " + reflectionPerm2.Flags.ToString() + " is null.")
        End If

    End Sub 'IntersectDemo


    'Copy creates and returns an identical copy of the current permission.
    Private Shared Sub CopyDemo()

        Dim reflectionPerm1 As New ReflectionPermission(ReflectionPermissionFlag.AllFlags)
        Dim reflectionPerm2 As ReflectionPermission = CType(reflectionPerm1.Copy(), ReflectionPermission)
        Console.WriteLine("Result of copy = " + reflectionPerm2.ToString())

    End Sub 'CopyDemo

    ' 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 Shared Sub ToFromXmlDemo()

        Dim reflectionPerm1 As New ReflectionPermission(ReflectionPermissionFlag.AllFlags)
        Dim reflectionPerm2 As New ReflectionPermission(PermissionState.None)
        reflectionPerm2.FromXml(reflectionPerm1.ToXml())
        Console.WriteLine("Result of ToFromXml = " + reflectionPerm2.ToString())

    End Sub 'ToFromXmlDemo
End Class 'ReflectionPermissionDemo


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

Show:
© 2017 Microsoft