Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReflectionTypeLoadException: Type is attempting to implement an inaccessible interface

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!

like image 469
Luca Avatar asked Dec 03 '10 22:12

Luca


2 Answers

The problem was solved by removing the protected nested type from the public type.

like image 177
Luca Avatar answered Nov 17 '22 00:11

Luca


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();
        }
        }
}
like image 22
Vishal Hulawale Avatar answered Nov 17 '22 02:11

Vishal Hulawale