Roland Zerek <nobody@nowhere.com> wrote:[color=blue][color=green]
> > How does MyClass.sm_Instance guarantee that the prototype registers
> > itself? I don't see how it achieves that at all.[/color]
>
> OK - the idea is that the static member is initialized at the beginning. So
> if the static member is being initialized its constructor is being invoked
> and the constructor registers itself in the manager for later use...
>
> However, I am advanced C++ user and rather novice C# so I assumed that the
> static member is being initialized at the very beginning of an application.
> But it seems it does not. Instead of it it is being initialized at the first
> refferrencing try. In fact my idea was not to explicitly do it.[/color]
Type initializers are executed either "some time before any static
field is first referenced" or "at the first access to any static member
or constructor" depending on the beforefieldinit flag.
See
http://www.pobox.com/~skeet/csharp/beforefieldinit.html for more
information.
[color=blue]
> But I still would like to have code that initializes itself at the
> beginning. Do any C# solutions exist?[/color]
Well, you could write some code to examine all the types in all the
loaded assemblies, looking for (say) a specific attribute which you
define to mean "call my type initializer immediately". You could then
hook into AppDomain.AssemblyLoad to apply that procedure to any freshly
loaded assembly too.
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too