简体   繁体   English

在第一次调用dll时执行代码

[英]execute code on first call to the dll

I ac# class library. 我ac#类库。 Is there anyway that a method can be run on first call and only on the first call to the dll? 无论如何,有一种方法可以在第一次调用并且仅在第一次调用dll时运行? ie. 即。 similar to in a web application - global.asax - Application_Start method. 类似于Web应用程序中的-global.asax-Application_Start方法。

Have a static initializer on your interface object. 在接口对象上有一个静态初始化器。 However, you should only expose this Interface class through your DLL that way, and make all other functional calls internal. 但是,您只应通过这种方式通过DLL公开此Interface类,并使所有其他功能调用成为内部函数。

public class DllInterface
{
    static DllInterface()
    {
        // Do initialization magic here
    }

    // Do other stuff
}

I searched a while for it and found this site . 我搜索了一会儿,发现了这个网站 It looks very promising, but i hadn't the time to test it by myself. 它看起来非常有前途,但是我没有时间自己进行测试。

(Currently the site is down for maintenance. Here is the site from google cache .) (当前该网站正在维护中。这是Google缓存中的网站。)

Interesting question. 有趣的问题。 I don't know if in c# you can do that, but I don't think so. 我不知道在c#中是否可以做到这一点,但我不这么认为。 What you can do is to create a static constructor that it will be called before any other call in a class: 您可以做的是创建一个静态构造函数,该构造函数将在类中的任何其他调用之前被调用:

public class Foo
{
   static Foo()
   { 
      Console.WriteLine("called first time only");
   }

   public Foo()
   {
      Console.WriteLine("called every new object"); 
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM