ReflectionTypeLoadException: Type is attempting to implement an inaccessible interface

那年仲夏 提交于 2019-12-19 02:37:35

问题


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!


回答1:


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




回答2:


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();
        }
        }
}


来源:https://stackoverflow.com/questions/4350363/reflectiontypeloadexception-type-is-attempting-to-implement-an-inaccessible-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!