Making runtime call static c'tor without creating an instance

T

Tony Maresca

Given:

public class MyClass
{
private MyClass() {}

static MyClass()
{
//..
}
}

How can I make the runtime call the static c'tor
without creating an instance of the class?

TIA.

--
Tony M.
 
R

Randy A. Ynchausti

Tony.
public class MyClass
{
private MyClass() {}

static MyClass()
{
//..
}
}

How can I make the runtime call the static c'tor
without creating an instance of the class?

You can provide MyClass with some additional static member and then access
that static member. This will cause the static constructor to be invoked.

Regards,

Randy
 
M

Mattias Sjögren

I start with only an instance of the class Type
e.g., typeof(SomeClass), and from that, I would like to
make the runtime call its static constructor, without
creating an instance of it.

System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(SomeClass).TypeHandle)


Mattias
 
J

Jon Skeet [C# MVP]

Mattias Sjögren said:
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(ty
peof(SomeClass).TypeHandle)

Or you can invoke Type.TypeInitializer.

Note that these should be done with great care, as they (at least the
latter) will *rerun* the type initializer as many times as you call
them. Some types may not like that! (For instance, for the normal
singleton pattern implementation, you could end up with multiple
instances...)
 
T

Tony Maresca

Thanks to you and Jon.

That seems to work.

I'm also storing a static arraylist of classes
that have had their initializers called, so I
don't mistakenly do it more than once.

--
Tony M.
 
B

Bennie Haelen

The static constructor of a Type is invoked whenever the first instance
of the Type is loaded in your AppDomain. As far as I know, it is not
possible to explicitly invoke a static constructor by using Reflection.
If you tried, you would probably get a "Type Initializer was not
callable" exception.


HTH,

Bennie Haelen
 
J

Jon Skeet [C# MVP]

Bennie Haelen said:
The static constructor of a Type is invoked whenever the first instance
of the Type is loaded in your AppDomain.

Or when you first use a static member, such as a method or property.
As far as I know, it is not
possible to explicitly invoke a static constructor by using Reflection.
If you tried, you would probably get a "Type Initializer was not
callable" exception.

On the contrary, you can even call it multiple times:

using System;
using System.Reflection;

class InvokeMe
{
static InvokeMe()
{
Console.WriteLine ("Hi there!");
}
}

class Test
{
static void Main()
{
ConstructorInfo ctor = typeof(InvokeMe).TypeInitializer;
ctor.Invoke(null, null);
ctor.Invoke(null, null);
}
}

Note that if you just call Invoke(null), you do indeed get an
exception.
 

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