471,325 Members | 1,469 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

List<Type>

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[i].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


Jan 17 '06 #1
8 5538
Rainer Queck wrote:

<snip>
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

Jan 17 '06 #2
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
myClassList[i].<static Field>.
I was now looking for a simular mechanism in C#.
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
Jan 17 '06 #3
Rainer Queck wrote:
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[i].<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.
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.
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

Jan 17 '06 #4
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
Jan 17 '06 #5
Rainer Queck wrote:
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

Jan 17 '06 #6
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
Jan 17 '06 #7
Use a dictionary to map TelegramNo to Type

"Rainer Queck" <Ra****@noemail.noemail> wrote in message
news:eL**************@TK2MSFTNGP12.phx.gbl...
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[i].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


Jan 18 '06 #8
Hi Nick,

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

Regards
Rainer
Jan 18 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Robert Bravery | last post: by
1 post views Thread by =?Utf-8?B?RnJhbmNvaXNWaWxqb2Vu?= | last post: by
2 posts views Thread by Berryl Hesh | last post: by
6 posts views Thread by A.Rocha | last post: by
reply views Thread by rosydwin | last post: by

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.