CodeAccessSecurityAttribute 类

为代码访问安全性指定基属性类。

**命名空间:**System.Security.Permissions
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<AttributeUsageAttribute(AttributeTargets.Assembly Or AttributeTargets.Class Or AttributeTargets.Struct Or AttributeTargets.Constructor Or AttributeTargets.Method, AllowMultiple:=True, Inherited:=False)> _
Public MustInherit Class CodeAccessSecurityAttribute
    Inherits SecurityAttribute
用法
Dim instance As CodeAccessSecurityAttribute
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method, AllowMultiple=true, Inherited=false)] 
public abstract class CodeAccessSecurityAttribute : SecurityAttribute
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets::Assembly|AttributeTargets::Class|AttributeTargets::Struct|AttributeTargets::Constructor|AttributeTargets::Method, AllowMultiple=true, Inherited=false)] 
public ref class CodeAccessSecurityAttribute abstract : public SecurityAttribute
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method, AllowMultiple=true, Inherited=false) */ 
public abstract class CodeAccessSecurityAttribute extends SecurityAttribute
SerializableAttribute 
ComVisibleAttribute(true) 
AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method, AllowMultiple=true, Inherited=false) 
public abstract class CodeAccessSecurityAttribute extends SecurityAttribute

备注

该属性类使 SecurityAction(例如,Demand)与自定义安全属性相关联。

CodeAccessSecurityAttribute 派生的类型用于帮助限制对资源或安全操作的访问。

安全属性所声明的安全信息存储在属性目标的元数据中,系统在运行时会访问这些信息。安全属性仅用于声明性安全。对于强制性安全,应使用从 CodeAccessPermission 派生的相应权限类。

给继承者的说明 所有从该类派生的权限属性必须只有一个将 SecurityAction 作为其唯一参数的构造函数。

示例

下面的示例显示从 CodeAccessSecurityAttribute 类派生的权限属性。

Imports System
Imports System.IO
Imports System.Runtime.Remoting
Imports System.Security
Imports System.Security.Permissions
Imports System.Reflection
Imports MyPermission
Imports Microsoft.VisualBasic


' Use the command line option '/keyfile' or appropriate project settings to sign this assembly.
<Assembly: System.Security.AllowPartiallyTrustedCallersAttribute()> 
Namespace MyPermissionAttribute

    <AttributeUsage(AttributeTargets.All, AllowMultiple:=True, Inherited:=False)> Public NotInheritable Class NameIdPermissionAttribute
        Inherits CodeAccessSecurityAttribute
        Private m_Name As String = Nothing
        Private m_unrestricted As Boolean = False


        Public Sub New(ByVal action As SecurityAction)
            MyBase.New(action)
        End Sub 'New


        Public Property Name() As String
            Get
                Return m_name
            End Get
            Set(ByVal Value As String)
                m_name = Value
            End Set
        End Property

        Public Overrides Function CreatePermission() As IPermission
            If m_unrestricted Then
                Throw New ArgumentException("Unrestricted permissions not allowed in identity permissions.")
            Else
                If m_name Is Nothing Then
                    Return New NameIdPermission(PermissionState.None)
                End If
                Return New NameIdPermission(m_name)
            End If
        End Function 'CreatePermission
    End Class ' NameIdPermissionAttribute
End Namespace
using System;
using System.IO;
using System.Runtime.Remoting;
using System.Security;
using System.Security.Permissions;
using System.Reflection;
using MyPermission;
// Use the command line option '/keyfile' or appropriate project settings to sign this assembly.
[assembly: System.Security.AllowPartiallyTrustedCallersAttribute ()]

namespace MyPermissionAttribute
{
    [AttributeUsage (AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
    [Serializable]
    sealed public class  NameIdPermissionAttribute : CodeAccessSecurityAttribute
    {
        private String m_Name = null;
        private bool m_unrestricted = false;

        public  NameIdPermissionAttribute (SecurityAction action): base( action )
        {
        }

        public String Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }

        public override IPermission CreatePermission ()
        {
            if (m_unrestricted)
            {
                throw new ArgumentException ("Unrestricted permissions not allowed in identity permissions.");
            }
            else
            {
                if (m_Name == null)
                    return new  NameIdPermission (PermissionState.None);

                return new  NameIdPermission (m_Name);
            }
        }
    }
}

继承层次结构

System.Object
   System.Attribute
     System.Security.Permissions.SecurityAttribute
      System.Security.Permissions.CodeAccessSecurityAttribute
         派生类

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

CodeAccessSecurityAttribute 成员
System.Security.Permissions 命名空间
CodeAccessPermission 类

其他资源

利用属性扩展元数据