我正在使用LINQPad来测试代码(我必须说这是一个很棒的产品),但是现在当我尝试将Thread.CurrentPrincipal设置为标有SerializableAttribute的自定义IPrincipal时遇到了异常
跟随一个演示问题的样本
void Main()
{
Thread.CurrentPrincipal = new MyCustomPrincipal();
}
// Define other methods and classes here
[Serializable]
public class MyCustomPrincipal : IPrincipal
{
public bool IsInRole(string role)
{
return true;
}
public IIdentity Identity
{
get
{
return new WindowsIdentity("RECUPERA\\m.casamento");
}
}
}
当我在LINQPad(C#程序作为语言)中运行此代码时,出现以下异常
Type is not resolved for member 'UserQuery+MyCustomPrincipal,query_nhxfev, Version=0.0.0.0,
Culture=neutral, PublicKeyToken=null'
RuntimeMethodInfo: PluginWindowManager.get_Form ()
如果我删除Serializable属性,一切都会很好。似乎与LINQPad使用的AppDomain架构相关的问题以及该框架无法找到定义MyCustomPrincipal的程序集。
另外,我相信将MyCustomPrincipal定义到另一个程序集中并将其放入GAC可以解决该问题,但这对我来说不是一个选择。
有人有主意吗?
谢谢,
马可
编辑:我不知道它是否有帮助,但是我在SqlDependency.Start上遇到了同样的问题。开始:将Serializable放在IPrincipal上会使框架抛出错误,提示它找不到定义该组件的程序集。 IPrincipal的类型。我已经解决了一个卑鄙的骇客:
System.Security.Principal.IPrincipal principal;
principal = System.Threading.Thread.CurrentPrincipal;
System.Threading.Thread.CurrentPrincipal = null;
try
{
SqlDependency.Start(connectionString);
m_SqlDependencyStarted = true;
}
catch (Exception ex)
{
throw (ex);
}
finally
{
System.Threading.Thread.CurrentPrincipal = principal;
}
最佳答案
您可以使用的一种技巧是对包含自定义主体的程序集进行强命名,然后将其放入“全局程序集缓存”中。这样,任何应用程序都可以找到所需的程序集,这不是理想的解决方案,因为您随后必须再次将其删除。无论如何,如果已安装Visual Studio,则要安装到GAC中,请运行VS命令提示符并运行以下命令:gacutil -i nameofassembly.dll
关于c# - linqpad和自定义IPrincipal可序列化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11472033/