.NET 应用程序如何检测其运行所在的信任级别?
具体来说,我想要一种方法来做类似的事情
if (RUNNING IN GREATER THAN MEDIUM TRUST) {
// set private fields & properties using reflection
}
我目前的解决方案是使用
public static class CodeAccessSecurityTool {
private static volatile bool _unrestrictedFeatureSet = false;
private static volatile bool _determinedUnrestrictedFeatureSet = false;
private static readonly object _threadLock = new object();
public static bool HasUnrestrictedFeatureSet {
get {
if (!_determinedUnrestrictedFeatureSet)
lock (_threadLock) {
if (!_determinedUnrestrictedFeatureSet) {
try {
// See if we're running in full trust
new PermissionSet(PermissionState.Unrestricted).Demand();
_unrestrictedFeatureSet = true;
} catch (SecurityException) {
_unrestrictedFeatureSet = false;
}
_determinedUnrestrictedFeatureSet = true;
}
}
return _unrestrictedFeatureSet;
}
}
}
但是,这有点骇人听闻。