2

I have a class that I need to connect with a few other classes and libraries. One of these libs requires a webcam. Problem is that this requirement is done in a static (I suspect a constructor of a static) and this library is in binairy form so I cannot edit it. Now if no webcam is pressent I don't want to use this library but if it is present I want to use this library. What I have tried so far is by setting up an interface between the two (like this except a lot more complete)

interface caminterface {
     void dostuff();//executes lib function
}
class cam{
     void dostuff();
}
class main{
    private caminterface;
    void start(){
    if(haswebcam())
        caminterface=new cam();
     }
}

note that all 3 of these classes/interface were in their own files. But this did not work and the variables kept on loading.

A more full example is this:

namespace Projection
{
    using System.Diagnostics.CodeAnalysis;
    using UnityEngine;
    /// <summary>
    /// Marks which marker is used for the basis of the level by the meta one
    /// </summary>
    public class BaseForLevel : MonoBehaviour
    {
        private MetaMarkerInterface metaMarker;
        public bool activeMarkers; //we have a cam 
        public void Start()
        {
            if(this.activeMarkers){
                this.metaMarker= new MetaMarker();
                this.metaMarker.RegisterMeta();
            }
        }    
    }

using UnityEngine;
using System.Collections;
namespace Projection{
public interface MetaMarkerInterface  {
     void RegisterMeta();
     bool MoveTransformToMarker(int loc ,  Transform trans);
}

}

using UnityEngine; using System.Collections; using Meta; namespace Projection {

public class MetaMarker : MonoBehaviour , MetaMarkerInterface{ /// /// meta object required for tracking /// private GameObject markerdetectorGO;

    /// <summary>
    /// meta object required for tracking 
    /// </summary>
    private MarkerTargetIndicator marketTargetindicator;


    /// <summary>
    /// sets up the detector and marker indicator to find this marker/ 
    /// </summary>
    public void RegisterMeta(){
        this.markerdetectorGO = MarkerDetector.Instance.gameObject;

        // hide markerindicator
        this.marketTargetindicator = this.markerdetectorGO.GetComponent<MarkerTargetIndicator>();
        this.marketTargetindicator.enabled = false;

    }
    ///<summary>
    ///orignally from the metaExample script, heavily edited and returns true 
    ///if the marker was seen if it is then it also moves the object to the marker position 
    ///</summary>
    public bool MoveTransformToMarker(int id,Transform trans){
        if (!this.markerdetectorGO.activeSelf)
        {
            this.markerdetectorGO.SetActive(true);
        }
        if (MarkerDetector.Instance != null)
        {
            // check if we can see this marker 
            if (MarkerDetector.Instance.updatedMarkerTransforms.Contains(id))
            {
                // if we can then move this marker to that position 
                MarkerDetector.Instance.GetMarkerTransform(id, ref trans);
                return true;
            }
        }
        return false;
    }
}
}
Thijser
  • 2,625
  • 1
  • 36
  • 71
  • Can you create a full example that demonstrates the problem? If you don't reference a type, it shouldn't run static constructors. Or is this perhaps not a .NET assembly at all? When you say "static library", what specifically do you mean? – Lasse V. Karlsen Jun 01 '15 at 09:39
  • I have added a more complete example. – Thijser Jun 01 '15 at 09:44

1 Answers1

1

You cannot stop a loaded DLL's static constructor from being called...

However - if you don't load/reference the DLL until runtime, you can avoid that.

If you want to dynamically load the DLL - and then interface with it using dynamic dispatch, you can avoid the issue.

namespace ConsoleApplication1
{
    using System;
    using System.Reflection;

    class Program
    {
        static void Main(string[] args)
        {
            var DLL = Assembly.LoadFile(@"C:\visual studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\DLL.That.Keeps.Crashing.dll");

            foreach(Type type in DLL.GetExportedTypes())
            {
                dynamic c = Activator.CreateInstance(type);
                //Call a method on the loaded type dynamically
                c.Output(@"Hello");
            }

            Console.ReadLine();
        }
    }
}
Dave Bish
  • 19,263
  • 7
  • 46
  • 63