当继承SafeHandle时,我们是否应该重新应用ReliabilityContract属性?
在 关于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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您必须重新应用该属性,因为 ReliabilityContract 的继承属性设置为 false,这意味着派生类中的方法不会应用该属性。
看看下面的代码。如果将
Inherited
命名参数设置为false
,则派生类中的 Method1 不会应用该属性。之后,将相同的参数 (Inherited) 设置为true
并再次运行。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 tofalse
, the Method1 in the derived class does not have the attribute applied. After that, set the same parameter (Inherited) totrue
and run it again.