StrongNameIdentityPermission.Union(IPermission) 方法

定義

建立目前權限與指定權限聯集的權限。Creates a permission that is the union of the current permission and the specified permission.

public:
 override System::Security::IPermission ^ Union(System::Security::IPermission ^ target);
public override System.Security.IPermission Union (System.Security.IPermission target);
override this.Union : System.Security.IPermission -> System.Security.IPermission
Public Overrides Function Union (target As IPermission) As IPermission

參數

target
IPermission

要與目前權限結合的權限,A permission to combine with the current permission. 必須與目前權限屬於相同的類型。It must be of the same type as the current permission.

傳回

IPermission

代表目前權限與指定權限聯集的新權限。A new permission that represents the union of the current permission and the specified permission.

例外狀況

target 參數不是 null,而且與目前權限屬於不同的類型。The target parameter is not null and is not of the same type as the current permission.

-或--or- 兩個權限不相等,且其中一個權限是另一個權限的子集。The two permissions are not equal and one is a subset of the other.

範例

下列程式碼範例會顯示使用方法的結果 Union ,而不是如何使用方法的結果。The following code example shows the results of the use of the Union method, not how to use the method. 這個範例是針對類別提供之較大範例的一部分 StrongNameIdentityPermissionThis example is part of a larger example provided for the StrongNameIdentityPermission class. 此範例的最佳用途是建立並執行整個範例,並查看其輸出。The best use for this example is to build and execute the entire example, and view its output.

注意

程式碼範例的目的是要顯示方法的行為,而不是示範其用途。The code example is intended to show the behavior of the method, not to demonstrate its use. 一般而言,安全性基礎結構會使用許可權類別的方法;它們通常不會在應用程式中使用。In general, the methods of permission classes are used by the security infrastructure; they are not typically used in applications.

// Union creates a new permission that is the union of the current permission and the specified permission.
bool UnionDemo()
{
    bool returnValue = true;
    StrongNameIdentityPermission^ snIdPerm1;
    StrongNameIdentityPermission^ snIdPerm2;
    IPermission^ snIdPerm3;
    snIdPerm1 = gcnew StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.*", gcnew Version("1.0.0.0"));
    snIdPerm2 = gcnew StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.MyFile", gcnew Version("1.0.0.0"));
    snIdPerm3 = dynamic_cast<StrongNameIdentityPermission^>(snIdPerm1->Union( snIdPerm2 ));
    snIdPerm3 = snIdPerm1->Union( snIdPerm2 );

    try
    {
        Console::WriteLine("The union of MyCompany.MyDepartment.*" +
            "and MyCompany.MyDepartment.MyFile is " +
            (dynamic_cast<StrongNameIdentityPermission^>(snIdPerm3))->Name); 
    }
    catch (Exception^ e)
    {
        Console::WriteLine("An expected exception was thrown: " + e->Message);
    }


    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;

    StrongNameIdentityPermission snIdPerm1, snIdPerm2;
    IPermission snIdPerm3;

    snIdPerm1 = new StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.*", new Version("1.0.0.0"));
    snIdPerm2 = new StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.MyFile", new Version("1.0.0.0"));

    snIdPerm3 = (StrongNameIdentityPermission)snIdPerm1.Union(snIdPerm2);

    try
    {
        Console.WriteLine("The union of MyCompany.MyDepartment.*" +
        "and MyCompany.MyDepartment.MyFile is " +
        ((StrongNameIdentityPermission)snIdPerm3).Name.ToString());
    }
    catch (Exception e)
    {
        Console.WriteLine("An expected exception was thrown: " + e.Message);
    }

    return returnValue;
}
' 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 snIdPerm1, snIdPerm2 As StrongNameIdentityPermission
    Dim snIdPerm3 As IPermission
    
    snIdPerm1 = New StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.*", New Version("1.0.0.0"))
    snIdPerm2 = New StrongNameIdentityPermission(blob, "MyCompany.MyDepartment.MyFile", New Version("1.0.0.0"))
    
    snIdPerm3 = CType(snIdPerm1.Union(snIdPerm2), StrongNameIdentityPermission)
    
    Try
        Console.WriteLine("The union of MyCompany.MyDepartment.*" + "and MyCompany.MyDepartment.MyFile is " + CType(snIdPerm3, StrongNameIdentityPermission).Name.ToString())
    Catch e As Exception
        Console.WriteLine("An expected exception was thrown: " + e.Message)
    End Try
    
    
    Return returnValue

End Function 'UnionDemo
 

備註

呼叫的結果 Union 是代表由目前許可權和指定的許可權所表示之所有作業的許可權。The result of a call to Union is a permission that represents all the operations represented by both the current permission and the specified permission. 任何通過任一許可權的要求都會傳遞其聯集。Any demand that passes either permission passes their union.

許可權的聯集,而且 null 是不是的許可權 nullThe union of a permission and null is the permission that is not null. 許可權的聯集和該許可權的子集是包含另一個許可權的許可權。The union of a permission and a subset of that permission is the permission that contains the other. 任何其他組合都會導致擲回 ArgumentException 例外狀況。Any other combination results in an ArgumentException exception being thrown.

適用於