- Home /
Multiple DLL with the same class reference
Hello, I am using third party libraries in my projects, one is
Diagflow for Unity and the second is Ros for Unity
My issue comes with the fastJSON.dll and NewtonSoft.Json.dll they both are sharing classes namespaces and i get the error
"The class X exist in both LIBRARY1 and LIBRARY2"
if i remove one of the 2 libraries errors dissapear, BUT there are third libraries that depend to both libraries, so if i remove one i can run the project but i get the error
"THIRDASEMBLY can not be load because it cant locate (THE LIBRARY I HAVE JUST DELETED)"
I cant modify those libraries since they are thirdparty, I have tried adding an extern alias following this example without success
Answer by Bunny83 · Jun 17, 2020 at 02:23 PM
I haven't really used any of the libraries you've mentioned. However I doubt that there are actual type name collisions between those libraries. As far as I can tell the NetwonSoft library has all its classes inside its own namespace (Newtonsoft). So you could only get name collisions when you put two using statements of different namespaces inside the same file, or if the other library abusively uses the Newtonsoft namespace for its own class(es).
This is of course just an example. I haven't actually used the libraries. You haven't mentioned the actual classes and namespaces involved here. Are you sure you did not just mindlessly put up using statements? One of the most common example that doesn't involve any third party libraries is the Random class. Unity has it's own Random class inside the namespace UnityEngine. However there's also a Random class inside the System namespace. They can happily co-exist next to each other. However you can not use both using statements at the same time. So using UnityEngine;
and using System;
is not possible at the same time when you want to refer to the Random class. In that case you have to use the full qualified classname, including the namespace.
If the two libraries in fact define a class with the same name inside the same namespace, you could simply blame the creators of those libraries. You said you tried the library alias solution. Did you actually create the [compiler configuration file "smcs.rsp"][1] and specify alias names for your two libraries?
As I mentioned since your question is lacking the actual details of your error / issue I will not go further into this. You can not expect us to download and import the libraries outselfs just to see what's the actual issue.
ps: I often get the impression that some people think a using statement is somehow required to use a certain library. This is completely false. Using statements at the top of a file just make a namespace available for this file for convenience so you don't have to type out the namespace all the time. A namespace also does not necessarily be related to one particular library, though it commonly is which might be the reason for that assumption. Namespaces are just a way of organising and seperating classes within the app domain specifically to avoid name collisions between third party libraries. [1]: https://github.com/DashW/UnityExternAlias/blob/master/Assets/smcs.rsp
hello, sorry i should have added more resources to clarify my question, first i am adding an image showing the exact error i get
If its not legible (i chose that font size so i could add full script to the image) the exact error is:
The Class that is giving issues is for example: "Newton.Json.JsonIgnoreAtribute" and others "Newton.Json.X" classes
Thanks for pointing out the multiple using issue, but i indeed made sure that i wasnt adding multiple using statement, but to make sure here is a link to the apiaisdk github library that is using the namespace Newtonsoft.Json (from fastJSON) and here is a link to the rosharp library using that same namespace but from the Netwonsoft.Json dll.
into the extern alias issue i have added the alias to make sure i was using the correct Newtonsoft.Json class here is the resulted code, without showing any errors,
extern alias test2;
using test2::Newtonsoft.Json;
namespace RosSharp.RosBridgeClient.$$anonymous$$essageTypes.Sensor
{
public class SetCameraInfoResponse : $$anonymous$$essage
{
[JsonIgnore]
public const string Ros$$anonymous$$essageName = "sensor_msgs/SetCameraInfo";
public bool success;
// True if the call succeeded
public string status_message;
// Used to give details about success
public SetCameraInfoResponse()
{
this.success = false;
this.status_message = "";
}
public SetCameraInfoResponse(bool success, string status_message)
{
this.success = success;
this.status_message = status_message;
}
}
}
but when unityeditor compiles the code it returns the next error:
(IT IS ATTACHED TO NEXT CO$$anonymous$$$$anonymous$$ENT IT DOESNT LET $$anonymous$$E UPLOAD $$anonymous$$ORE THAN 2 FILES) so i searched and found about the smcs.rsp, i tried several times, without success, and when googling the error i found this
https://issuetracker.unity3d.com/issues/unity-compiler-ignores-reference-alias-in-rsp-files
so i imagined that was simply not supported in newer versions (i am using 2019) and my next approach was to come to unity forum in case someone could give me an advice
Sorry for the lack of details i was in a hurry i should not have upload the question like this.
@Bunny83 thanks for your time any advice, even if its to simply stop trying to find a solution and write my own library is appreciated.
$$anonymous$$ario.
Well, I have never really used assembly alias names in Unity since I wasn't in need of them. It seems that Unity's compiler does not support assembly alias names. So your only other option would be to fix the issue at the source. To me it seems very strange that your fastJSON library actually contains classes from a completely different library. This seems to be a custom build of the fastJSON library. At least the version 2.2.0 of the fastJSON code project does not contain those namespaces (checked with ILSpy).
So you could try replacing the fastJSON.dll with a newer "official" one and see if it's compatible. If it's not you could probably decompile your fastJSON dll. remove all the classes and type definitions in the Newtonsoft namespace and replace the few uses with the actual classes from the proper Json.Net library. That means ins$$anonymous$$d of shipping your own version of the attributes used by fastJSON, it would actually reference the Newtonsoft library to get access to those attributes. Though depending on if those attributes are actually used in your code, you might be able to just replace / rename them inside the fastJSON library source.
Your answer
Follow this Question
Related Questions
merged DLL not working 0 Answers
Windows Phone 8.1 Assembly 0 Answers
Unity losing references to dll in the project 0 Answers
unity3d import c++ dll for pass by reference method 0 Answers
Why does Unity does not see my DLLs? 1 Answer