Wednesday, August 29, 2012

Class defined in Portable Class Library leads to TypeInitializationException when referenced in Silverlight

Class defined in Portable Class Library leads to TypeInitializationException when referenced in Silverlight

We have a class, WeakRef, that inherits from WeakReference. This class was originally part of a namespace that targets the standard .NET framework. Recently, we have migrated to .NET 4.5 and decided utilize the Portable Class Library. We included WeakRef, but referencing it in our Silverlight application has led to the following runtime exception:

  • $exception {System.TypeInitializationException: The type initializer for 'AxisModel.ImageDownloadAlert' threw an exception. ---> System.TypeLoadException: Inheritance security rules violated by type: 'Escalon.Common.WeakRef`1'. Derived types must either match the security accessibility of the base type or be less accessible. If the base class has a non-transparent default constructor, the derived class must also have a default constructor, and the method inheritance rules apply across those two methods.

    at Escalon.Common.WeakRefCallbackHostCore`1..ctor(Object syncRoot)
    at AxisModel.ImageDownloadAlert..ctor()
    at AxisModel.ImageDownloadAlert..cctor()

    --- End of inner exception stack trace ---
    at AxisModel.ImageDownloadAlert.get_Instance()
    at SLWebApp.App..ctor()}

    System.Exception {System.TypeInitializationException}

I noticed that the Silverlight version of WeakReference has an empty default constructor, which is not the case in the standard .NET version of WeakReference. I think that this is the reason for the thrown exception, since WeakRef does not contain an empty constructor. The problem is that adding an empty constructor to this class will lead to compilation errors in our WPF applications.

Is there a way to share WeakRef between our Silverlight and WPF applications without leading to runtime and compilation errors? I'm looking for minimally invasive solutions.

Answers & Comments...




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog