By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,990 Members | 1,406 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,990 IT Pros & Developers. It's quick & easy.

Create an instacne by class name

P: n/a
Hello everybody,
I have a method "CreateInstance" defined in the following code snippet:

//To create an instance by a class's full name,but shoult check if it
can be multi-instantiated at first
public T CreateInstance<T>(string classFullName, bool
canMultiInstantiated)
{
T instanceName;
if (!canMultiInstantiated)
{
//check if the class has been instantiated
//yes: continue
//no: instanceName=TheExistingInstance
//and return the reference of the existing instance
bool existing = CheckForExistance(classFullName);//dont
know how to check
if (existing)
return TheExistingInstance; // I do want to return
the ref of the existing instacne ,but dont know how
// return default(T);
}
instanceName =
(T)Activator.CreateInstance(Type.GetType(classFull Name, true, false));
return instanceName;
}

I have create an instance by the class full name successfully, but I
dont know how to check if another instance has been existing in the
current applicaiton ,can anyone help me??

Thanks very muck!

May 12 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Rader" <ra*******@gmail.com> wrote:
I have create an instance by the class full name successfully, but I
dont know how to check if another instance has been existing in the
current applicaiton ,can anyone help me??


I would suggest that you keep track of the instance when it is created,
in its constructor or always creating instances via some kind of factory
method, and keeping track of instances there.

There is no general way that I know of to enumerate all instances on the
GC heap, unless you're willing to consider debugging the application
from a separate process.

-- Barry
May 12 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.