.net TypeAccessException:按方法尝试。。。要访问类型。。。失败

.net TypeAccessException:按方法尝试。。。要访问类型。。。失败,.net,.net,所有例外情况详情如下: System.TypeAccessException occurred Message=Attempt by method 'DynamicClass.(System.Text.StringBuilder, System.Object, Int32)' to access type 'System.Collections.Generic.IEnumerable`1<System.Collections.Generic.KeyValuePair`2<Syst

所有例外情况详情如下:

System.TypeAccessException occurred
  Message=Attempt by method 'DynamicClass.(System.Text.StringBuilder, System.Object, Int32)' to access type 'System.Collections.Generic.IEnumerable`1<System.Collections.Generic.KeyValuePair`2<System.Object,NetworkCatcher.Entities.Server.CalculatedFCStateManager+Descriptor>>' failed.
  Source=Anonymously Hosted DynamicMethods Assembly
  TypeName=""
  StackTrace:
       at (StringBuilder , Object , Int32 )
  InnerException: 
堆栈跟踪是:

[Lightweight Function]  
Shunra.Common.dll!Shunra.Common.DebugUtils.AppendObject<System.Collections.Generic.IDictionary<object,NetworkCatcher.Entities.Server.CalculatedFCStateManager.Descriptor>>(System.Text.StringBuilder sb, System.Collections.Generic.IDictionary<object,NetworkCatcher.Entities.Server.CalculatedFCStateManager.Descriptor> obj, int maxItemsToDisplay) Line 261 + 0x14 bytes    C#
Shunra.Common.dll!Shunra.Common.DebugUtils.ToString<System.Collections.Generic.IDictionary<object,NetworkCatcher.Entities.Server.CalculatedFCStateManager.Descriptor>>(System.Collections.Generic.IDictionary<object,NetworkCatcher.Entities.Server.CalculatedFCStateManager.Descriptor> obj, int maxItemsToDisplay) Line 435 + 0x4d bytes  C#
NC.Entities.Server.dll!NetworkCatcher.Entities.Server.CalculatedFCStateManager.Populate() Line 98 + 0x2c bytes  C#
NC.Entities.Server.dll!NetworkCatcher.Entities.Server.RunManager.Initialize() Line 500 + 0x1c bytes C#
NC.Entities.Server.dll!NetworkCatcher.Entities.Server.EntryPoint.OnStart() Line 63 + 0x5 bytes  C#
Shunra.Infra.dll!Shunra.Infra.EntryPoint.Start() Line 37 + 0xb bytes    C#
Shunra.Common.dll!Shunra.Common.Wcf.ShunraServiceHost.OnOpening() Line 47 + 0x11 bytes  C#
System.ServiceModel.dll!System.ServiceModel.Channels.CommunicationObject.Open(System.TimeSpan timeout) + 0x113 bytes    
System.ServiceModel.dll!System.ServiceModel.Channels.CommunicationObject.Open() + 0x25 bytes    
NC.Server.Host.exe!NetworkCatcher.Server.Host.NCServerInstance.StartHosts() + 0xef bytes    
Shunra.Common.dll!Shunra.Common.Wcf.ShunraServerInstance<NetworkCatcher.Server.Host.NCServerInstance,NetworkCatcher.Server.Host.ServerHost>.Run() Line 357 + 0xb bytes  C#
NC.Server.Host.exe!NetworkCatcher.Server.Host.Program.Main(string[] args) + 0x84 bytes  
[轻量级功能]
Shunra.Common.dll!Shunra.Common.DebugUtils.AppendObject(System.Text.StringBuilder sb,System.Collections.Generic.IDictionary obj,int-maxItemsToDisplay)第261行+0x14字节C#
Shunra.Common.dll!Shunra.Common.DebugUtils.ToString(System.Collections.Generic.IDictionary obj,int-maxItemsToDisplay)行435+0x4d字节C#
NC.Entities.Server.dll!NetworkCatcher.Entities.Server.CalculatedFCStateManager.Populate()行98+0x2c字节C#
NC.Entities.Server.dll!NetworkCatcher.Entities.Server.RunManager.Initialize()行500+0x1c字节C#
NC.Entities.Server.dll!NetworkCatcher.Entities.Server.EntryPoint.OnStart()行63+0x5字节C#
Shunra.Infra.dll!Shunra.Infra.EntryPoint.Start()行37+0xb字节C#
Shunra.Common.dll!Shunra.Common.Wcf.ShunraServiceHost.onOpen()行47+0x11字节C#
System.ServiceModel.dll!System.ServiceModel.Channel.CommunicationObject.Open(System.TimeSpan超时)+0x113字节
System.ServiceModel.dll!System.ServiceModel.Channel.CommunicationObject.Open()+0x25字节
NC.Server.Host.exe!NetworkCatcher.Server.Host.NCServerInstance.StartHosts()+0xef字节
Shunra.Common.dll!Shunra.Common.Wcf.ShunraServerInstance.Run()行357+0xb字节C#
NC.Server.Host.exe!NetworkCatcher.Server.Host.Program.Main(字符串[]args)+0x84字节
最后,两个程序集-Shunra.Common.dll和NC.Entities.Server.dll属于同一代码库。NC.Entities.Server.dll从根文件夹(Shunra.Common.dll所在位置)正下方的专用bin文件夹加载

