创建自定义codeAccessSecurityAttribute导致异常的编译 [英] Creating custom CodeAccessSecurityAttribute leads to exception on compile

查看:214
本文介绍了创建自定义codeAccessSecurityAttribute导致异常的编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个自定义的codeAccessSecurityAttribute处理授权为我的WCF服务。我建立了A级为这样的:

I'm building a custom CodeAccessSecurityAttribute to handle authorization for my WCF services. I built class A as such:

public class A : CodeAccessSecurityAttribute
{
    public A() : base(SecurityAction.Demand)
    {
        // Constructor Code
    }

    public override IPermission CreatePermission()
    {
        // Permission Creation Code
    }

}

和上编译它产生这种错误。

And on compilation it produces this error.

Error emitting 'A' attribute -- 'Serialized security custom attribute is 
truncated or incorrectly formed.'

玩它过了一会儿,我想出了,它编译没有错误的下一个样本:

After playing with it a little I came up with the next sample that does compile without error:

public class B : CodeAccessSecurityAttribute
{
    public B(SecurityAction Action) : base(Action)
    {
        // Constructor Code
    }

    public override IPermission CreatePermission()
    {
        // Permission Creation Code
    }

}

我知道这是因为SecurityAction枚举不直接引用到A类的公共端,但我想不通的是如何让这个我能做到这一点的A类方法,而不是B类

I know it's because the SecurityAction enum isn't directly referenced to public side of Class A, but what I can't figure out is how to make it so that I can do it the Class A method instead of the Class B.

推荐答案

我不知道要求的确切原因,但<一个href="http://msdn.microsoft.com/en-us/library/system.security.permissions.$c$caccesssecurityattribute.aspx"相对=nofollow>在codeAccessSecurityAttribute MSDN文档明确规定

I don’t know the exact reason for the requirement, but the MSDN documentation on CodeAccessSecurityAttribute clearly states

对继承

从这个类派生的所有权限属性必须只有一个构造函数一个SecurityAction作为其唯一的参数。

All permission attributes derived from this class must have only a single constructor that takes a SecurityAction as its only parameter.

修订:这样做的理由要求是 codeAccessSecurityAttribute 是,从低层次的角度,从其他自定义完全不同属性。一般情况下,自定义属性都存储在 CustomAttribute 表的编制元数据。但是,安全属性,从 SecurityAttribute 导出,分别存储在 DeclSecurity 表。而这个表不包含像CustomAttribute表一般的数据,该表中包含行动值,属性类型的名称,加上一组属性(命名参数)之类的自定义属性的情况下。因此,编译器需要转换的一般定义属性的语法来在该元数据表的条目,因此它需要它跟随的固定形式,如上所述。 (参见这篇博客文章< /一>,或分区第二,部分的 22.11 DeclSecurity:0x0E的的的的通用语言基础结构(CLI)的标准)

Amended: The reason for this requirement is that CodeAccessSecurityAttribute is, from a low-level view, quite different from other custom attributes. Generally, custom attributes are stored in the compiled metadata in the CustomAttribute table. But the security attributes, deriving from SecurityAttribute, are stored separately in the DeclSecurity table. And this table does not contain the general data like the CustomAttribute table, this table contains the value of Action, the name of the attribute type, plus a set of the properties (named arguments) like in the custom attribute case. So, the compiler needs to convert a general custom attribute syntax to an entry in this metadata table, so it needs it to follow the fixed form noted above. (See also this blog post, or Partition II, section 22.11 DeclSecurity : 0x0E of the Common Language Infrastructure (CLI) standard.)

这篇关于创建自定义codeAccessSecurityAttribute导致异常的编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