c# - prevent static library from being initialized -
i have class need connect few other classes , libraries. 1 of these libs requires webcam. problem requirement done in static (i suspect constructor of static) , library in binairy form cannot edit it. if no webcam pressent don't want use library if present want use library. have tried far setting interface between 2 (like except 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 3 of these classes/interface in own files. did not work , variables kept on loading.
a more full example this:
namespace projection { using system.diagnostics.codeanalysis; using unityengine; /// <summary> /// marks marker used basis of level meta 1 /// </summary> public class baseforlevel : monobehaviour { private metamarkerinterface metamarker; public bool activemarkers; //we have 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 tracking /// private gameobject markerdetectorgo;
/// <summary> /// meta object required tracking /// </summary> private markertargetindicator markettargetindicator; /// <summary> /// sets detector , marker indicator find marker/ /// </summary> public void registermeta(){ this.markerdetectorgo = markerdetector.instance.gameobject; // hide markerindicator this.markettargetindicator = this.markerdetectorgo.getcomponent<markertargetindicator>(); this.markettargetindicator.enabled = false; } ///<summary> ///orignally metaexample script, heavily edited , returns true ///if marker seen if moves object marker position ///</summary> public bool movetransformtomarker(int id,transform trans){ if (!this.markerdetectorgo.activeself) { this.markerdetectorgo.setactive(true); } if (markerdetector.instance != null) { // check if can see marker if (markerdetector.instance.updatedmarkertransforms.contains(id)) { // if can move marker position markerdetector.instance.getmarkertransform(id, ref trans); return true; } } return false; } } }
you cannot stop loaded dll's static constructor being called...
however - if don't load/reference dll until runtime, can avoid that.
if want dynamically load dll - , interface using dynamic dispatch, can avoid 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 method on loaded type dynamically c.output(@"hello"); } console.readline(); } } }
Comments
Post a Comment