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

reflection a singleton class?

How to use reflection on a singlton object (i.e. the default constructor is
private)?

I have a configuration file which contains a list of class names. All these
class are simplified singleton type with the similar defintion as below:

public class S
{
private S() {....}

public static S Instance // can be replaced with a method if that makes
things easier
{
get { return _me; }
}
private static S _me = new S();

public void Init() { .....} // do something
};

what I want to do is to invoked the Init() method on all these classes
(whose names are defined in the configuration file).

How can I achieve this? Many thanks in advance for any help and/or thought.
Nov 16 '05 #1
2 3399
why do you need to call Init on a singleton? initialization should be done
inside the singleton in its constructor.

If you need to call a method on the singleton, you need to get its instance
method to return the object. There is no other way. It should be no
different than calling any other static method.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Xing Zhou" <Xi******@discussions.microsoft.com> wrote in message
news:24**********************************@microsof t.com...
How to use reflection on a singlton object (i.e. the default constructor is private)?

I have a configuration file which contains a list of class names. All these class are simplified singleton type with the similar defintion as below:

public class S
{
private S() {....}

public static S Instance // can be replaced with a method if that makes things easier
{
get { return _me; }
}
private static S _me = new S();

public void Init() { .....} // do something
};

what I want to do is to invoked the Init() method on all these classes
(whose names are defined in the configuration file).

How can I achieve this? Many thanks in advance for any help and/or

thought.
Nov 16 '05 #2
what I want to do is to invoked the Init() method on all these classes
(whose names are defined in the configuration file).

How can I achieve this?


Something like this

Type.GetType(fullyQualifiedSingletonTypeName).Invo keMember("Init",
BindingFlags.Static | BindingFlags.Public, null, null, null );

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #3

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

Similar topics

7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
1
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and g++'s output. Any help? // singleton.h ...
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
3
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided...
12
by: Preets | last post by:
Can anyone explain to me the exact use of private constructors in c++ ?
3
by: john | last post by:
Hi to All To demonstrate: public class MyBaseGenericClass<T> { } public class MyGenericClass1<T: MyBaseGenericClass<T> {
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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...

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.