467,077 Members | 1,068 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

Making runtime call static c'tor without creating an instance

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.
Nov 17 '05 #1
  • viewed: 1589
Share:
6 Replies
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
Nov 17 '05 #2
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.Run ClassConstructor(typeof(SomeClass).TypeHandle)
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #3
Mattias Sjögren <ma********************@mvps.org> wrote:
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.Run ClassConstructor(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...)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
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.
"Mattias Sjögren" <ma********************@mvps.org> wrote in message news:ub*************@TK2MSFTNGP12.phx.gbl...

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.Run ClassConstructor(typeof(SomeClass).TypeHandle)


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #5
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

Tony Maresca wrote:
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.

Nov 17 '05 #6
Bennie Haelen <Be***********@jda.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

90 posts views Thread by Ben Finney | last post: by
4 posts views Thread by Tony Johansson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.