I a c# class library. Is there anyway that a method can be run on first call and only on the first c开发者_JS百科all to the dll? ie. similar to in a web application - global.asax - Application_Start method.
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
}
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");
}
}
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.)
精彩评论