Export (0) Print
Expand All
Expand Minimize

ReflectionPermissionFlag Enumeration

Specifies the permitted use of System.Reflection on members that are not visible.

This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

[Visual Basic]
<Flags>
<Serializable>
Public Enum ReflectionPermissionFlag
[C#]
[Flags]
[Serializable]
public enum ReflectionPermissionFlag
[C++]
[Flags]
[Serializable]
__value public enum ReflectionPermissionFlag
[JScript]
public
   Flags
 Serializable
enum ReflectionPermissionFlag

Remarks

This enumeration is used by ReflectionPermission. If no ReflectionPermission is granted, reflection is allowed only on visible members.

CAUTION   Because ReflectionPermission can provide access to private class members and metadata, it is recommended that ReflectionPermission not be granted to Internet code.

Members

Member name Description Value
AllFlags TypeInformation, MemberAccess, and ReflectionEmit are set. 7
MemberAccess Invocation of operations on all type members is allowed. If this flag is not set, only invocation of operations on visible type members is allowed. 2
NoFlags No reflection is allowed on types that are not visible. 0
ReflectionEmit Use of System.Reflection.Emit is allowed. 4
TypeInformation Reflection is allowed on members of a type that are not visible. 1

Example

[Visual Basic] 
' This class generates ReflectionPermission objects using ReflectionPermissionFlag enumeration values.

Imports System
Imports System.Security
Imports System.Security.Permissions
Imports Microsoft.VisualBasic

Public Class ReflectionGenerator


    Private myReflection As ReflectionPermissionFlag() = _
        {ReflectionPermissionFlag.AllFlags, _
         ReflectionPermissionFlag.MemberAccess, _
         ReflectionPermissionFlag.NoFlags, _
         ReflectionPermissionFlag.ReflectionEmit, _
         ReflectionPermissionFlag.TypeInformation}

    Private reflectionIndex As Integer = 0


    Public Sub New()
        ResetIndex()
    End Sub 'New


    Public Sub ResetIndex()
        reflectionIndex = 0
    End Sub 'ResetIndex

    ' CreateReflection creates a ReflectionPermission object.
    Public Function CreateReflection(ByRef reflectionPerm As ReflectionPermission, ByRef reflection As ReflectionPermissionFlag) As Boolean

        If reflectionIndex >= myReflection.Length Then
            reflectionPerm = New ReflectionPermission(PermissionState.None)
            reflection = ReflectionPermissionFlag.NoFlags
            reflectionIndex &= 1
            Return False
        End If
        reflection = myReflection(reflectionIndex)
        reflectionIndex = reflectionIndex + 1
        Try
            reflectionPerm = New ReflectionPermission(reflection)
            Return True
        Catch e As Exception
            Console.WriteLine(("Cannot create ReflectionPermission: " & reflection & " " & e.ToString()))
            reflectionPerm = New ReflectionPermission(PermissionState.None)
            reflection = ReflectionPermissionFlag.NoFlags
            Return True
        End Try
    End Function 'CreateReflection
End Class 'ReflectionGenerator 
' End of ReflectionGenerator.

[C#] 
// This class generates ReflectionPermission objects using ReflectionPermissionFlag enumeration values.

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


public  class ReflectionGenerator
{


    private ReflectionPermissionFlag[] myReflection = 
        {
            ReflectionPermissionFlag.AllFlags,
            ReflectionPermissionFlag.MemberAccess,
            ReflectionPermissionFlag.NoFlags,
            ReflectionPermissionFlag.ReflectionEmit,
            ReflectionPermissionFlag.TypeInformation
        };

    private int reflectionIndex = 0;

    public ReflectionGenerator()
    {
        ResetIndex();
    }

    public void ResetIndex()
    {
        reflectionIndex = 0;
    }
    // CreateReflection creates a ReflectionPermission object.
    public bool CreateReflection(out ReflectionPermission reflectionPerm, 
        out ReflectionPermissionFlag reflection)
    {

        if(reflectionIndex >= myReflection.Length) 
        {
            reflectionPerm = new ReflectionPermission(PermissionState.None);
            reflection=ReflectionPermissionFlag.NoFlags;
            reflectionIndex++;
            return false;
        }
        reflection = myReflection[reflectionIndex++];
        try
        {
            reflectionPerm = new ReflectionPermission(reflection);
            return true;
        } 
        catch(Exception e)
        {
            Console.WriteLine("Cannot create ReflectionPermission: " + reflection +" "+e);
            reflectionPerm = new ReflectionPermission(PermissionState.None);
            reflection=ReflectionPermissionFlag.NoFlags;
            return true;
        }
    }
} // End of ReflectionGenerator.

[C++] 
// This class generates ReflectionPermission objects using ReflectionPermissionFlag enumeration values.

#using <mscorlib.dll>

using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::Runtime::InteropServices;

public __gc class ReflectionGenerator {

private:
   ReflectionPermissionFlag  myReflection[];
   int  reflectionIndex;

public:
   ReflectionGenerator() {
      ReflectionPermissionFlag temp[] = {
         ReflectionPermissionFlag::AllFlags,
         ReflectionPermissionFlag::MemberAccess,
         ReflectionPermissionFlag::NoFlags,
         ReflectionPermissionFlag::ReflectionEmit,
         ReflectionPermissionFlag::TypeInformation
      };
      myReflection = temp;

      ResetIndex();
   }

   void ResetIndex() {
      reflectionIndex = 0;
   }
   // CreateReflection creates a ReflectionPermission Object*.
   bool CreateReflection([Out] ReflectionPermission** reflectionPerm,
      [Out] ReflectionPermissionFlag* reflection) {

         if (reflectionIndex >= myReflection->Length) {
            *reflectionPerm = new ReflectionPermission(PermissionState::None);
            *reflection=ReflectionPermissionFlag::NoFlags;
            reflectionIndex++;
            return false;
         }
         *reflection = myReflection[reflectionIndex++];
         try {
            *reflectionPerm = new ReflectionPermission(*reflection);
            return true;
         } catch (Exception* e) {
            Console::WriteLine(S"Cannot create ReflectionPermission: {0}{1}", __box(*reflection), e);
            *reflectionPerm = new ReflectionPermission(PermissionState::None);
            *reflection=ReflectionPermissionFlag::NoFlags;
            return true;
         }
      }
}; // End of ReflectionGenerator.

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Security.Permissions

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

System.Security.Permissions Namespace | ReflectionPermission | ReflectionPermissionAttribute

Show:
© 2014 Microsoft