. NET 2.0 introduces a level-1 transparency model (but is only enforced within the Assembly). Transparent Code cannot access key code, even in the same assembly (or in the same type), the following code:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Security;
Using system. Security. permissions;
// Use transparency Model Level 1
[Assembly: securityrules (securityruleset. level1)]
[Assembly: securitycritical]
Namespace mgen
{
Class Program
{
// Transparent
Static void main (string [] ARGs)
{
Doo ();
}
// Key
[Securitycritical, securitytreatassafe]
Static void doo ()
{}
}
}
This code throws an exception:
Unhandled exception: system. methodaccessexception: attempt by security transpare
NT method 'mgen. program. Main (system. String []) 'to access security critical metho
D 'mgen. program. Doo () 'failed.
Obviously, transparent code cannot directly access key code.
In transparency Model Level 1, you can use the securitytreatassafe feature to mark the method and access it normally:
[Securitycritical, securitytreatassafe]
Static void doo ()
{}
However. net 4.0 introduced transparency Model Level 2, security model has undergone many changes, securitytreatassafe is also discarded, replaced by the securitysafecritical feature, directly marked as "reliable security code ".
The above code in. Net 4.0 should be complete:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Security;
Using system. Security. permissions;
//. Net 4.0 uses the transparency Model Level 2 by default
[Assembly: allowpartiallytrustedcallers]
Namespace mgen
{
Class Program
{
// Transparent
Static void main (string [] ARGs)
{
Doo ();
}
// Key to reliability
[Securitysafecritical]
Static void doo ()
{}
}
}