When using assembly to encapsulate code, there may be a special overload method compilation error!

Using asmdef to define different assemblies and accessing overload methods in one assembly A to another assembly B, all parameters and return values of the target access method can be accessed normally by assembly A. However, as long as there are overload methods in this overload method that contain object types that assembly A cannot access and have the same number of necessary parameters, compilation errors will occur.

For example, the following example:

namespace AsmdefOne
{
    public struct TestStruct
    {
        public TestStruct(string key) => Key = key;
        public string Key;
    }
}

namespace AsmdefTwo
{
    public static class TestMethod
    {
        public static void Foo0(string str) { }
        public static void Foo0(AsmdefOne.TestStruct testStruct) { }
        
        public static void Foo1(string str) { }
        public static void Foo1(AsmdefOne.TestStruct testStruct, int index) { }

        public static void Foo2(string str) { }
        public static void Foo2(AsmdefOne.TestStruct testStruct, int index = default) { }
        
        public static void Foo3(string str) { }
        public static void Foo3(int index, AsmdefOne.TestStruct testStruct) { }

        public static void Foo4(string str) { }
        public static void Foo4(int index, AsmdefOne.TestStruct testStruct = default) { }
    }
}

namespace AsmdefThree
{
    public class Tester : UnityEngine.MonoBehaviour
    {
        private void Awake()
        {
            AsmdefTwo.TestMethod.Foo0("123"); // error
            AsmdefTwo.TestMethod.Foo1("123"); // ok
            AsmdefTwo.TestMethod.Foo2("123"); // error
            AsmdefTwo.TestMethod.Foo3("123"); // ok
            AsmdefTwo.TestMethod.Foo4("123"); // error
        }
    }
}

I have defined three assemblies with reference relationships:
AsmdefThree ->AsmdefTwo ->AsmdefOne (AsmdefThree does not reference AsmdefOne)

Three objects were defined in each of these three assemblies sets
AsmdefOne:TestStruct
AsmdefTwo:TestMethod
AsmdefThree:Tester


Accessing the TestMethod method in Tester that contains the TestStruct parameter yields the following results.

None of Foo0/Foo2/Foo4 can be compiled

Yep, that is a limitation of the compiler. You’d just have to move TestStruct to an assembly which AsmdefThree knows about, or give the offending methods different names, to work around it.

1 Like