SecurityPermission クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<SerializableAttribute> _ <ComVisibleAttribute(True)> _ Public NotInheritable Class SecurityPermission Inherits CodeAccessPermission Implements IUnrestrictedPermission
[SerializableAttribute] [ComVisibleAttribute(true)] public sealed class SecurityPermission : CodeAccessPermission, IUnrestrictedPermission
[SerializableAttribute] [ComVisibleAttribute(true)] public ref class SecurityPermission sealed : public CodeAccessPermission, IUnrestrictedPermission


SecurityPermission クラス メソッドの動作を示すコード例を次に示します。この例は、メソッドの結果を示すことだけを目的としており、メソッドの一般的な使用方法を示すためのものではありません。SecurityPermission クラスを使用して特定の SecurityPermissionFlag 列挙値に対するアクセス許可を要求する例については、SecurityPermission(SecurityPermissionFlag) コンストラクタのトピックを参照してください。
Imports System Imports System.Security Imports System.Security.Permissions Imports Microsoft.VisualBasic <Assembly: CLSCompliant(True)> Public Class SecurityPermissionDemo ' IsSubsetOf determines whether the current permission is a subset of the specified permission. Private Function IsSubsetOfDemo() As Boolean Dim returnValue As Boolean = True Dim Security1, Security2 As SecurityPermissionFlag Dim SecurityPerm1, SecurityPerm2 As SecurityPermission Dim SecurityGen1 As New SecurityGenerator() Dim SecurityGen2 As New SecurityGenerator() SecurityGen1.ResetIndex() While SecurityGen1.GetSecurityFlag(Security1) SecurityPerm1 = New SecurityPermission(Security1) SecurityGen2.ResetIndex() Console.WriteLine("********************************************************" & ControlChars.Lf) While SecurityGen2.GetSecurityFlag(Security2) SecurityPerm2 = New SecurityPermission(Security2) Try If SecurityPerm1.IsSubsetOf(SecurityPerm2) Then Console.WriteLine((SecurityPerm1.Flags.ToString().ToString() _ & " is a subset of " & SecurityPerm2.Flags.ToString())) Else Console.WriteLine((SecurityPerm1.Flags.ToString() & " is not a subset of " _ & SecurityPerm2.Flags.ToString())) End If Catch e As Exception Console.WriteLine(("An exception was thrown for subset :" & SecurityPerm1.ToString() _ & ControlChars.Lf & SecurityPerm2.ToString() & ControlChars.Lf & e.ToString())) returnValue = False End Try ContinueWhile2: End While ContinueWhile1: End While Return returnValue End Function 'IsSubsetOfDemo ' Union creates a new permission that is the union of the current permission and ' the specified permission. Private Function UnionDemo() As Boolean Dim returnValue As Boolean = True Dim Security1, Security2 As SecurityPermissionFlag Dim SecurityPerm1, SecurityPerm2 As SecurityPermission Dim SecurityPerm3 As IPermission Dim SecurityGen1 As New SecurityGenerator() Dim SecurityGen2 As New SecurityGenerator() SecurityGen1.ResetIndex() While SecurityGen1.GetSecurityFlag(Security1) SecurityPerm1 = New SecurityPermission(Security1) Console.WriteLine("**********************************************************" & ControlChars.Lf) SecurityGen2.ResetIndex() While SecurityGen2.GetSecurityFlag(Security2) SecurityPerm2 = New SecurityPermission(Security2) SecurityPerm3 = CType(SecurityPerm1.Union(SecurityPerm2), SecurityPermission) SecurityPerm3 = SecurityPerm1.Union(SecurityPerm2) If SecurityPerm3 Is Nothing Then Console.WriteLine(("The union of " & Security1 & " and " & Security2 & " is null.")) Else Console.WriteLine(("The union of " & SecurityPerm1.Flags.ToString() _ & " and " & SecurityPerm2.Flags.ToString() & " = " & CType(SecurityPerm3, SecurityPermission).Flags.ToString().ToString())) End If ContinueWhile2: End While ContinueWhile1: End While Return returnValue End Function 'UnionDemo ' Intersect creates and returns a new permission that is the intersection of the current ' permission and the permission specified. Private Function IntersectDemo() As Boolean Dim returnValue As Boolean = True Dim Security1, Security2 As SecurityPermissionFlag Dim SecurityPerm1, SecurityPerm2, SecurityPerm3 As SecurityPermission Dim SecurityGen1 As New SecurityGenerator() Dim SecurityGen2 As New SecurityGenerator() SecurityGen1.ResetIndex() While SecurityGen1.GetSecurityFlag(Security1) SecurityPerm1 = New SecurityPermission(Security1) Console.WriteLine("**********************************************************" & ControlChars.Lf) SecurityGen2.ResetIndex() While SecurityGen2.GetSecurityFlag(Security2) SecurityPerm2 = New SecurityPermission(Security2) SecurityPerm3 = CType(SecurityPerm1.Intersect(SecurityPerm2), SecurityPermission) If Not (SecurityPerm3 Is Nothing) Then Console.WriteLine(("The intersection of " & Security1.ToString() & " and " _ & Security2.ToString() & " = " _ & CType(SecurityPerm3, SecurityPermission).Flags.ToString().ToString())) Else Console.WriteLine(("The intersection of " & Security1.ToString() & " and " _ & Security2.ToString() & " is null.")) End If ContinueWhile2: End While ContinueWhile1: End While Return returnValue End Function 'IntersectDemo 'Copy creates and returns an identical copy of the current permission. Private Function CopyDemo() As Boolean Dim returnValue As Boolean = True Dim Security1 As SecurityPermissionFlag Dim SecurityPerm1, SecurityPerm2 As SecurityPermission Dim SecurityGen1 As New SecurityGenerator() Dim SecurityGen2 As New SecurityGenerator() SecurityGen1.ResetIndex() While SecurityGen1.GetSecurityFlag(Security1) SecurityPerm1 = New SecurityPermission(Security1) SecurityGen2.ResetIndex() Console.WriteLine("********************************************************" & ControlChars.Lf) Try SecurityPerm2 = CType(SecurityPerm1.Copy(), SecurityPermission) Console.WriteLine(("Result of copy = " & SecurityPerm2.ToString())) Catch e As Exception Console.WriteLine(("Copy failed :" & SecurityPerm1.ToString() & e.ToString())) GoTo ContinueWhile1 End Try ContinueWhile1: End While Return returnValue End Function '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 Function ToFromXmlDemo() As Boolean Dim returnValue As Boolean = True Dim Security1 As SecurityPermissionFlag Dim SecurityPerm1, SecurityPerm2 As SecurityPermission Dim SecurityGen1 As New SecurityGenerator() Dim SecurityGen2 As New SecurityGenerator() SecurityGen1.ResetIndex() While SecurityGen1.GetSecurityFlag(Security1) SecurityPerm1 = New SecurityPermission(Security1) Console.WriteLine("********************************************************" & ControlChars.Lf) SecurityGen2.ResetIndex() Try SecurityPerm2 = New SecurityPermission(PermissionState.None) SecurityPerm2.FromXml(SecurityPerm1.ToXml()) Console.WriteLine(("Result of ToFromXml = " & SecurityPerm2.ToString())) Catch e As Exception Console.WriteLine(("ToFromXml failed :" & SecurityPerm1.ToString() & e.ToString())) GoTo ContinueWhile1 End Try ContinueWhile1: End While Return returnValue End Function 'ToFromXmlDemo ' Invoke all demos. Public Function runDemo() As Boolean Dim returnCode As Boolean = True Dim tempReturnCode As Boolean ' Call the IsSubsetOf demo. tempReturnCode = IsSubsetOfDemo() If tempReturnCode Then Console.Out.WriteLine("IsSubsetOf demo completed successfully.") Else Console.Out.WriteLine("IsSubsetOf demo failed.") End If returnCode = tempReturnCode AndAlso returnCode ' Call the Union demo. tempReturnCode = UnionDemo() If tempReturnCode Then Console.Out.WriteLine("Union demo completed successfully.") Else Console.Out.WriteLine("Union demo failed.") End If returnCode = tempReturnCode AndAlso returnCode ' Call the Intersect demo. tempReturnCode = IntersectDemo() If tempReturnCode Then Console.Out.WriteLine("Intersect demo completed successfully.") Else Console.Out.WriteLine("Intersect demo failed.") End If returnCode = tempReturnCode AndAlso returnCode ' Call the Copy demo. tempReturnCode = CopyDemo() If tempReturnCode Then Console.Out.WriteLine("Copy demo completed successfully.") Else Console.Out.WriteLine("Copy demo failed.") End If returnCode = tempReturnCode AndAlso returnCode ' Call the ToFromXml demo. tempReturnCode = ToFromXmlDemo() If tempReturnCode Then Console.Out.WriteLine("ToFromXml demo completed successfully.") Else Console.Out.WriteLine("ToFromXml demo failed.") End If returnCode = tempReturnCode AndAlso returnCode Return returnCode End Function 'runDemo ' Test harness. Public Overloads Shared Sub Main(ByVal args() As [String]) Try Dim democase As New SecurityPermissionDemo() Dim returnCode As Boolean = democase.runDemo() If returnCode Then Console.Out.WriteLine("SecurityPermission demo completed successfully.") Console.Out.WriteLine("Press the Enter key to exit.") Dim consoleInput As String = Console.ReadLine() System.Environment.ExitCode = 100 Else Console.Out.WriteLine("SecurityPermission demo failed.") Console.Out.WriteLine("Press the Enter key to exit.") Dim consoleInput As String = Console.ReadLine() System.Environment.ExitCode = 101 End If Catch e As Exception Console.Out.WriteLine("SecurityPermission demo failed.") Console.WriteLine(e.ToString()) Console.Out.WriteLine("Press the Enter key to exit.") Dim consoleInput As String = Console.ReadLine() System.Environment.ExitCode = 101 End Try End Sub 'Main End Class 'SecurityPermissionDemo ' This class generates SecurityPermission objects. Friend Class SecurityGenerator Private mySecurity As SecurityPermissionFlag() = {SecurityPermissionFlag.AllFlags, _ SecurityPermissionFlag.Assertion Or SecurityPermissionFlag.ControlAppDomain, _ SecurityPermissionFlag.ControlAppDomain, _ SecurityPermissionFlag.ControlDomainPolicy Or SecurityPermissionFlag.ControlEvidence, _ SecurityPermissionFlag.ControlEvidence, _ SecurityPermissionFlag.ControlPolicy Or SecurityPermissionFlag.ControlPrincipal, _ SecurityPermissionFlag.ControlPrincipal, _ SecurityPermissionFlag.ControlThread, _ SecurityPermissionFlag.Execution, _ SecurityPermissionFlag.Infrastructure, _ SecurityPermissionFlag.NoFlags, _ SecurityPermissionFlag.RemotingConfiguration Or SecurityPermissionFlag.SerializationFormatter, _ SecurityPermissionFlag.SerializationFormatter, _ SecurityPermissionFlag.SkipVerification, _ SecurityPermissionFlag.UnmanagedCode} Private reflectionIndex As Integer = 0 Public Sub New() ResetIndex() End Sub 'New Public Sub ResetIndex() reflectionIndex = 0 End Sub 'ResetIndex ' GetSecurityFlag creates a SecurityPermission object. Public Function GetSecurityFlag(ByRef Security As SecurityPermissionFlag) As Boolean If reflectionIndex >= mySecurity.Length Then Security = SecurityPermissionFlag.NoFlags reflectionIndex &= 1 Return False End If Security = mySecurity(reflectionIndex) reflectionIndex = reflectionIndex + 1 Return True End Function
using System; using System.Security; using System.Security.Permissions; [assembly:CLSCompliant(true)] public class SecurityPermissionDemo { // IsSubsetOf determines whether the current permission is a subset of the specified permission. private bool IsSubsetOfDemo() { bool returnValue = true; SecurityPermissionFlag security1,security2; SecurityPermission securityPerm1,securityPerm2; SecurityGenerator SecurityGen1 = new SecurityGenerator(); SecurityGenerator SecurityGen2 = new SecurityGenerator(); SecurityGen1.ResetIndex(); while(SecurityGen1.GetSecurityFlag( out security1 )) { securityPerm1 = new SecurityPermission(security1); SecurityGen2.ResetIndex(); Console.WriteLine("********************************************************\n"); while(SecurityGen2.GetSecurityFlag( out security2 )) { securityPerm2 = new SecurityPermission(security2); try { if (securityPerm1.IsSubsetOf(securityPerm2)) { Console.WriteLine(securityPerm1.Flags + " is a subset of " + securityPerm2.Flags); } else { Console.WriteLine(securityPerm1.Flags + " is not a subset of " + securityPerm2.Flags); } } catch(Exception e) { Console.WriteLine("An exception was thrown for subset :" + securityPerm1 + "\n" + securityPerm2 +"\n" + e); returnValue=false; } } } return returnValue; } // Union creates a new permission that is the union of the current permission and the specified permission. private bool UnionDemo() { bool returnValue = true; SecurityPermissionFlag security1,security2; SecurityPermission securityPerm1,securityPerm2; IPermission SecurityPerm3; SecurityGenerator SecurityGen1 = new SecurityGenerator(); SecurityGenerator SecurityGen2 = new SecurityGenerator(); SecurityGen1.ResetIndex(); while(SecurityGen1.GetSecurityFlag( out security1 )) { securityPerm1 = new SecurityPermission(security1); Console.WriteLine("**********************************************************\n"); SecurityGen2.ResetIndex(); while(SecurityGen2.GetSecurityFlag( out security2 )) { securityPerm2 = new SecurityPermission(security2); SecurityPerm3 = (SecurityPermission)securityPerm1.Union(securityPerm2); SecurityPerm3 = securityPerm1.Union(securityPerm2); if(SecurityPerm3 == null) { Console.WriteLine("The union of " + security1 + " and " + security2 + " is null." ); } else { Console.WriteLine("The union of " + securityPerm1.Flags + " and " + securityPerm2.Flags + " = " + ((SecurityPermission)SecurityPerm3).Flags.ToString()); } } } return returnValue; } // Intersect creates and returns a new permission that is the intersection of the current // permission and the permission specified. private bool IntersectDemo() { bool returnValue = true; SecurityPermissionFlag security1,security2; SecurityPermission securityPerm1,securityPerm2,SecurityPerm3; SecurityGenerator SecurityGen1 = new SecurityGenerator(); SecurityGenerator SecurityGen2 = new SecurityGenerator(); SecurityGen1.ResetIndex(); while(SecurityGen1.GetSecurityFlag( out security1 )) { securityPerm1 = new SecurityPermission(security1); Console.WriteLine("**********************************************************\n"); SecurityGen2.ResetIndex(); while(SecurityGen2.GetSecurityFlag( out security2 )) { securityPerm2 = new SecurityPermission(security2); SecurityPerm3 = (SecurityPermission)securityPerm1.Intersect(securityPerm2); if (SecurityPerm3 != null) { Console.WriteLine("The intersection of " + security1 + " and " + security2 + " = " + ((SecurityPermission)SecurityPerm3).Flags.ToString()); } else { Console.WriteLine("The intersection of " + security1 + " and " + security2 + " is null." ); } } } return returnValue; } //Copy creates and returns an identical copy of the current permission. private bool CopyDemo() { bool returnValue = true; SecurityPermissionFlag security1; SecurityPermission securityPerm1,securityPerm2; SecurityGenerator SecurityGen1 = new SecurityGenerator(); SecurityGenerator SecurityGen2 = new SecurityGenerator(); SecurityGen1.ResetIndex(); while(SecurityGen1.GetSecurityFlag( out security1 )) { securityPerm1 = new SecurityPermission(security1); SecurityGen2.ResetIndex(); Console.WriteLine("********************************************************\n"); try { securityPerm2 = (SecurityPermission)securityPerm1.Copy(); Console.WriteLine("Result of copy = " + securityPerm2.ToString()); } catch(Exception e) { Console.WriteLine("Copy failed :" + securityPerm1.ToString() + e); continue; } } return returnValue; } // 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 bool ToFromXmlDemo() { bool returnValue = true; SecurityPermissionFlag security1; SecurityPermission securityPerm1,securityPerm2; SecurityGenerator SecurityGen1 = new SecurityGenerator(); SecurityGenerator SecurityGen2 = new SecurityGenerator(); SecurityGen1.ResetIndex(); while(SecurityGen1.GetSecurityFlag( out security1 )) { securityPerm1 = new SecurityPermission(security1); Console.WriteLine("********************************************************\n"); SecurityGen2.ResetIndex(); try { securityPerm2= new SecurityPermission(PermissionState.None); securityPerm2.FromXml(securityPerm1.ToXml()); Console.WriteLine("Result of ToFromXml = " + securityPerm2.ToString()); } catch(Exception e) { Console.WriteLine("ToFromXml failed :" + securityPerm1.ToString() + e); continue; } } return returnValue; } // Invoke all demos. public bool runDemo() { bool returnCode = true; bool tempReturnCode; // Call the IsSubsetOf demo. tempReturnCode= IsSubsetOfDemo(); if(tempReturnCode)Console.Out.WriteLine("IsSubsetOf demo completed successfully."); else Console.Out.WriteLine("IsSubsetOf demo failed."); returnCode=tempReturnCode && returnCode; // Call the Union demo. tempReturnCode= UnionDemo(); if(tempReturnCode)Console.Out.WriteLine("Union demo completed successfully."); else Console.Out.WriteLine("Union demo failed."); returnCode=tempReturnCode && returnCode; // Call the Intersect demo. tempReturnCode= IntersectDemo(); if(tempReturnCode)Console.Out.WriteLine("Intersect demo completed successfully."); else Console.Out.WriteLine("Intersect demo failed."); returnCode=tempReturnCode && returnCode; // Call the Copy demo. tempReturnCode= CopyDemo(); if(tempReturnCode)Console.Out.WriteLine("Copy demo completed successfully."); else Console.Out.WriteLine("Copy demo failed."); returnCode=tempReturnCode && returnCode; // Call the ToFromXml demo. tempReturnCode= ToFromXmlDemo(); if(tempReturnCode)Console.Out.WriteLine("ToFromXml demo completed successfully."); else Console.Out.WriteLine("ToFromXml demo failed."); returnCode=tempReturnCode && returnCode; return ( returnCode ); } // Test harness. public static void Main(String[] args) { try { SecurityPermissionDemo democase = new SecurityPermissionDemo(); bool returnCode = democase.runDemo(); if (returnCode) { Console.Out.WriteLine("SecurityPermission demo completed successfully."); Console.Out.WriteLine("Press the Enter key to exit."); string consoleInput = Console.ReadLine(); System.Environment.ExitCode = 100; } else { Console.Out.WriteLine("SecurityPermission demo failed."); Console.Out.WriteLine("Press the Enter key to exit."); string consoleInput = Console.ReadLine(); System.Environment.ExitCode = 101; } } catch(Exception e) { Console.Out.WriteLine("SecurityPermission demo failed."); Console.WriteLine(e.ToString()); Console.Out.WriteLine("Press the Enter key to exit."); string consoleInput = Console.ReadLine(); System.Environment.ExitCode = 101; } } } // This class generates SecurityPermission objects. internal class SecurityGenerator { private SecurityPermissionFlag[] mySecurity = { SecurityPermissionFlag.AllFlags, SecurityPermissionFlag.Assertion | SecurityPermissionFlag.ControlAppDomain, SecurityPermissionFlag.ControlAppDomain, SecurityPermissionFlag.ControlDomainPolicy | SecurityPermissionFlag.ControlEvidence, SecurityPermissionFlag.ControlEvidence, SecurityPermissionFlag.ControlPolicy | SecurityPermissionFlag.ControlPrincipal, SecurityPermissionFlag.ControlPrincipal, SecurityPermissionFlag.ControlThread, SecurityPermissionFlag.Execution, SecurityPermissionFlag.Infrastructure, SecurityPermissionFlag.NoFlags, SecurityPermissionFlag.RemotingConfiguration | SecurityPermissionFlag.SerializationFormatter, SecurityPermissionFlag.SerializationFormatter, SecurityPermissionFlag.SkipVerification, SecurityPermissionFlag.UnmanagedCode}; private int reflectionIndex = 0; public SecurityGenerator() { ResetIndex(); } public void ResetIndex() { reflectionIndex = 0; } // GetSecurityFlag returns a SecurityPermissionFlag. public bool GetSecurityFlag(out SecurityPermissionFlag security) { if(reflectionIndex >= mySecurity.Length) { security = SecurityPermissionFlag.NoFlags; reflectionIndex++; return false; } security = mySecurity[reflectionIndex++]; return true; } } //
using namespace System; using namespace System::Security; using namespace System::Security::Permissions; using namespace System::Runtime::InteropServices; // This class generates SecurityPermission objects. [assembly:CLSCompliant(true)]; private ref class SecurityGenerator { private: array<SecurityPermissionFlag>^mySecurity; int reflectionIndex; public: SecurityGenerator() { array<SecurityPermissionFlag>^ temp = {SecurityPermissionFlag::AllFlags, SecurityPermissionFlag::Assertion | SecurityPermissionFlag::ControlAppDomain, SecurityPermissionFlag::ControlAppDomain, SecurityPermissionFlag::ControlDomainPolicy | SecurityPermissionFlag::ControlEvidence, SecurityPermissionFlag::ControlEvidence, SecurityPermissionFlag::ControlPolicy | SecurityPermissionFlag::ControlPrincipal, SecurityPermissionFlag::ControlPrincipal, SecurityPermissionFlag::ControlThread, SecurityPermissionFlag::Execution, SecurityPermissionFlag::Infrastructure, SecurityPermissionFlag::NoFlags, SecurityPermissionFlag::RemotingConfiguration | SecurityPermissionFlag::SerializationFormatter, SecurityPermissionFlag::SerializationFormatter, SecurityPermissionFlag::SkipVerification, SecurityPermissionFlag::UnmanagedCode}; mySecurity = temp; reflectionIndex = 0; ResetIndex(); } void ResetIndex() { reflectionIndex = 0; } // GetSecurityFlag creates a SecurityPermission object. bool GetSecurityFlag( [Out]SecurityPermissionFlag * security ) { if ( reflectionIndex >= mySecurity->Length ) { *security = SecurityPermissionFlag::NoFlags; reflectionIndex++; return false; } *security = mySecurity[ reflectionIndex++ ]; return true; } }; // End of SecurityGenerator. public ref class SecurityPermissionDemo { private: // IsSubsetOf determines whether the current permission is a subset of the specified permission. bool IsSubsetOfDemo() { bool returnValue = true; SecurityPermissionFlag security1; SecurityPermissionFlag security2; SecurityPermission^ securityPerm1; SecurityPermission^ securityPerm2; SecurityGenerator^ SecurityGen1 = gcnew SecurityGenerator; SecurityGenerator^ SecurityGen2 = gcnew SecurityGenerator; SecurityGen1->ResetIndex(); while ( SecurityGen1->GetSecurityFlag( &security1 ) ) { securityPerm1 = gcnew SecurityPermission(security1); SecurityGen2->ResetIndex(); Console::WriteLine( "********************************************************\n" ); while ( SecurityGen2->GetSecurityFlag( &security2 ) ) { securityPerm2 = gcnew SecurityPermission(security2); try { if ( securityPerm1->IsSubsetOf( securityPerm2 ) ) { Console::WriteLine( "{0} is a subset of {1}", securityPerm1->Flags, securityPerm2->Flags ); } else { Console::WriteLine( "{0} is not a subset of {1}", securityPerm1->Flags, securityPerm2->Flags ); } } catch ( Exception^ e ) { Console::WriteLine( "An exception was thrown for subset :{0}\n{1}\n{2}", securityPerm1, securityPerm2, e ); returnValue = false; } } } return returnValue; } // Union creates a new permission that is the union of the current permission and the specified permission. bool UnionDemo() { bool returnValue = true; SecurityPermissionFlag security1; SecurityPermissionFlag security2; SecurityPermission^ securityPerm1; SecurityPermission^ securityPerm2; IPermission^ SecurityPerm3; SecurityGenerator^ SecurityGen1 = gcnew SecurityGenerator; SecurityGenerator^ SecurityGen2 = gcnew SecurityGenerator; SecurityGen1->ResetIndex(); while ( SecurityGen1->GetSecurityFlag( &security1 ) ) { securityPerm1 = gcnew SecurityPermission(security1); Console::WriteLine( "**********************************************************\n" ); SecurityGen2->ResetIndex(); while ( SecurityGen2->GetSecurityFlag( &security2 ) ) { securityPerm2 = gcnew SecurityPermission(security2); SecurityPerm3 = dynamic_cast<SecurityPermission^>(securityPerm1->Union( securityPerm2 )); SecurityPerm3 = securityPerm1->Union( securityPerm2 ); if ( SecurityPerm3 == nullptr ) { Console::WriteLine( "The union of {0} and {1} is null.", security1, security2 ); } else { Console::WriteLine( "The union of {0} and {1} = {2}", securityPerm1->Flags, securityPerm2->Flags, (dynamic_cast<SecurityPermission^>(SecurityPerm3))->Flags ); } } } return returnValue; } // Intersect creates and returns a new permission that is the intersection of the current // permission and the permission specified. bool IntersectDemo() { bool returnValue = true; SecurityPermissionFlag security1; SecurityPermissionFlag security2; SecurityPermission^ securityPerm1; SecurityPermission^ securityPerm2; SecurityPermission^ SecurityPerm3; SecurityGenerator^ SecurityGen1 = gcnew SecurityGenerator; SecurityGenerator^ SecurityGen2 = gcnew SecurityGenerator; SecurityGen1->ResetIndex(); while ( SecurityGen1->GetSecurityFlag( &security1 ) ) { securityPerm1 = gcnew SecurityPermission(security1); Console::WriteLine( "**********************************************************\n" ); SecurityGen2->ResetIndex(); while ( SecurityGen2->GetSecurityFlag( &security2 ) ) { securityPerm2 = gcnew SecurityPermission(security2); SecurityPerm3 = dynamic_cast<SecurityPermission^>(securityPerm1->Intersect( securityPerm2 )); if ( SecurityPerm3 != nullptr ) { Console::WriteLine( "The intersection of {0} and {1} = {2}", security1, security2, (dynamic_cast<SecurityPermission^>(SecurityPerm3))->Flags ); } else { Console::WriteLine( "The intersection of {0} and {1} is null.", security1, security2 ); } } } return returnValue; } //Copy creates and returns an identical copy of the current permission. bool CopyDemo() { bool returnValue = true; SecurityPermissionFlag security1; SecurityPermission^ securityPerm1; SecurityPermission^ securityPerm2; SecurityGenerator^ SecurityGen1 = gcnew SecurityGenerator; SecurityGenerator^ SecurityGen2 = gcnew SecurityGenerator; SecurityGen1->ResetIndex(); while ( SecurityGen1->GetSecurityFlag( &security1 ) ) { securityPerm1 = gcnew SecurityPermission(security1); continue; SecurityGen2->ResetIndex(); Console::WriteLine( "********************************************************\n" ); try { securityPerm2 = dynamic_cast<SecurityPermission^>(securityPerm1->Copy()); Console::WriteLine( "Result of copy = {0}", securityPerm2 ); } catch ( Exception^ e ) { Console::WriteLine( "Copy failed :{0}{1}", securityPerm1, e ); continue; } } return returnValue; } // ToXml creates an XML encoding of the permission and its current state; // FromXml reconstructs a permission with the specified state from the XML encoding. bool ToFromXmlDemo() { bool returnValue = true; SecurityPermissionFlag security1; SecurityPermission^ securityPerm1; SecurityPermission^ securityPerm2; SecurityGenerator^ SecurityGen1 = gcnew SecurityGenerator; SecurityGenerator^ SecurityGen2 = gcnew SecurityGenerator; SecurityGen1->ResetIndex(); while ( SecurityGen1->GetSecurityFlag( &security1 ) ) { securityPerm1 = gcnew SecurityPermission(security1); continue; Console::WriteLine( "********************************************************\n" ); SecurityGen2->ResetIndex(); try { securityPerm2 = gcnew SecurityPermission( PermissionState::None ); securityPerm2->FromXml( securityPerm1->ToXml() ); Console::WriteLine( "Result of ToFromXml = {0}", securityPerm2 ); } catch ( Exception^ e ) { Console::WriteLine( "ToFromXml failed :{0}{1}", securityPerm1, e ); continue; } } return returnValue; } public: // Invoke all demos. bool runDemo() { bool returnCode = true; bool tempReturnCode; // Call the IsSubsetOf demo. tempReturnCode = IsSubsetOfDemo(); if ( tempReturnCode ) Console::Out->WriteLine( "IsSubsetOf demo completed successfully." ); else Console::Out->WriteLine( "IsSubsetOf demo failed." ); returnCode = tempReturnCode && returnCode; // Call the Union demo. tempReturnCode = UnionDemo(); if ( tempReturnCode ) Console::Out->WriteLine( "Union demo completed successfully." ); else Console::Out->WriteLine( "Union demo failed." ); returnCode = tempReturnCode && returnCode; // Call the Intersect demo. tempReturnCode = IntersectDemo(); if ( tempReturnCode ) Console::Out->WriteLine( "Intersect demo completed successfully." ); else Console::Out->WriteLine( "Intersect demo failed." ); returnCode = tempReturnCode && returnCode; // Call the Copy demo. tempReturnCode = CopyDemo(); if ( tempReturnCode ) Console::Out->WriteLine( "Copy demo completed successfully." ); else Console::Out->WriteLine( "Copy demo failed." ); returnCode = tempReturnCode && returnCode; // Call the ToFromXml demo. tempReturnCode = ToFromXmlDemo(); if ( tempReturnCode ) Console::Out->WriteLine( "ToFromXml demo completed successfully." ); else Console::Out->WriteLine( "ToFromXml demo failed." ); returnCode = tempReturnCode && returnCode; return (returnCode); } }; // Test harness. int main() { try { SecurityPermissionDemo^ democase = gcnew SecurityPermissionDemo; bool returnCode = democase->runDemo(); if ( returnCode ) { Console::Out->WriteLine( "SecurityPermission demo completed successfully." ); Console::Out->WriteLine( "Press the Enter key to exit." ); String^ consoleInput = Console::ReadLine(); System::Environment::ExitCode = 100; } else { Console::Out->WriteLine( "SecurityPermission demo failed." ); Console::Out->WriteLine( "Press the Enter key to exit." ); String^ consoleInput = Console::ReadLine(); System::Environment::ExitCode = 101; } } catch ( Exception^ e ) { Console::Out->WriteLine( "SecurityPermission demo failed." ); Console::WriteLine( e ); Console::Out->WriteLine( "Press the Enter key to exit." ); String^ consoleInput = Console::ReadLine(); System::Environment::ExitCode = 101; } }
import System.*; import System.Security.*; import System.Security.Permissions.*; /** @assembly CLSCompliant(true) */ public class SecurityPermissionDemo { // IsSubsetOf determines whether the current permission is a // subset of the specified permission. private boolean IsSubsetOfDemo() { boolean returnValue = true; SecurityPermissionFlag security1[] = new SecurityPermissionFlag[1]; SecurityPermissionFlag security2[] = new SecurityPermissionFlag[1]; SecurityPermission securityPerm1[] = new SecurityPermission[1]; SecurityPermission securityPerm2[] = new SecurityPermission[1]; SecurityGenerator securityGen1 = new SecurityGenerator(); SecurityGenerator securityGen2 = new SecurityGenerator(); securityGen1.ResetIndex(); while (securityGen1.CreateSecurity(securityPerm1, security1)) { if (securityPerm1[0] == null) { continue ; } securityGen2.ResetIndex(); Console.WriteLine("****************************" + "****************************\n"); while (securityGen2.CreateSecurity(securityPerm2, security2)) { if (securityPerm2[0] == null) { continue ; } try { if (securityPerm1[0].IsSubsetOf(securityPerm2[0])) { Console.WriteLine((securityPerm1[0].get_Flags() + " is a subset of " + securityPerm2[0].get_Flags())); } else { Console.WriteLine((securityPerm1[0].get_Flags() + " is not a subset of " + securityPerm2[0].get_Flags())); } } catch(System.Exception e) { Console.WriteLine(("An exception was " + "thrown for subset :" + securityPerm1[0] + "\n" + securityPerm2[0] + "\n" + e)); returnValue = false; } } } return returnValue ; } //IsSubsetOfDemo // Union creates a new permission that is the union of the // current permission and the specified permission. private boolean UnionDemo() { boolean returnValue = true; SecurityPermissionFlag security1[] = new SecurityPermissionFlag[1]; SecurityPermissionFlag security2[] = new SecurityPermissionFlag[1]; SecurityPermission securityPerm1[] = new SecurityPermission[1]; SecurityPermission securityPerm2[] = new SecurityPermission[1]; IPermission securityPerm3; SecurityGenerator securityGen1 = new SecurityGenerator(); SecurityGenerator securityGen2 = new SecurityGenerator(); securityGen1.ResetIndex(); while (securityGen1.CreateSecurity(securityPerm1, security1)) { if (securityPerm1[0] == null) { continue ; } Console.WriteLine("*******************************" + "***************************\n"); securityGen2.ResetIndex(); while (securityGen2.CreateSecurity(securityPerm2, security2)) { if (securityPerm2[0] == null) { continue ; } securityPerm3 =((SecurityPermission)(securityPerm1[0]. Union(securityPerm2[0]))); securityPerm3 = securityPerm1[0].Union(securityPerm2[0]); if (securityPerm3 == null) { Console.WriteLine(("The union of " + security1[0] + " and " + security2[0] + " is null.")); } else { Console.WriteLine(("The union of " + securityPerm1[0].get_Flags() + " and " + securityPerm2[0].get_Flags() + " = " + ((SecurityPermission)(securityPerm3)). get_Flags().ToString())); } } }return returnValue ; } //UnionDemo // Intersect creates and returns a new permission that is the // intersection of the current // permission and the permission specified. private boolean IntersectDemo() { boolean returnValue = true; SecurityPermissionFlag security1[] = new SecurityPermissionFlag[1]; SecurityPermissionFlag security2[] = new SecurityPermissionFlag[1]; SecurityPermission securityPerm1[] = new SecurityPermission[1]; SecurityPermission securityPerm2[] = new SecurityPermission[1]; SecurityPermission securityPerm3; SecurityGenerator securityGen1 = new SecurityGenerator(); SecurityGenerator securityGen2 = new SecurityGenerator(); securityGen1.ResetIndex(); while (securityGen1.CreateSecurity(securityPerm1, security1)) { if (securityPerm1[0] == null) { continue ; } Console.WriteLine("********************************" + "**************************\n"); securityGen2.ResetIndex(); while (securityGen2.CreateSecurity(securityPerm2, security2)) { if (securityPerm2[0] == null) { continue ; } securityPerm3 =((SecurityPermission)( securityPerm1[0].Intersect(securityPerm2[0]))); if (securityPerm3 != null) { Console.WriteLine(("The intersection of " + security1[0] + " and " + security2[0] + " = " + ((SecurityPermission)(securityPerm3)). get_Flags().ToString())); } else { Console.WriteLine(("The intersection of " + security1[0] + " and " + security2[0] + " is null.")); } } } return returnValue ; } //IntersectDemo //Copy creates and returns an identical copy of the current permission. private boolean CopyDemo() { boolean returnValue = true; SecurityPermissionFlag security1[] = new SecurityPermissionFlag[1]; SecurityPermission securityPerm1[] = new SecurityPermission[1]; SecurityPermission securityPerm2[] = new SecurityPermission[1]; SecurityGenerator securityGen1 = new SecurityGenerator(); SecurityGenerator securityGen2 = new SecurityGenerator(); securityGen1.ResetIndex(); while (securityGen1.CreateSecurity(securityPerm1, security1)) { if (securityPerm1[0] == null) { continue ; } securityGen2.ResetIndex(); Console.WriteLine("***************************************" + "*****************\n"); try { securityPerm2[0] =((SecurityPermission)( securityPerm1[0].Copy())); Console.WriteLine(("Result of copy = " + securityPerm2[0].ToString())); } catch(System.Exception e) { Console.WriteLine(("Copy failed :" + securityPerm1[0].ToString() + e)); continue ; } } return returnValue ; } //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 boolean ToFromXmlDemo() { boolean returnValue = true; SecurityPermissionFlag security1[] = new SecurityPermissionFlag[1]; SecurityPermission securityPerm1[] = new SecurityPermission[1]; SecurityPermission securityPerm2[] = new SecurityPermission[1]; SecurityGenerator securityGen1 = new SecurityGenerator(); SecurityGenerator securityGen2 = new SecurityGenerator(); securityGen1.ResetIndex(); while (securityGen1.CreateSecurity(securityPerm1, security1)) { if (securityPerm1[0] == null) { continue ; } Console.WriteLine("*****************************" + "***************************\n"); securityGen2.ResetIndex(); try { securityPerm2[0] = new SecurityPermission(PermissionState.None); securityPerm2[0].FromXml(securityPerm1[0].ToXml()); Console.WriteLine(("Result of ToFromXml = " + securityPerm2[0].ToString())); } catch(System.Exception e) { Console.WriteLine(("ToFromXml failed :" + securityPerm1[0].ToString() + e)); continue ; } } return returnValue ; } //ToFromXmlDemo // Invoke all demos. public boolean runDemo() { boolean returnCode = true; boolean tempReturnCode; // Call the IsSubsetOf demo. tempReturnCode = IsSubsetOfDemo(); if (tempReturnCode) { Console.get_Out().WriteLine("IsSubsetOf demo " + "completed successfully."); } else { Console.get_Out().WriteLine("IsSubsetOf demo failed."); } returnCode = tempReturnCode && returnCode; // Call the Union demo. tempReturnCode = UnionDemo(); if (tempReturnCode) { Console.get_Out().WriteLine("Union demo completed successfully."); } else { Console.get_Out().WriteLine("Union demo failed."); } returnCode = tempReturnCode && returnCode; // Call the Intersect demo. tempReturnCode = IntersectDemo(); if (tempReturnCode) { Console.get_Out().WriteLine("Intersect demo " + "completed successfully."); } else { Console.get_Out().WriteLine("Intersect demo failed."); } returnCode = tempReturnCode && returnCode; // Call the Copy demo. tempReturnCode = CopyDemo(); if (tempReturnCode) { Console.get_Out().WriteLine("Copy demo completed successfully."); } else { Console.get_Out().WriteLine("Copy demo failed."); } returnCode = tempReturnCode && returnCode; // Call the ToFromXml demo. tempReturnCode = ToFromXmlDemo(); if (tempReturnCode) { Console.get_Out().WriteLine("ToFromXml demo " + "completed successfully."); } else { Console.get_Out().WriteLine("ToFromXml demo failed."); } returnCode = tempReturnCode && returnCode; return returnCode ; } //runDemo // Test harness. public static void main(String[] args) { try { SecurityPermissionDemo democase = new SecurityPermissionDemo(); boolean returnCode = democase.runDemo(); if (returnCode) { Console.get_Out().WriteLine("SecurityPermission demo " + "completed successfully."); Console.get_Out().WriteLine("Press the Enter key to exit."); String consoleInput = Console.ReadLine(); System.Environment.set_ExitCode(100); } else { Console.get_Out().WriteLine("SecurityPermission demo failed."); Console.get_Out().WriteLine("Press the Enter key to exit."); String consoleInput = Console.ReadLine(); System.Environment.set_ExitCode(101); } } catch (System.Exception e) { Console.get_Out().WriteLine("SecurityPermission demo failed."); Console.WriteLine(e.ToString()); Console.get_Out().WriteLine("Press the Enter key to exit."); String consoleInput = Console.ReadLine(); System.Environment.set_ExitCode(101); } } //main } //SecurityPermissionDemo // This class generates SecurityPermission objects. class SecurityGenerator { private SecurityPermissionFlag mySecurity[] = { SecurityPermissionFlag.AllFlags, SecurityPermissionFlag.Assertion, SecurityPermissionFlag.ControlAppDomain, SecurityPermissionFlag.ControlDomainPolicy, SecurityPermissionFlag.ControlEvidence, SecurityPermissionFlag.ControlPolicy, SecurityPermissionFlag.ControlPrincipal, SecurityPermissionFlag.ControlThread, SecurityPermissionFlag.Execution, SecurityPermissionFlag.Infrastructure, SecurityPermissionFlag.NoFlags, SecurityPermissionFlag.RemotingConfiguration, SecurityPermissionFlag.SerializationFormatter, SecurityPermissionFlag.SkipVerification, SecurityPermissionFlag.UnmanagedCode }; private int reflectionIndex = 0; public SecurityGenerator() { ResetIndex(); } //SecurityGenerator public void ResetIndex() { reflectionIndex = 0; } //ResetIndex // CreateSecurity creates a SecurityPermission object. public boolean CreateSecurity( SecurityPermission securityPerm[], SecurityPermissionFlag security[]) { if (reflectionIndex >= mySecurity.length) { securityPerm[0] = new SecurityPermission(PermissionState.None); security[0] = SecurityPermissionFlag.NoFlags; reflectionIndex++; return false ; } security[0] = mySecurity[reflectionIndex++]; try { securityPerm[0] = new SecurityPermission(security[0]); return true ; } catch(System.Exception e) { Console.WriteLine(("Cannot create SecurityPermission: " + security[0] + " " + e)); securityPerm[0] = new SecurityPermission(PermissionState.None); security[0] = SecurityPermissionFlag.NoFlags; return true ; } } //CreateSecurity } //SecurityGenerator

System.Security.CodeAccessPermission
System.Security.Permissions.SecurityPermission


Windows 98, Windows 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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- SecurityPermission クラスのページへのリンク