当继承SafeHandle时,我们是否应该重新应用ReliabilityContract属性?

发布于 2024-09-06 15:11:50 字数 993 浏览 1 评论 0原文

关于SafeHandles的.Net安全博客文章,其中提到需要将ReliabilityContract属性应用到关闭句柄的本机方法的签名上。

当我们继承 SafeHandle 时,我们必须声明一个构造函数、ReleaseHandle 方法和 IsInvalid 属性,所有这些都在基类中应用了 ReliabilityContract(我使用 Reflector 查看了 SafeHandle):

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
protected SafeHandle(IntPtr invalidHandleValue, bool ownsHandle);

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
protected abstract bool ReleaseHandle();

public abstract bool IsInvalid { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] get; }

ReliabilityContract 有 其继承属性设置为 false - 我think 意味着我们重写的方法将不再具有该属性——那么,我们需要重新应用该属性吗?

In the .Net security blog article on SafeHandles, it mentions that you need to apply the ReliabilityContract attribute to the signature of the native method that closes the handle.

When we inherit from SafeHandle we have to declare a constructor, ReleaseHandle method and IsInvalid property, all of which have the ReliabilityContract applied in the base class (I used Reflector to have a look at SafeHandle):

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
protected SafeHandle(IntPtr invalidHandleValue, bool ownsHandle);

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
protected abstract bool ReleaseHandle();

public abstract bool IsInvalid { [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] get; }

ReliabilityContract has its inherited property set to false - which I think means that the methods we override will no longer have the attribute -- so, do we need to re-apply the attribute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Smile简单爱 2024-09-13 15:11:50

是的,您必须重新应用该属性,因为 ReliabilityContract 的继承属性设置为 false,这意味着派生类中的方法不会应用该属性。

看看下面的代码。如果将 Inherited 命名参数设置为 false,则派生类中的 Method1 不会应用该属性。之后,将相同的参数 (Inherited) 设置为 true 并再次运行。

[AttributeUsage(AttributeTargets.Method, Inherited=false)]
public class MyAttribute : Attribute { }

class BaseClass
{
    [My] // MyAttribute applied to base class
    public virtual void Method1() { }
}

class DerivatedClass : BaseClass
{
    // MyAttribute not applied to derivate class
    public override void Method1() { }
}

public class Program
{
    static void Main(string[] args)
    {
        var attributes = typeof(DerivatedClass)
            .GetMethod("Method1")
            .GetCustomAttributes(true);

        foreach (var attr in attributes)
        {
            Console.Write(attr.ToString());
        }
    }
} 

Yes, you have to re-apply the attribute because the ReliabilityContract has its inherited property set to false and that means methods in derived classes won't have the attribute applied.

Take a look at the below code. If you set the Inherited named parameter to false, the Method1 in the derived class does not have the attribute applied. After that, set the same parameter (Inherited) to true and run it again.

[AttributeUsage(AttributeTargets.Method, Inherited=false)]
public class MyAttribute : Attribute { }

class BaseClass
{
    [My] // MyAttribute applied to base class
    public virtual void Method1() { }
}

class DerivatedClass : BaseClass
{
    // MyAttribute not applied to derivate class
    public override void Method1() { }
}

public class Program
{
    static void Main(string[] args)
    {
        var attributes = typeof(DerivatedClass)
            .GetMethod("Method1")
            .GetCustomAttributes(true);

        foreach (var attr in attributes)
        {
            Console.Write(attr.ToString());
        }
    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文