Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot serialize internal class? #3

Closed
itn3000 opened this issue Mar 13, 2017 · 6 comments
Closed

cannot serialize internal class? #3

itn3000 opened this issue Mar 13, 2017 · 6 comments

Comments

@itn3000
Copy link
Contributor

itn3000 commented Mar 13, 2017

Environment

MessagePack assembly version is 1.0.1

here is dotnet --info output

.NET Command Line Tools (1.0.1)

Product Information:
 Version:            1.0.1
 Commit SHA-1 hash:  005db40cd1

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  16.04
 OS Platform: Linux
 RID:         ubuntu.16.04-x64
 Base Path:   /usr/share/dotnet/sdk/1.0.1

Steps to reproduce

compile the following source and run with dotnet run

using System;

namespace msgpack_cli_test
{
    using MessagePack;
    // if this class is public struct, success
    [MessagePackObject]
    struct TestObject
    {
        [Key(0)]
        public int A{get;set;}
        [Key(1)]
        public string B{get;set;}
    }
    class Program
    {
        static void Main(string[] args)
        {
            var obj  = new TestObject()
            {
                A = 100,
                B = "piyopiyo"
            };
            var data = MessagePackSerializer.Serialize(obj);
            Console.WriteLine($"{data.Length}");
            var x = MessagePackSerializer.Deserialize<TestObject>(data);
            Console.WriteLine($"{x.A}");
        }
    }
}

Expected Behavior

output two digits then exit 0

Actual Behavior

output following exception message

Unhandled Exception: System.TypeLoadException: Type 'MessagePack.Formatters.msgpack_cli_test_TestObjectFormatter' from assembly 'MessagePack.Resolvers.DynamicObjectResolver, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is attempting to implement an inaccessible interface.
   at MessagePack.FormatterResolverExtensions.GetFormatterWithVerify[T](IFormatterResolver resolver)
   at MessagePack.MessagePackSerializer.Serialize[T](T obj, IFormatterResolver resolver)
   at msgpack_cli_test.Program.Main(String[] args) in /home/malion/src/msgpack-cli-test/Program.cs:line 23
@neuecc
Copy link
Member

neuecc commented Mar 13, 2017

Yes.
This is assembly builder limitation.
https://msdn.microsoft.com/en-us/library/system.reflection.emit.assemblybuilder(v=vs.110).aspx

MessagePack for C# create assembly per resolver and create dynamic formatter per resolver.

var t = DynamicObjectResolver.Instance.GetFormatter<TestObject>();

// MessagePack.Formatters.TestObjectFormatter
Console.WriteLine(t.GetType().FullName);

// MessagePack.Resolvers.DynamicObjectResolver, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Console.WriteLine(t.GetType().Assembly.FullName);

Created assembly is same as standard assembly so can not access external internal type.

Note: But I should output more informational exception message.

@itn3000
Copy link
Contributor Author

itn3000 commented Mar 13, 2017

Thank you for response,I understand.
I think it's better adding this limitation in README, how about it?

neuecc added a commit that referenced this issue Mar 13, 2017
@neuecc
Copy link
Member

neuecc commented Mar 13, 2017

Thank you, I've added it.
and I'll add varidate and throw informational exception.

@itn3000
Copy link
Contributor Author

itn3000 commented Mar 14, 2017

Great thanks.
Should I close this issue, or leave for fix?

@neuecc neuecc closed this as completed Mar 14, 2017
@neuecc
Copy link
Member

neuecc commented Mar 14, 2017

Thanks, closed.

@asyncx
Copy link

asyncx commented Jun 24, 2017

I don't know if this has been mentioned but you can workaround if you add the following lines to your project

[assembly: InternalsVisibleTo("MessagePack")]
[assembly: InternalsVisibleTo("MessagePack.Resolvers.DynamicObjectResolver")]
[assembly: InternalsVisibleTo("MessagePack.Resolvers.DynamicUnionResolver")]

This way MessagePack.Resolvers.DynamicObjectResolver will be able to access your internal types and avoid the "attempting to implement an inaccessible interface" error.

AbhitejJohn pushed a commit to AbhitejJohn/MessagePack-CSharp that referenced this issue Dec 12, 2018
AArnott pushed a commit that referenced this issue Aug 2, 2022
Revise the fix slightly
AArnott added a commit that referenced this issue Mar 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants