List<Type>

R

Rainer Queck

Hi NG,

I have a base class "Telegram". This class , as well as all descants have a
static public field "public static int TelegramNo = <a tlg no>;"

Now I added all descants of Telegram to a generic List<Type>:

List<Type> tlgList = new List<Type>();

TlgList.Add(typeof(Telegram1));

TlgList.Add(typeof(Telegram2));

The idea now is, that if I receive a tlg stream I look for a telegram
nummber in it, and then scan my tlgList for the right class to instanciat.

My problem now is, how can I do that?

A static field of a class I can access by :

int i = Telegram.TelegramNo;

but if I try the same with:

tlgList.TelegramNo I can't compile my code any more, because of
"System.Type has no definition for TelegramNo".

How can I get access to the static field, with out creating an instance of
Telegram?

Thanks for hints and help!

Regards

Rainer
 
J

Jon Skeet [C# MVP]

Rainer Queck wrote:

How can I get access to the static field, with out creating an instance of
Telegram?

You'd need to use reflection. You're basically trying to add
polymorphism over static members, which doesn't exist. An alternative
would be to have a class TelegramType which had the Type it was about,
and the number.

Jon
 
R

Rainer Queck

Hi Jon,

thans for you answer.
A little background Info...
I am comming from Delphi, where there is a class "TClassList".
There I was able to store the Class in the ist and use its static (class)
members like
You'd need to use reflection. You're basically trying to add
polymorphism over static members, which doesn't exist. An alternative
would be to have a class TelegramType which had the Type it was about,
and the number.
But then I would have to build a instance of TelegramType, before I can
access it's number, right?
My thought was not to build any instance, before I know which one to build.

I already thought about a hash table where the key could be the telegram
number....
Do you think that would be a solution?

Regards
Rainer
 
J

Jon Skeet [C# MVP]

Rainer said:
A little background Info...
I am comming from Delphi, where there is a class "TClassList".
There I was able to store the Class in the ist and use its static (class)
members like
myClassList.<static Field>.
I was now looking for a simular mechanism in C#.


Yes - I believe Delphi has polymorphism over types as well as
instances. .NET doesn't.
But then I would have to build a instance of TelegramType, before I can
access it's number, right?
My thought was not to build any instance, before I know which one to build.

These would be instances of type descriptors though - not the telegrams
themselves. Each telegram type would have an instance of TelegramType
which knew its number and how to build a Telegram instance.
I already thought about a hash table where the key could be the telegram
number....
Do you think that would be a solution?

Yes, that would work too. It depends whether you want to put more
useful information in, such as how to build a Telegram instance given
the type number.

Jon
 
R

Rainer Queck

Hi Jon,
Yes, that would work too. It depends whether you want to put more
useful information in, such as how to build a Telegram instance given
the type number.
In my case I think that should be enough. A telegram class would know how
build its instance by a given byte stream.
Would building the instance with

Activator.CreateInstance(telegram1,{tlgByteStream});

where tlgByteStream is a byte[] and ther is a constructor to telegram1 like
"telegram1(byte[] msg)" work?

Regards
Rainer
 
J

Jon Skeet [C# MVP]

Rainer said:
Yes, that would work too. It depends whether you want to put more
useful information in, such as how to build a Telegram instance given
the type number.
In my case I think that should be enough. A telegram class would know how
build its instance by a given byte stream.
Would building the instance with

Activator.CreateInstance(telegram1,{tlgByteStream});

where tlgByteStream is a byte[] and ther is a constructor to telegram1 like
"telegram1(byte[] msg)" work?

Nearly - you'd need to change it to

Activator.CreateInstance (telegram1, new object[]{tlgByteStream});

But other than that, it should work.

Jon
 
R

Rainer Queck

Hi Jon,
Nearly - you'd need to change it to

Activator.CreateInstance (telegram1, new object[]{tlgByteStream});

But other than that, it should work.
Great! Then this seems to be the way, how I will do it.

Thanks for your Help!
Rainer
 
R

Rainer Queck

Hi Nick,

thanks for this hint!
Dictionary looks good for my purpose.

Regards
Rainer
 

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