覆盖成员时违反了继承安全规则 - SecurityRuleSet.Level2 [英] Inheritance security rules violated while overriding member - SecurityRuleSet.Level2
问题描述
我有一个继承自 Exception 的类.在 .NET 4 中,我开始收到运行时错误:
I have a class that inherits from Exception. In .NET 4, I started receiving a runtime error:
违反了继承安全规则而覆盖成员:MyBusinessException.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)'.安全覆盖方法的可访问性必须匹配安全可访问性被覆盖的方法.
Inheritance security rules violated while overriding member: MyBusinessException.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.
我认为问题是由于我覆盖了 GetObjectData.
I think the issue is caused by the fact that I am overriding GetObjectData.
我知道解决问题的一个答案是设置 SecurityRuleSet:
I know one answer for resolving the issue is to set the SecurityRuleSet:
[assembly: SecurityRules(SecurityRuleSet.Level1)]
这不是一个可接受的答案,我想知道如何解决该问题而不必放宽 .NET 4 中的默认安全规则.
This is not an acceptable answer, I'd like to know how to fix the issue without having to relax the default security rules in .NET 4.
推荐答案
将 GetObjectData
标记为 SecurityCriticalAttribute
,因为它应用于 Exception.GetObjectData
.被覆盖的成员应具有相同的安全可访问性(关键、安全关键或透明).
Mark GetObjectData
with SecurityCriticalAttribute
, because it's applied to Exception.GetObjectData
. An overridden member should have the same security accessibility (Critical, Safe Critical or Transparent).
阅读 .NET Framework 中的安全更改4 和 安全透明代码,级别 2 来自 MSDN,用于更多信息.
Read Security Changes in the .NET Framework 4 and Security Transparent Code, Level 2 from MSDN for more information.
为避免所有潜在的安全运行时异常,请使用安全规则集启用代码分析.您将收到可能与运行时错误相对应的静态分析警告.
To avoid all potential security runtime exceptions, enable Code Analysis with the Security rule set. You'll get static analysis warnings that might correspond to runtime errors.
这篇关于覆盖成员时违反了继承安全规则 - SecurityRuleSet.Level2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!