防止静态库被初始化

本文关键字:初始化 静态 | 更新日期: 2023-09-27 17:52:36

我有一个类,我需要与其他几个类和库连接。其中一个库需要网络摄像头。问题是这个要求是在静态(我怀疑是静态的构造函数)中完成的,这个库是二进制形式的,所以我不能编辑它。现在,如果没有网络摄像头,我不想使用这个库,但如果有,我想使用这个库。到目前为止,我所尝试的是通过在两者之间设置一个接口(像这样,除了更完整)

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

注意所有这3个类/接口都在它们自己的文件中。但这行不通,变量一直在加载。

一个更完整的例子是:

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;使用元;名称空间投影{

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

防止静态库被初始化

您无法阻止已加载的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();
        }
    }
}