StrongNameIdentityPermission.Union(IPermission) 方法

定义

创建一个权限,该权限是当前权限与指定权限的并集。

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

与当前权限合并的权限。 它必须与当前权限属于同一类型。

返回

表示同时兼具当前权限和指定权限的新权限。

例外

target 参数不为 null,并且与当前权限不属于同一类型。

- 或 -

这两个权限不相等,而且其中一个是另一个的子集。

示例

下面的代码示例演示了使用 Union 方法的结果,而不是如何使用 方法。 此示例是为 类提供的更大示例的一 StrongNameIdentityPermission 部分。 此示例的最佳用途是生成并执行整个示例,并查看其输出。

注意

代码示例旨在显示方法的行为,而不是演示其用法。 通常,安全基础结构使用权限类的方法;它们通常不用于应用程序。

// 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 的结果是权限,该权限表示当前权限和指定权限表示的所有操作。 通过任一权限的任何要求都会通过其联合。

权限 和 null 的联合是不是 null的权限。 权限的并集和该权限的子集是包含另一个的权限。 任何其他组合都会导致 ArgumentException 引发异常。

适用于