继承安全规则违反而压倒一切的成员 - SecurityRuleSet.Level2规则、成员、安全、SecurityRuleSet

2023-09-02 01:26:39 作者:只羡鸳鸯不献血

我有一个类继承自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 和安全透明code,级别从MSDN 2 了解详情。

要避免所有潜在的安全运行时异常,启用与安全规则集code分析。你会得到可能对应运行时错误的静态分析警告。

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.