P
Pavils Jurjans
Hello,
I have two separate questions on C# features, related with classes:
1.
Is there a way to store class references, say in array, or collection,
without creating an instance? I have an application that has a number of
"document" objects that are each a separate class that inherit from common
parent, "Document" class. In the application session, I may use only some of
these document classes, so I'd like to instantiate them only if there's a
need, and then I could use the instance until the end of session
(singleton). Currently I keep a hastable with single instance of every
document class and then refer to the requested one upon need. However, I am
worried that it's an ineffective way, since I take up memory with objects
that may never be used. Instead, I think, it would be more efficient, if I
could keep a hashtable with class references, and then instantiate object
only when needed. Something like this:
public class TxtDocClass : Document
{
....
}
.... more classes defined
Hashtable docClassLib = new Hashtable();
docClassLib.Add("txtDoc", TxtDocClass);
docClassLib.Add("docDoc", DocDocClass);
docClassLib.Add("xlsDoc", XlsDocClass);
// Instantiate one
class RequestedClass = docClassLib["xlsDoc"];
Document requestedDoc = new RequestedClass();
Of course, this is thought-out syntax, but maybe something like indirect
class reference is possible?
2.
Is there some way to walk over given namespace and take an instance of every
class that is defined in that namespace? This question is related to the
first one. If it is possible to find a way to store class references instead
of instances, I'd like to do that.
Thanks,
Pavils
I have two separate questions on C# features, related with classes:
1.
Is there a way to store class references, say in array, or collection,
without creating an instance? I have an application that has a number of
"document" objects that are each a separate class that inherit from common
parent, "Document" class. In the application session, I may use only some of
these document classes, so I'd like to instantiate them only if there's a
need, and then I could use the instance until the end of session
(singleton). Currently I keep a hastable with single instance of every
document class and then refer to the requested one upon need. However, I am
worried that it's an ineffective way, since I take up memory with objects
that may never be used. Instead, I think, it would be more efficient, if I
could keep a hashtable with class references, and then instantiate object
only when needed. Something like this:
public class TxtDocClass : Document
{
....
}
.... more classes defined
Hashtable docClassLib = new Hashtable();
docClassLib.Add("txtDoc", TxtDocClass);
docClassLib.Add("docDoc", DocDocClass);
docClassLib.Add("xlsDoc", XlsDocClass);
// Instantiate one
class RequestedClass = docClassLib["xlsDoc"];
Document requestedDoc = new RequestedClass();
Of course, this is thought-out syntax, but maybe something like indirect
class reference is possible?
2.
Is there some way to walk over given namespace and take an instance of every
class that is defined in that namespace? This question is related to the
first one. If it is possible to find a way to store class references instead
of instances, I'd like to do that.
Thanks,
Pavils