views:

45

answers:

3

I a c# class library. Is there anyway that a method can be run on first call and only on the first call to the dll? ie. similar to in a web application - global.asax - Application_Start method.

+1  A: 

Interesting question. I don't know if in c# you can do that, but I don't think so. 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"); 
   }
}
jmservera
+2  A: 

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.

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

    // Do other stuff
}
Jan Jongboom
+2  A: 

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.)

Oliver