473,320 Members | 2,097 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,320 software developers and data experts.

Two questions on C# classes

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
Nov 16 '05 #1
8 1996
Jon Skeet [C# MVP] wrote:

For each assembly, you can get the assemblies it references using
Assembly.GetReferencedAssemblies. You can then basically load the
transitive closure of all the assemblies involved, and then look at
them.


Aha, Thanks.

Rgds Tim.
Nov 16 '05 #2
Can I answer your question in 2 ways.

Firstly, you can store an array of class types.

e.g.
Type t = typeof(MyDocumentClass);

I'll let you decide the best way to store the t object. You can then
instanciate the classes perhapse using reflection or someother method
when needed.

e.g.
System.Type t = typeof(MyClass);
MyClass myClass = (MyClass)t.GetConstructors()[0].Invoke(new object[]
{"Hello"});

with

public class MyClass
{
public MyClass(string s)
{
Console.WriteLine(s);
}
}

Note: This could also be slow as reflection is slower than directly
manipulating objects/classes.

Secondly, for performance related issues I'd recommend you measure the
performance problem (where possible) instead of guessing what the
performance problem is.

i.e. I run my app and it runs too slow - so I imporve where I *think*
the bottleneck is, and (as always) I was wrong and I have imporoved
performance but not where it matters - so the app is still slow...

Adam
Nov 16 '05 #3
Pavils Jurjans <pa****@mailbox.riga.lv> wrote:
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?
<snip>
Of course, this is thought-out syntax, but maybe something like indirect
class reference is possible?
I think you're looking for the typeof() operator, which gives a Type
reference. You can then use that Type in Activator.CreateInstance, for
example.
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.


You can find out all classes in a namespace within a particular
assembly, yes - just by finding all the types within the assembly, and
only "remembering" the ones which match the namespace you're interested
in. See Assembly.GetTypes for more information.

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

P.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Pavils Jurjans <pa****@mailbox.riga.lv> wrote:
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?


<snip>
Of course, this is thought-out syntax, but maybe something like indirect
class reference is possible?


I think you're looking for the typeof() operator, which gives a Type
reference. You can then use that Type in Activator.CreateInstance, for
example.
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.


You can find out all classes in a namespace within a particular
assembly, yes - just by finding all the types within the assembly, and
only "remembering" the ones which match the namespace you're interested
in. See Assembly.GetTypes for more information.

--
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
Jon Skeet [C# MVP] wrote:
interested in. See Assembly.GetTypes for more information.


Hi Jon,

Just to piggy back on this thread, in an application that uses many
assemblies (9 or 10 plus) is there any way of getting all types from
all assemblies, including those that have not yet been loaded.

The specific issue I am having is looking for a class attribute
decorating a class, for registration into a factory object. The "fudge"
I am using at the moment is referencing the class and forcing the
assembly to be loaded, In itself not a big problem, but means that I
can't just add a new decorated class and have the registration code
pick it up, I have to alter the registration code as well.

Rgds Tim.
Nov 16 '05 #6
Tim Jarvis <tj*****@NoSpamForMe.com> wrote:
Just to piggy back on this thread, in an application that uses many
assemblies (9 or 10 plus) is there any way of getting all types from
all assemblies, including those that have not yet been loaded.

The specific issue I am having is looking for a class attribute
decorating a class, for registration into a factory object. The "fudge"
I am using at the moment is referencing the class and forcing the
assembly to be loaded, In itself not a big problem, but means that I
can't just add a new decorated class and have the registration code
pick it up, I have to alter the registration code as well.


You can find all the currently loaded assemblies in the AppDomain with
AppDomain.GetAssemblies.

For each assembly, you can get the assemblies it references using
Assembly.GetReferencedAssemblies. You can then basically load the
transitive closure of all the assemblies involved, and then look at
them.

I wrote some primitive code to do the reference walking a while ago.
There may be some extra implications I'm unaware of, but it seems to
work. Do a groups.google.com search for a thread called
".NET Dependency Walker?" and you should find it.

--
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
Jon Skeet [C# MVP] wrote:

For each assembly, you can get the assemblies it references using
Assembly.GetReferencedAssemblies. You can then basically load the
transitive closure of all the assemblies involved, and then look at
them.


Aha, Thanks.

Rgds Tim.
Nov 16 '05 #8
Can I answer your question in 2 ways.

Firstly, you can store an array of class types.

e.g.
Type t = typeof(MyDocumentClass);

I'll let you decide the best way to store the t object. You can then
instanciate the classes perhapse using reflection or someother method
when needed.

e.g.
System.Type t = typeof(MyClass);
MyClass myClass = (MyClass)t.GetConstructors()[0].Invoke(new object[]
{"Hello"});

with

public class MyClass
{
public MyClass(string s)
{
Console.WriteLine(s);
}
}

Note: This could also be slow as reflection is slower than directly
manipulating objects/classes.

Secondly, for performance related issues I'd recommend you measure the
performance problem (where possible) instead of guessing what the
performance problem is.

i.e. I run my app and it runs too slow - so I imporve where I *think*
the bottleneck is, and (as always) I was wrong and I have imporoved
performance but not where it matters - so the app is still slow...

Adam
Nov 16 '05 #9

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

Similar topics

1
by: nick | last post by:
Hi - apologies if these have been asked before. I've been looking around, but can't find answers to my question. Anyway... I'm an experienced programmer with JSP, ASP, and ASP.NET and am...
3
by: alexhong2001 | last post by:
When design a class, should always make it "derivable" as a base class? Is there really a situation that the designed class not "derivable"? When should make a member "protected"? Only when...
1
by: Invalidlastname | last post by:
Hi, I have some questions regarding to use vs.net to generate xsd from database tables then generate classes, not typed datasets, from the xsd. Basically I want to have some light-weigh classes...
1
by: jason | last post by:
Hello everyone, I have some general questions about the DataTable object, and how it works. Moderately new to C#, I have plenty of texts describing the language, but not so much to reference...
3
by: Phil Lee | last post by:
Hi, I have a few questions regarding web services in .NET 2 1) Why, when I run code analysis do I get a source controlled files named {guid}/codeanalysislog.xml...
8
by: NH | last post by:
Hi, I'm looking for some opinions and advice on designing ASP.Net apps. Let me explain my approach to how I currently design and hopefully you can give me some advice. I create systems for a...
7
by: alternativa | last post by:
Hello, I have a few questions concerning classes. 1) Why some people use default constructos, i.e constructors with no parameters? To me it doesn't make any sense, is there something I should...
9
by: Setash | last post by:
I've got a tiny bit of coding background, but its not the most extensive. That said, I'm trying to wrap my head around python and have a couple questions with classes and functions. Two...
19
by: adriancico | last post by:
Hi I am working on a python app, an outliner(a window with a TreeCtrl on the left to select a document, and a RichTextBox at the right to edit the current doc). I am familiarized with OOP...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.