How to force execution of static constructor?

M

Marc Selis

Hi,

I have a class with a static constructor in which the class registers itself
as capable of doing something (using a delegate)
The problem is that the static constructor is never called, as no other
class references it. The only way to communicate with the class is using
the delegate.

Here is my code:


internal class Factory

{

static Factory()

{

Configurator.RequestNew+=new RequestNewDelegate(OnRequestNew);

}



private static void OnRequestNew(string _objectName, ref ConfigObject
_object)

{

//Handle request



}


}





Is there any way to force the static constructor to be executed, using
attributes or something?



Regards,
Marc Selis
 
J

Jon Skeet [C# MVP]

Is there any way to force the static constructor to be executed, using
attributes or something?

You can get the type initializer using Type.TypeInitializer, and then
invoke that.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Marc,


Not a very elegant solution but you can add a dump method, just to force
the constructor to be called:

static void DumpMethod(){}


Cheers,
 
M

Marc Selis

Hi Ignacio & Jon,

Thanks for you answers, but both your solutions require some other class
knowing the existence of my Factory class.
The whole point of my set up was to avoid this.

What I'm trying to do is the following:

I have a class (let's call it Builder) that needs to create a new instance
of a type that implements a certain -wellknow- interface, but that Builder
only has some kind of generic -but unique- name (no typename) describing the
type to create. The Builder isn't even sure that the requested type even
exists. That is why I choose to fire an event, and hope that some Factory
that is able to create it would respond to it.

Our plan is to provide extra Factories in the future, but the problem is
registering them to the Builder's event. The only alternative I have now is
to use reflection, and to look in every DLL for Factories, but I would
rather use another approach.

I thought there might be a way to instruct the mechanism that loads the
assemblies or types to automatically call a static constructor (or some
other code) using attributes or something...

Regards,
Marc
 
J

Jon Skeet [C# MVP]

Marc Selis said:
Thanks for you answers, but both your solutions require some other class
knowing the existence of my Factory class.
The whole point of my set up was to avoid this.

What I'm trying to do is the following:

I have a class (let's call it Builder) that needs to create a new instance
of a type that implements a certain -wellknow- interface, but that Builder
only has some kind of generic -but unique- name (no typename) describing the
type to create. The Builder isn't even sure that the requested type even
exists. That is why I choose to fire an event, and hope that some Factory
that is able to create it would respond to it.

That's fine - you can probe for all the type which implement an
interface, and then fire the type initializers for all of those types.

Use Assembly.GetTypes and
typeof(TheInterface).IsAssignableFrom(testType) for each type it
returns.
 
K

Kevin Yu [MSFT]

Hi Marc,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
M

Marc Selis

Hi Kevin,

My problem is solved, yes. I choose to let the factories implement an
interface, and register the factories to the Builder class when the
application is launched.

My question remains unanswered though:
Is it possible to automatically execute some code (in a static method) when
the assembly that contains the code is loaded by the CLR? By using some
attribute on the static method for example?

Thanks,
Marc
 
J

Jon Skeet [C# MVP]

Marc Selis said:
My question remains unanswered though:
Is it possible to automatically execute some code (in a static method) when
the assembly that contains the code is loaded by the CLR? By using some
attribute on the static method for example?

No, that's not possible as far as I know.

You can, however, use the AssemblyLoad event to execute a piece of code
(in an already loaded assembly) whenever a new assembly is loaded.
 
M

Marc Selis

OK.

Thanks All for your help...


Jon Skeet said:
No, that's not possible as far as I know.

You can, however, use the AssemblyLoad event to execute a piece of code
(in an already loaded assembly) whenever a new assembly is loaded.
 

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