一切正常,直到我决定在另一个帐户下运行代码。因此,我创建了一个新帐户,使其成为管理员,并在该帐户下打开了一个提升的控制台窗口。
该帐户可以访问所有文件。不过,当我运行应用程序时,我会遇到这个错误,对此我完全不知所措。

您可能会通过运行fuslogvw(Fusion日志查看器)了解更多关于发生了什么的信息。如果您不熟悉这一点,它可以让您记录所有绑定和尝试绑定到.NET程序集的情况—它将告诉您查找程序集的所有路径,并可能为您提供绑定失败的更多原因。如果你熟悉它,你可以忽略我要说的其余部分

如果这不是绑定失败,至少消除了一种可能性

要运行查看器,请从Visual Studio CMM和提示符中fuslogvw,并将设置从“日志禁用”更改为“日志绑定失败到磁盘”或“日志所有绑定到磁盘”(根据首选项)。再次运行代码并单击“刷新”。双击条目以查看详细信息


确保在完成时禁用日志-fuslogvw正在监视所有进程的绑定,如果让它继续运行,您的计算机将1)运行缓慢,2)充满日志

我最近遇到了一个类似的问题,结果证明我调用的类型(即
MethodInfo.DeclaringType
)是内部的。将其设置为public后,类型访问成功。

我遇到了类似的异常,异常的症状是围绕委托类型并在我的Moq'ed类上使用CallBase=true

对我来说,解决这个问题的方法是在范围内公开委托,以便通过Moq可以看到它。我们最初将委托的作用域设置为受保护,我收到了这个异常,因此似乎需要公共作用域

就原来的问题而言,我认为你需要改变

private delegate StringBuilder AppendToStringBuilderDelegate(StringBuilder sb, object obj, int maxItemsToDisplay);


好的,我运行了fuslogvw,日志绑定失败,日志全部失败。没有看到任何异常,但如果您有任何具体想法-请告诉我发布什么,我会发布。没有什么比查看应用程序的任何绑定是否失败更具体的了-但是您必须双击主窗口中的条目以查看每个绑定的详细信息。如果在实例化DynamicMethod时使用参数'restrictedSkipVisibility:true',也可以解决这个可见性问题。调用程序集和正在访问的类型之间的信任级别应该是兼容的,但值得一试。
private delegate StringBuilder AppendToStringBuilderDelegate(StringBuilder sb, object obj, int maxItemsToDisplay);
public delegate StringBuilder AppendToStringBuilderDelegate(StringBuilder sb, object obj, int maxItemsToDisplay);