Using dll's from C#?

Hello

I am trying to use a dll I made in C# .net 2.0 without any luck. From what I have read, the dll should be placed in the asset folder and can then be used as any other scripts.

My Dll:

using UnityEngine;
using System;
using System.Collections.Generic;
using System.Text;

public class SerialGPS_DLL2 : MonoBehaviour
{
    public void SerialGPSInit(string comPort, int baudRate)
    {
        //Some code
    }

    public void SerialGPSClose()
    {
        //Some code
    }

    public void SerialGPSUpdate(ref double latitude, ref double longitude, ref string status)
    {
        //Some Code
    }
}

How I use it:
Adding dll as referance in Visual Studio(just to get intellisense).

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class SerialGPS : MonoBehaviour 
{
    private SerialGPS_DLL2 m_cSerialGPS_DLL;
    
    void Start () 
    {
        m_cSerialGPS_DLL.SerialGPSInit("COM3", 4800);
    }

    void Update()
    {

    }
}

The error i get:

Internal compiler error. See the console log for more information. output was:
Unhandled Exception: Mono.CSharp.InternalErrorException: Assets/SerialGPS.cs(5,14): SerialGPS ---> Mono.CSharp.InternalErrorException: Assets/SerialGPS.cs(12,28): SerialGPS.m_cSerialGPS_DLL ---> System.TypeLoadException: Could not load type 'SerialGPS_DLL2' from assembly 'SerialGPS_DLL2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
  at (wrapper managed-to-native) System.Reflection.Assembly:InternalGetType (System.Reflection.Module,string,bool,bool)
  at System.Reflection.Assembly.GetType (System.String name, Boolean throwOnError, Boolean ignoreCase) [0x00000] in <filename unknown>:0 
  at System.Reflection.Assembly.GetType (System.String name) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.SimpleName.Error_TypeOrNamespaceNotFound (IMemberContext ec) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.SimpleName.ResolveAsTypeStep (IMemberContext ec, Boolean silent) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Expression.ResolveAsBaseTerminal (IMemberContext ec, Boolean silent) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Expression.ResolveAsTypeTerminal (IMemberContext ec, Boolean silent) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.MemberBase.ResolveMemberType () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.MemberBase.Define () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Field.Define () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.TypeContainer+MemberCoreArrayList.DefineContainerMembers () [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at Mono.CSharp.TypeContainer+MemberCoreArrayList.DefineContainerMembers () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.TypeContainer.DefineContainerMembers (Mono.CSharp.MemberCoreArrayList mcal) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Class.DefineContainerMembers (Mono.CSharp.MemberCoreArrayList list) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.TypeContainer.DoDefineMembers () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Class.DoDefineMembers () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.TypeContainer.Define () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.ClassOrStruct.Define () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Class.Define () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.RootContext.PopulateTypes () [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at Mono.CSharp.RootContext.PopulateTypes () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0 

My only theory at the moment is that I am building my dll in a way that is not supported.

You can drop .Net DLLs in your project, compiling order has no effect here as all Unity scripts are also compiled into .Net DLLs before the scene is run.

However, I think your issue comes from creating an instance of MonoBehaviour that way. Remove the MonoBehaviour subclass part from the SerialGPS_DLL2 class definition and try it.

I striped the dll for all functionality and found out it didn’t like the System.IO.Ports namespace I was using.

After testing System.IO.Ports in a new class in Unity i got the error: “the type or namespace ‘port’ does not exist in namespace ‘System.IO’”.

So my problem does not seem to be dll related after all. Found a post about System.IO.Ports here.

in the player settings change the .NET 2.0 subset to .NET2.0 it will work…

Yeah… lame… really… really… LAME error message. Just cost me several hours while I’m on a deadline. My .Net common libraries reference SYstem.IO.Ports… I need them to debug a serial peripheral device.