all groups > dotnet clr > may 2006 >
You're in the

dotnet clr

group:

Forcing type initialization


Forcing type initialization Ole Nielsby
5/16/2006 8:55:24 PM
dotnet clr: I want to enforce certain type initializers in an assembly to run at
startup.

foreach (Type t in Assembly.GetExecutingAssembly().GetTypes())
{
if (...some criteria...)
{
t.TypeInitializer.Invoke(type,new object[0]);
}
}

This doesn't work.

What's the simplest way to initialize a type, given its Type object?

Re: Forcing type initialization Mattias Sjögren
5/16/2006 11:48:46 PM
[quoted text, click to view]

Are you looking for
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor() ?


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Re: Forcing type initialization Ole Nielsby
5/17/2006 5:11:43 PM

[quoted text, click to view]

Yes - though I'm not quite happy with what I see. Seems a bit
weird or low-level to use a handle. The docs state nothing about
what will happen if the type has already been run, so I think I'll
just stick with using reflection to access a static field of the classes
instead - which will do the job in my case.

But thanks for the hint anyway - I didn't figure CompilerServices would
be the place to look.

Ole N.

AddThis Social Bookmark Button