Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeAccessException: Attempt by method ... to access type ... failed

Tags:

.net

The full exception details are:

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: 

The method name DynamicClass.(System.Text.StringBuilder, System.Object, Int32) corresponds to this dynamically generated method:

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

private static AppendToStringBuilderDelegate EmitDelegate(Type type, MethodInfo methodInfo)
{
  var dynamicMethod = new DynamicMethod(string.Empty, typeof(StringBuilder), TypeArray<StringBuilder, object, int>.Value);
  var parameters = methodInfo.GetParameters();

  var il = dynamicMethod.GetILGenerator();
  il.Emit(OpCodes.Ldarg_0);
  il.Emit(OpCodes.Ldarg_1);
  il.Emit(OpCodes.Unbox_Any, parameters[1].ParameterType);
  il.Emit(OpCodes.Ldarg_2);
  il.EmitCall(OpCodes.Call, methodInfo, null);
  il.Emit(OpCodes.Ret);
  return s_methodCache[type] = (AppendToStringBuilderDelegate)dynamicMethod.CreateDelegate(typeof(AppendToStringBuilderDelegate));
}

The stack trace is:

[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  

Finally, the two assemblies - Shunra.Common.dll and NC.Entities.Server.dll belong to the same codebase. NC.Entities.Server.dll is loaded from the private bin folder located immediately below the root folder, where Shunra.Common.dll lives.

Everything worked just fine, until I decided to run the code under another account. So, I created a new account, made it an admin and opened an elevated console window under this account. The account has access to all the files. Still, when I run the application I get this error and I am completely lost at it.

like image 850
mark Avatar asked Jun 15 '11 10:06

mark


1 Answers

I had a similar issue recently and it turned out that the type I called (i.e. MethodInfo.DeclaringType) was internal. After setting it to public, the type access succeeded.

like image 142
Munir Husseini Avatar answered Sep 18 '22 15:09

Munir Husseini