How to forcibly call a static constructor?

J

Jeff Brown

I understand that a static constructor is not called until and unless the
class is instantiated, or any of its static members are referenced.

But suppose (for the sake of argument) a class is neither instantiatable,
nor has any static members. For example:

static public class MyClass
{
static MyClass()
{
Console.WriteLine("Hooray, you did it!");
}
}

Is there any way on heaven or earth to get that static constructor to
execute? Through reflection or whatever?

Thanks for any help.

Jeff Brown
 
M

Marc Gravell

OK; if the class is neither instantiable nor has static members, what
the heck is it? i.e. what is the point? The only use I can think of is
to kick-start a thread (or such) in the static ctor, but even then
there's better approaches. Anyway, you could always just add:
static void ForceLoad() {}

or something... does nothing except force the static class to
initialise.

Marc
 
N

Nicholas Paldino [.NET/C# MVP]

Jeff,

The first time the type information is accessed, the static constructor
is going to be called.

However, in your situation, if you do not expose anything on the type to
be called or accessed then the static constructor will never fire. In this
case, you will have to add a method to be called explicitly, which you
should do your work in.

The static constructor has a very clear definition on when it is called.
It seems that you have different timing requirements. As a result, you
shouldn't be using a static constructor, but another method (figuratively,
and literally, in this case).

Hope this helps.
 
A

Alan Pretre

Jeff Brown said:
Is there any way on heaven or earth to get that static constructor to
execute? Through reflection or whatever?

You could have an empty static Initialize() stub in the static class, then
call it at app startup.

internal static void Initialize() {
}

-- Alan
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
The first time the type information is accessed, the static constructor
is going to be called.

What do you mean by "the type information"? Accessing the type with
typeof and/or reflection doesn't trigger the static constructor.

You can specifically call the static constructor with reflection, but
then the guarantees about it only being called once don't hold. Here's
a sample:

using System;
using System.Reflection;

class InitMe
{
static InitMe()
{
Console.WriteLine ("Initialized!");
}
}

class Test
{
static void Main()
{
Type t = typeof(InitMe);
foreach (MemberInfo mi in t.GetMembers())
{
Console.WriteLine (mi);
}
ConstructorInfo info = t.TypeInitializer;

Console.WriteLine ("Running now!");
info.Invoke(null, null);
}
}
 
J

Jeff Brown

Actually, in your code, the statement

info.Invoke(null, null);

causes the static constructor to be called TWICE... the first time,
implicitly, because the class is initialized, and the second time because
the static constructor is explicitly called!

Jeff
 
J

Jon Skeet [C# MVP]

Jeff Brown said:
Actually, in your code, the statement

info.Invoke(null, null);

causes the static constructor to be called TWICE... the first time,
implicitly, because the class is initialized, and the second time because
the static constructor is explicitly called!

Yup - hence the bit about the guarantees not holding any more. You'd
need to code defensively on that front.
 
N

Nicholas Paldino [.NET/C# MVP]

When I say "type information", I meant the first time you create an
instance of the type, or access a static member on the type. I should have
been more specific.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top