I'm using Assembly.GetTypes() for getting all types defined by in a plugin library (so I can instantiate plugin instances).
On a particular library, the method raise a ReflectionTypeLoadException, saying:
Type <Type> is attempting to implement an inaccessible interface
Googling it seems because the specific Type implements a non-public interface. And actually it is, but the Type is nested in another public class, declared as private.
How avoid this exception?
....
Made interface public the code works. Is it possible to define this strange behavior is a bug (in Assembly.GetTypes())? This imply that a library type cannot implement a protected interaface!
The problem was solved by removing the protected nested type from the public type.
I faced same issue while MessagePack serialization. I was trying to implement interface in internal class so it thew this exception "from assembly 'MessagePack.Resolvers.DynamicObjectResolver, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is attempting to implement an inaccessible interface."
So I took that internal class out of its container class in same namespace below is my before and after code...
Before....
using MessagePack;
using System;
namespace ConsoleApp1
{
class CallbackReciever
{
[MessagePackObject]
public class SampleCallback : IMessagePackSerializationCallbackReceiver
{
[Key(0)]
public int Key { get; set; }
public void OnBeforeSerialize()
{
Console.WriteLine("OnBefore");
}
public void OnAfterDeserialize()
{
Console.WriteLine("OnAfter");
}
}
public static void Main()
{
SampleCallback b1 = new SampleCallback { Key = 1 };
Console.WriteLine("Starting serialization");
byte[] data = MessagePackSerializer.Serialize<dynamic>(b1);
foreach (byte b in data)
{
Console.WriteLine(b);
}
SampleCallback temp = MessagePackSerializer.Deserialize<SampleCallback>(data);
Console.WriteLine(MessagePackSerializer.ToJson(temp));
}
}
}
After....
using MessagePack;
using System;
namespace ConsoleApp1
{
[MessagePackObject]
public class SampleCallback : IMessagePackSerializationCallbackReceiver
{
[Key(0)]
public int Key { get; set; }
public void OnBeforeSerialize()
{
Console.WriteLine("OnBefore");
}
public void OnAfterDeserialize()
{
Console.WriteLine("OnAfter");
}
}
class CallbackReciever
{
public static void Main()
{
SampleCallback b1 = new SampleCallback { Key = 1 };
Console.WriteLine("Starting serialization");
byte[] data = MessagePackSerializer.Serialize<dynamic>(b1);
foreach (byte b in data)
{
Console.WriteLine(b);
}
SampleCallback temp = MessagePackSerializer.Deserialize<SampleCallback>(data);
Console.WriteLine(MessagePackSerializer.ToJson(temp));
Console.ReadKey();
}
}
}