2012-01-31 51 views
5

您可以創建一個.NET 4版本的應用程序進行測試是老闆的無辜問題 - 當然!System.TypeLoadException未處理/覆蓋成員時違反繼承安全規則

但之後我改變了我們的27個項目在我們的WinForms應用程序到.NET 4,並重新編譯,啓動應用程序時,我得到

System.TypeLoadException了未處理
消息= 繼承在覆蓋成員時違反安全規則: 'MyCustomORM.GetObjectData(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext)'。 重寫方法的安全性可訪問性必須與被重寫的方法的安全性可訪問性相匹配。

嗯.....

MyCustomORM確實實現ISerializable接口,因而具有這種方法

[Serializable] 
public abstract class MyCustomORM: IMyCustomORM, ISerializable, ICloneable, ISecurable 
{ 
    public virtual void GetObjectData(SerializationInfo info, StreamingContext context) 
    { 
     // do stuff here....... 
    } 
} 

我也有兩班,從Exception推導覆蓋GetObjectData方法。

但是,這裏可能有什麼錯?周圍的Googling我找到了一些附加的屬性,堅持到我的方法和命名空間 - 所以我做:

[assembly: SecurityPermission(SecurityAction.RequestMinimum, Execution = true)] 
namespace MyApplication.ORM 
{ 
    [Serializable] 
    public abstract class MyCustomORM: IMyCustomORM, ISerializable, ICloneable, ISecurable 
    { 
     [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] 
     public virtual void GetObjectData(SerializationInfo info, StreamingContext context) 
     { 
      // do stuff here....... 
     } 
    } 
} 

但是,這並不能改變什麼.....

異常甚至發生在我的第一行之前在我的static Main()方法代碼達到....

我已經通過這個項目梳理,除去老.NET 1.1庫的引用(是的,應用程序是老.....)和替換它們與他們的.NET 4對應(主要是log4net)。仍然沒有運氣....

任何想法??

+1

有一個「標誌」來控制這種行爲。雖然不記得在哪裏。這個錯誤還表明你不能在那裏使用'virtual'。 – leppie 2012-01-31 11:44:11

+0

另外,'GetObjectData'在抽象類中並沒有什麼意義,因爲你永遠無法重新實例化它(抽象類型的一個實例)。 – leppie 2012-01-31 11:45:38

回答

6

MyCustomORM類所在的程序集標有SecurityTransparentAttribute?如果是這樣,則問題源自.NET 3.5和.NET 4.0之間安全透明模型的更改。對於您的測試場景,您可能希望簡單選擇使用舊的透明度機制。要做到這一點,添加以下程序集級屬性:

[assembly: SecurityRules(SecurityRuleSet.Level1)] 

有關Level1和Level2透明度模型之間的差異的詳細信息,請參閱http://blogs.msdn.com/b/shawnfa/archive/2009/11/12/differences-between-the-security-rule-sets.aspx

+1

所有程序集似乎都具有屬性'[assembly:AllowPartiallyTrustedCallers]' – 2012-01-31 15:09:09

+0

在Level 2規則下,APTCA導致程序集中的所有代碼被視爲透明,除非標記爲關鍵。通過SecurityRulesAttribute指定Level 1規則應該解決您的即時問題,就像您使用SecurityTransparentAttribute一樣。 – 2012-01-31 16:41:18

1

我知道這已經很老了,但我最近遇到了一個我的程序集遇到了這個問題。它只發生在一些機器上,很難確定是什麼原因造成的。我不只是想把安全規則調整到,所以經過很多搜索後,我跑過了Visual Studio附帶的SecAnnotate工具。

Using SecAnnotate to Identify Transparency Violations

使用這個工具,我能夠確定我的組件之一是引用含有這是導致該問題的一些安全屬性的DLL的舊版本。更新參考解決了問題。

SecAnnotate工具似乎是識別您可能意外忽略或不知道的任何違規的好方法。

希望這可以幫助別人。

相關問題