代码之家  ›  专栏  ›  技术社区  ›  Wim Coenen

当属性getter/setter需要LinkDemand时,如何安抚FxCop?

  •  3
  • Wim Coenen  · 技术社区  · 15 年前

    public int Foo
    {
       get
       {
          return GetFoo();
       }
       set
       {
          SetFoo(value);
       }
    }
    

    二者都 GetFoo SetFoo 装饰有:

    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
    

    因此,FxCop正确地抱怨Foo属性(或者说它的隐式getter和setter方法)没有相同的LinkDemand:

    CA2122:Microsoft。安全性: 'Bar.Foo.get()'调用 “Bar.GetFoo()”具有LinkDemand。 通过进行此调用,“Bar.GetFoo()”是 间接暴露于用户代码。 请查看以下调用堆栈: 安全保护:

    SecurityPermission 属性来修复此警告,结果表明属性不是此属性的有效目标。


    编辑 :回应Eric Lippert的评论“到底是为什么?”?
    1. 我使用 Marshal.getiunknownfobject,其中 具有对非托管代码的LinkDemand权限。
    2. 我运行FxCop,它向CA2122投诉
    3. 我用谷歌搜索CA2122寻找线索 关于错误的含义和解决方法 解决它
    4. 首先 first google hit 看到了Micheal Fanning的建议 LinkDemand以解决该错误

    读了你的反应后,我很快猜到范宁的建议不适用于我的情况。我现在看了一下 Demand vs. LinkDemand 文章,并将尝试使用需求替代。

    2 回复  |  直到 9 年前
        1
  •  6
  •   Eric Rosenberger    15 年前

    您应该能够直接将该属性应用于getter和setter,即:

    public int Foo
    {
       [SecurityPermission(...)]
       get
       {
          return GetFoo();
       }
    
       [SecurityPermission(...)]
       set
       {
          SetFoo(value);
       }
    }
    
        2
  •  0
  •   Community Egal    7 年前

    仅供参考,您可以使用

    [PermissionSetAttribute(SecurityAction.LinkDemand,Name=“FullTrust”)]

    见第页: what does this security warning mean (.Net Process class)?