473,394 Members | 1,718 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,394 software developers and data experts.

How to force execution of static constructor?

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


Nov 16 '05 #1
10 10659
Marc Selis <Ma*******@work.axi> wrote:

<snip>
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.

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

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Marc Selis" <Ma*******@work.axi> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #3
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

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:ui**************@TK2MSFTNGP10.phx.gbl...
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Marc Selis" <Ma*******@work.axi> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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


Nov 16 '05 #4
Marc Selis <Ma*******@work.axi> wrote:
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.

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

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:ui**************@TK2MSFTNGP10.phx.gbl...
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Marc Selis" <Ma*******@work.axi> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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


Nov 16 '05 #6
Marc Selis <Ma*******@work.axi> wrote:
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.

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

Nov 16 '05 #8
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
"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:A1**************@cpmsftngxa10.phx.gbl...
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."

Nov 16 '05 #9
Marc Selis <Ma*******@work.axi> wrote:
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.

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

Thanks All for your help...
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Marc Selis <Ma*******@work.axi> wrote:
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.

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

Nov 16 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: August1 | last post by:
i'm having difficulties with the execution of a program, i believe due to something i am missing in the copy constructor implementation. When i create one object of the class and run the program...
21
by: Steve - DND | last post by:
I know it's possible to require inheriting classes to implement a particular function or property, but is it possible to require a inheriting class to implement a constructor of it's own? ...
7
by: john bailo | last post by:
In anther thread I learned that I could set up a static member to act as a kind of global variable for a web service; one that could be managed for multiple access using a mutex. Now I am...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
12
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
10
by: Marc Gravell | last post by:
Given a generic method "of T", is there a good way of ensuring that any static ctor on T has executed? Following code demonstrates (TestClass1) that via generics you can use the Type instance long...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
14
by: andreyvul | last post by:
g++ says a reinterpret cast from void* is disallowed; however, all I'm trying to do is de-capsulate a void* argument that holds the pointer to a class (and static_cast forces cast constructor) any...
5
by: Dave | last post by:
Hello, Suppose you have a class with a static property with only a get (read only). You also have code in a static constructor that sets these properties but takes 1 hour to run. Now suppose...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.