I have a partially trusted AppDomain Sandbox, I execute method X
, which instantiates MyException:
[Serializable]
class MyException: Exception
{
public MyException():base("My exception") { }
protected MyException(SerializationInfo serializationInfo, StreamingContext streamingContext)
: base(serializationInfo, streamingContext) { }
[SecurityCritical]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
Console.WriteLine("A line");
}
}
Assembly A
, which contains MyException
is NOT loaded with full trust into the Sandbox. When X
is executed in Sandbox, TypeLoadException
occurs:
Inheritance security rules violated while overriding member: MyException.GetObjectData .
I thought, that having [SecurityCritical]
on GetObjectData
is what it requires, but it appears to have no effect.
If I add [assembly: AllowPartiallyTrustedCallers]
to A
, and load A
with full trust into Sandbox, this code works fine. However, A
contains many classes, and I only face this problem with this particular GetObjectData
.
Without reverting to older security models, is there a way to make MyException
work without AllowPartiallyTrustedCallers
and without loading it with full trust?