applejag/Newtonsoft.Json-for-Unity

Ensure constructor that is an internal class inside another package?

jelmer3000 opened this issue · 2 comments

I'm getting the well known error:
There was a problem: Unable to find a constructor to use for type Q42.HueApi.NuPnPResponse. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path '[0].id', line 1, position 7.

Q42.HueApi.NuPnPResponse is an internal class inside a DLL from a package called Q42.HueApi. The source can be found here.

What I have so far is:

  • I imported Newtonsoft.Json-for-Unity
  • I added the link.xml from this project
  • I added another link.xml to try to include that internal class Q42.HueApi.NuPnPResponse. But I am unsure if this is the right way to do it.... The error still shows up at runtime. This link.xml looks like this:
<linker>
    <assembly fullname="AssemblyCSharp">
        <type fullname="Q42.HueApi.NuPnPResponse">
            <method signature="System.Void .ctor()"/>
        </type>
    </assembly>
</linker>
  • Can/should I add that internal class like this, can that work at all?
  • What would be the right assembly name? I am not using assemblies myself, but I don't know if a DLL counts as a separate assembly?

My goal is to create a HoloLens2 (ARM64) app to control my lights with cool gestures :)
It works in Unity editor!

Hello @jelmer3000! You're so close! That link.xml file looks spot on, except the assembly part.

All code must live inside an assembly in .NET. And every assembly has it's own .dll file.

The assembly is often (but not always) the file name of the .dll, but without the .dll extension. So with that you can assume it's Q42.HueApi, as the file name is Q42.HueApi.dll.

If you want to make sure, you can use the .NET utility static method System.Reflection.AssemblyName.GetAssemblyName on the file. For example, I've downloaded their NuGet package and unzipped it, and then I can run the method via PowerShell:

PS ~\Downloads\q42.hueapi.3.18.1\lib\netstandard2.0> [System.Reflection.AssemblyName]::GetAssemblyName("$pwd\Q42.HueApi.dll")

Version        Name
-------        ----
3.18.1.0       Q42.HueApi

I would recommend just retaining the entire type though. As the type is quite small with no methods and only 3 properties, it's less error-prone to just retain it all, which can be done by omitting the <type> element's body, resulting in:

<linker>
    <assembly fullname="Q42.HueApi">
        <type fullname="Q42.HueApi.NuPnPResponse" />
    </assembly>
</linker>

Thanks so much! Although my app is still crashing, but it does get past this crash point at least, judging from the Output window. So I made some progress. This has taken many hours so far (before I eventually got to this repo)
Really appreciate your reply, I don't know much about linkers and assemblies 🙉