防止静态库被初始化

时间:2015-06-01 09:22:58

标签: c# unity3d static static-libraries

我有一个类需要与其他一些类和库连接。其中一个库需要网络摄像头。问题是这个要求是在静态中完成的(我怀疑是一个静态的构造函数)而且这个库是binairy形式的,所以我无法编辑它。现在,如果没有网络摄像头,我不想使用这个库,但如果它存在,我想使用这个库。 到目前为止我所尝试的是在两者之间建立一个接口(除了更完整之外)

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

请注意,所有这三个类/接口都在他们自己的文件中。 但这不起作用,变量继续加载。

更完整的例子是:

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);
}

}

使用UnityEngine; 使用System.Collections; 使用Meta; 命名空间投影 {

公共类MetaMarker:MonoBehaviour,MetaMarkerInterface {         ///         ///跟踪所需的元对象         ///         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;
    }
}
}

1 个答案:

答案 0 :(得分:1)

您无法阻止加载的DLL的静态构造函数被调用...

但是 - 如果你没有加载/引用DLL直到运行时,你可以避免这种情况。

如果要动态加载DLL - 然后使用动态分派与其进行交互,则可以避免此问题。

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();
        }
    }
}