Search code examples
c#unity-game-engineunity3d-mirror

Does Mirror support internal classes?


I'm trying to write serializers for custom types as per documentation.

Is it possible to use InternalsVisibleToAttribute to make internal classes visible to the Mirror assemblies so it can access my custom data types and serializers?

This is what I've tried:

[assembly:InternalsVisibleTo("Mirror")]
[assembly:InternalsVisibleTo("Mirror.Authenticators")]
[assembly:InternalsVisibleTo("Mirror.CompilerSymbols")]
[assembly:InternalsVisibleTo("Mirror.Components")]
[assembly:InternalsVisibleTo("Mirror.Editor")]
[assembly:InternalsVisibleTo("Mirror.Transports")]
[assembly:InternalsVisibleTo("Unity.Mirror.CodeGen")]

namespace MyNamespace.InternalStuff
{ 
    [Serializable]
    internal class MyInternalClass
    { \\ ...

but I always get a

MethodAccessException: Method `MyNamespace.InternalStuff. MyInternalClassSerializer.WriteItem(
Mirror.NetworkWriter,MyNamespace.InternalStuff.MyInternalClass)'
is inaccessible from method `Mirror.GeneratedNetworkCode.InitReadWriters()'

(which I don't know which assembly it belongs to)

I successfully used InternalsVisibleToAttribute the same way to expose these classes to my unit test assembly.


Solution

  • I noticed that the error occurred in a class named GeneratedNetworkCode, so I searched the source code of Mirror and found that it weaves the assemblies in your project (Assembly-firstpass-CSharp.dll and other assemblies defined by the assembly definition files) to inject this generated code. So actually, you need to configure InternalsVisibleToAttribute for those assemblies in the project. Mirror skips assemblies ending in -Editor, .Editor, and .Test, as well as Mirror's own assemblies.