473,796 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Re questNew+=new RequestNewDeleg ate(OnRequestNe w);

}

private static void OnRequestNew(st ring _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 10698
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.TypeInitia lizer, and then
invoke that.

--
Jon Skeet - <sk***@pobox.co m>
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******** ********@TK2MSF TNGP10.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.Re questNew+=new RequestNewDeleg ate(OnRequestNe w);

}

private static void OnRequestNew(st ring _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.mach in AT dot.state.fl.us > wrote
in message news:ui******** ******@TK2MSFTN GP10.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******** ********@TK2MSF TNGP10.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.Re questNew+=new RequestNewDeleg ate(OnRequestNe w);

}

private static void OnRequestNew(st ring _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.GetTyp es and
typeof(TheInter face).IsAssigna bleFrom(testTyp e) for each type it
returns.

--
Jon Skeet - <sk***@pobox.co m>
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.mach in AT dot.state.fl.us > wrote
in message news:ui******** ******@TK2MSFTN GP10.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******** ********@TK2MSF TNGP10.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.Re questNew+=new RequestNewDeleg ate(OnRequestNe w);

}

private static void OnRequestNew(st ring _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.GetTyp es and
typeof(TheInter face).IsAssigna bleFrom(testTyp e) for each type it
returns.

--
Jon Skeet - <sk***@pobox.co m>
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.mic rosoft.com> wrote in message
news:A1******** ******@cpmsftng xa10.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10

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

Similar topics

4
1835
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 with that object alone, there are no problems. however, when subsequently using the copy constructor for a second object and/or a third object created on the heap memory - i run into an execution problem and assert failure dialog box (ignore,...
21
10209
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? Thanks, Steve
7
1673
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 thinking of other uses. For example, say I want to provide data validation on a field based on a table from a database. So, I would want to load that list once from a table, and then have all
11
3846
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 experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
12
3442
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 static constructor of the base class would be invoked even if a an object of the derived class is created. Is this correct ? Is there any way to ensure the base class's static constructor is invoked before the derived class instance is constructed ?...
10
2245
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 before the static ctor fires. With a generic class and the "new()" clause, I can force this in the static ctor of the generic class, but this is not always convenient, plus may (possibly?) be seen as a no-op . Sample; ideally I'd get "Static...
18
1933
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 requests at the same time. The first one stops execution as the second continues. If I place either an alert between the two requests or run the second through a setTimeout of only 1 millisecond, they both work. You can see a working example here:...
14
4440
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 solutions? also this pops up: 43: error: expected unqualified-id before 'return' code: includes: stdio.h, stdlib.h, time.h, pthread.h struct: template <typename Tstruct mergesort_thread { T* start;
5
4461
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 the first request comes in at 11:00 am and tries to read from this property. It will need to wait an hour until the page loads which is fine and makes sense. If a second (different) request at 11:01 am comes in and tries to read the same...
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10168
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.