473,322 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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
6 1783
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
54
by: Zytan | last post by:
I have a log class that makes a synchronized TextWriter like so, in the constructor: StreamWriter sw = new StreamWriter(filename); tw = TextWriter.Synchronized(sw); In the destructor,...
6
by: Grey Alien | last post by:
class A { public: A(const B& ref); private: static B& b ; }; How may b be initialized ?
4
by: Tony Johansson | last post by:
Hello! I have a class where all the members are static so I change the class to be static but I have run into one small problem and that is how do I code to prevent calling the C-tor because...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.