Export (0) Print
Expand All

StrongNameIdentityPermission.Intersect Method

Creates and returns a permission that is the intersection of the current permission and the specified permission.

[Visual Basic]
Overrides Public Function Intersect( _
   ByVal target As IPermission _
) As IPermission Implements IPermission.Intersect
[C#]
public override IPermission Intersect(
 IPermission target
);
[C++]
public: IPermission* Intersect(
 IPermission* target
);
[JScript]
public override function Intersect(
   target : IPermission
) : IPermission;

Parameters

target
A permission to intersect with the current permission. It must be of the same type as the current permission.

Return Value

A new permission that represents the intersection of the current permission and the specified permission. This new permission is a null reference (Nothing in Visual Basic) if the intersection is empty.

Implements

IPermission.Intersect

Exceptions

Exception Type Condition
ArgumentException The target parameter is not a null reference (Nothing in Visual Basic) and is not of the same type as the current permission.

Remarks

The intersection of two permissions is a permission that describes the set of operations they both describe in common. Only a demand that passes both original permissions will pass the intersection.

The intersection of two identical strong name identity permissions is the same permission. The intersection of two different (not wild card) expressions is an empty permission. The intersection of a wild card expression and a matching strong name is the strong name. The intersection of two wild card expressions that match is the longer, more specific of the two expressions.

Example

[Visual Basic, C#, C++] The following example shows the results of the use of the Intersect method, not how to use the method. The best use for this example is to build and execute the entire example, and view its output. This code is part of a larger example. For the complete example, refer to the StrongNameIdentityPermission class.

[Visual Basic] 
' 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 Sn1, Sn2 As String
    Dim SnIdPerm1, SnIdPerm2, SnIdPerm3 As StrongNameIdentityPermission

    Dim SnGen1 As New SnGenerator()
    Dim SnGen2 As New SnGenerator()

    SnGen1.ResetIndex()
    While SnGen1.CreateSn(SnIdPerm1, Sn1)
        If SnIdPerm1 Is Nothing Then
            GoTo ContinueWhile1
        End If
        Console.WriteLine("**********************************************************" + ControlChars.Lf)
        SnGen2.ResetIndex()

        While SnGen2.CreateSn(SnIdPerm2, Sn2)
            If SnIdPerm2 Is Nothing Then
                GoTo ContinueWhile2
            End If
            Dim firstPermission As String = IIf(Sn1 = "" Or Sn1 Is Nothing, "null", Sn1)
            Dim secondPermission As String = IIf(Sn2 = "" Or Sn2 Is Nothing, "null", Sn2)
            Try

                SnIdPerm3 = CType(SnIdPerm1.Intersect(SnIdPerm2), StrongNameIdentityPermission)
                If Not (SnIdPerm3 Is Nothing) AndAlso Not (SnIdPerm3.Name Is Nothing) Then

                    Console.WriteLine(("The intersection of " + firstPermission + ControlChars.Lf + ControlChars.Tab + "and  " + secondPermission + " = " + ControlChars.Lf + ControlChars.Tab + CType(SnIdPerm3, StrongNameIdentityPermission).Name.ToString()))
                Else
                    Console.WriteLine(("The intersection of " + firstPermission + ControlChars.Lf + ControlChars.Tab + "and  " + secondPermission + " is null."))
                End If
            Catch e As Exception
                Console.WriteLine(("An exception was thrown for intersection " + e.ToString()))
                returnValue = False
            End Try
ContinueWhile2:
        End While
ContinueWhile1:
    End While

    Return returnValue
End Function 'IntersectDemo


[C#] 
// 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;

            string sn1,sn2;
            StrongNameIdentityPermission snIdPerm1,snIdPerm2,snIdPerm3;

            SnGenerator snGen1 = new SnGenerator();
            SnGenerator snGen2 = new SnGenerator();

            snGen1.ResetIndex();
            while(snGen1.CreateSn(out snIdPerm1, out sn1)) 
            {
                if(snIdPerm1 == null) continue;
                Console.WriteLine("**********************************************************\n");
                snGen2.ResetIndex();

                while(snGen2.CreateSn(out snIdPerm2, out sn2)) 
                {
                    if(snIdPerm2 == null) continue;
                    string firstPermission = sn1 == "" | sn1 == null ? "null" : sn1 ;
                    string secondPermission = sn2 == "" | sn2 == null ? "null" : sn2;
                    try
                    {
            
                        snIdPerm3 = (StrongNameIdentityPermission)snIdPerm1.Intersect(snIdPerm2);
                        if (snIdPerm3 != null && snIdPerm3.Name != null)
                        {

                            Console.WriteLine("The intersection of " + firstPermission + "\n\tand  "  
                                + secondPermission + " = \n\t" 
                                + ((StrongNameIdentityPermission)snIdPerm3).Name.ToString());
                        }
                        else
                        {
                            Console.WriteLine("The intersection of " + firstPermission + "\n\tand  "  
                                + secondPermission + " is null.");
                        }
                    }
                    catch(Exception e) 
                    {
                        Console.WriteLine("An exception was thrown for intersection "  + e);
                        returnValue=false;
                    }
                }
            }

            return returnValue;

        }

[C++] 
// Intersect creates and returns a new permission that is the intersection of the current
// permission and the permission specified.
bool IntersectDemo() {

   bool returnValue = true;

   String* sn1, *sn2;
   StrongNameIdentityPermission *snIdPerm1, *snIdPerm2, *snIdPerm3;

   SnGenerator* snGen1 = new SnGenerator();
   SnGenerator* snGen2 = new SnGenerator();

   snGen1->ResetIndex();
   while(snGen1->CreateSn(&snIdPerm1, &sn1)) {
      if (snIdPerm1 == 0) continue;
      Console::WriteLine(S"**********************************************************\n");
      snGen2->ResetIndex();

      while(snGen2->CreateSn(&snIdPerm2, &sn2)) {
         if (snIdPerm2 == 0) continue;
         String* firstPermission = sn1->Equals(S"") || sn1 == 0 ? S"null" : sn1 ;
         String* secondPermission = sn2->Equals(S"") || sn2 == 0 ? S"null" : sn2;
         try {
            snIdPerm3 = dynamic_cast<StrongNameIdentityPermission*>(snIdPerm1->Intersect(snIdPerm2));
            if (snIdPerm3 != 0 && snIdPerm3->Name != 0) {
               Console::WriteLine(S"The intersection of {0}\n\tand  {1} = \n\t{2}",
                  firstPermission,
                  secondPermission,
                  (dynamic_cast<StrongNameIdentityPermission*>(snIdPerm3))->Name);
            } else {
               Console::WriteLine(S"The intersection of {0}\n\tand  {1} is null.",
                  firstPermission, secondPermission);
            }
         } catch (Exception* e) {
            Console::WriteLine(S"An exception was thrown for intersection {0}", e);
            returnValue=false;
         }
      }
   }

   return returnValue;

}

[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

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

See Also

StrongNameIdentityPermission Class | StrongNameIdentityPermission Members | System.Security.Permissions Namespace

Show:
© 2014 Microsoft