accessing static members through Type?

R

Rainer Queck

Hi NG,

I have a generic list of Type where I hold some class types like:

List<Type> classList = new List<Type>();
classList.Add(typeof(Telegram1);
classList.Add(typeof(Telegram2);
......

Each of my Telegram<x> classes has a static Member "TlgNo";
With out building a instance of the class, I can get the TlgNo like
int tlgNo = Telegram1.TlgNo;

Is there a way to get this static member of the class from my list?
What I would like to do is:

for(int i=0; i<classList.Count; i++)
{
aTlgNo = classList.TlgNo;
......
}

Thanks for hints,

Rainer Queck
 
J

Joanna Carter [TeamB]

"Rainer Queck" <[email protected]> a écrit dans le message de (e-mail address removed)...

| I have a generic list of Type where I hold some class types like:
|
| List<Type> classList = new List<Type>();
| classList.Add(typeof(Telegram1);
| classList.Add(typeof(Telegram2);
| .....
|
| Each of my Telegram<x> classes has a static Member "TlgNo";
| With out building a instance of the class, I can get the TlgNo like
| int tlgNo = Telegram1.TlgNo;
|
| Is there a way to get this static member of the class from my list?
| What I would like to do is:
|
| for(int i=0; i<classList.Count; i++)
| {
| aTlgNo = classList.TlgNo;
| ......
| }

Assuming TlgNo is a static property, you need to use reflection like this :

foreach (object o in classList)
{
Type itemType = o.GetType();

PropertyInfo tlgNoProperty = itemType.GetProperty("TlgNo");
if (tlgNoProperty != null)
{
int aTlgNo = (int) tlgNoProperty.GetValue(null, null);
...
}
...
}

Joanna
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

A more object oriented solution could be to add an abstract property in
the base class and implement it in each class:

class Telegram {
abstract int TelegramNo { get; }
}

class Telegram1 : Telegram {
static int TlgNo = 1;
public virtual int TelegramNo { get { return TlgNo; } }
}

class Telegram2 : Telegram {
static int TlgNo = 2;
public virtual int TelegramNo { get { return TlgNo; } }
}

(It's my personal opinion that reflection generally is bad for program
structure. ;) )
 
J

Joanna Carter [TeamB]

"Göran Andersson" <[email protected]> a écrit dans le message de %[email protected]...

|A more object oriented solution could be to add an abstract property in
| the base class and implement it in each class:

....

| (It's my personal opinion that reflection generally is bad for program
| structure. ;) )

Unfortunately Rainer wanted to access the TlgNo value without having to
instantiate anything. Therefore, virtual properties/methods are not a viable
option.

Therefore, in the absence of virtual static members, the simplest choice is
reflection.

Joanna
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Aha, I see. When I look at the code again, I see that the List is only
containing the type of the objects, not any objects. Odd construction...
but in that case there is not much to do but to use reflection.

I didn't pay that much attention to the code as it isn't the actual code
that is used, as it won't even compile...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top