By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,966 Members | 2,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,966 IT Pros & Developers. It's quick & easy.

C#-APP:Getting type at runtime

P: 71
Hi all, can someone please help me on this.
I want to get the type of the property at run time but when I use the
code below C# just return the type as "RuntimePropertyInfo"

Expand|Select|Wrap|Line Numbers
  1. public static void RecurseOnProp(PropertyInfo[] propInfo)
  2.  {
  3.                 foreach (PropertyInfo controlProperty in propInfo)
  4.                 {
  5.                     myType = controlProperty.GetType();
  6.                 }
  7. }
  8.  
How can I know if the type is a string,uint32 and others?

Thanks in advance.
Dec 12 '08 #1
Share this Question
Share on Google+
12 Replies


nukefusion
Expert 100+
P: 221
GetType() always returns the type of the object it is called on, which in this case is PropertyInfo. You'll need to use the PropertyType property instead. The following code sample will do what you need:

Expand|Select|Wrap|Line Numbers
  1. foreach (PropertyInfo controlProperty in propInfo)
  2. {
  3.     Type type = controlProperty.PropertyType;
  4.  
Dec 12 '08 #2

P: 71
thanks so much.

I have a follow up though after that.
i want to get the Properties inside that Type.

Below is the sample of my class and i want to loop on all the properties inside it
including the class properties inside.
Expand|Select|Wrap|Line Numbers
  1. public class RoomState
  2.     {
  3.         private uint _numOfPlayers;
  4.         private uint _playerTurn;
  5.         public uint NumOfPlayers
  6.         {
  7.             get { return _numOfPlayers; }
  8.             set { _numOfPlayers = value; }
  9.         }
  10.         public uint PlayerTurn
  11.         {
  12.             get { return _playerTurn; }
  13.             set { _playerTurn = value; }
  14.         }
  15.     }
  16. public class State
  17.     {
  18.         private RoomState _gmRoomState = new RoomState();
  19.  
  20.         public RoomState RoomState
  21.         {
  22.             get { return _gmRoomState; }
  23.             set { _gmRoomState = value; }
  24.         }
  25. }
  26. public class PlayMessage : MessageHeader
  27.     {
  28.         public PlayMessage() { } 
  29.         private uint _gmID;
  30.         private uint _gmTableID;
  31.         private uint _playerID;
  32.         private State _gState = new State();
  33.         private PlayerAction _playerAction = new PlayerAction();
  34.  
  35.         public uint GameID
  36.         {
  37.             get { return _gmID; }
  38.             set { _gmID = value; }
  39.         }
  40.         public uint GameTableID
  41.         {
  42.             get { return _gmTableID; }
  43.             set { _gmTableID = value; }
  44.         }
  45.         public uint PlayerID
  46.         {
  47.             get { return _playerID; }
  48.             set { _playerID = value; }
  49.         }
  50.         public State State
  51.         {
  52.             get { return _gState; }
  53.             set { _gState = value; }
  54.         }
  55.  
Thanks in advance.
Dec 12 '08 #3

nukefusion
Expert 100+
P: 221
Then you would probably need to set up a recursive function along the lines of:

Expand|Select|Wrap|Line Numbers
  1. private void getProperties(Type type)
  2. {
  3.     PropertyInfo[] propInfo = type.GetProperties();
  4.     foreach (PropertyInfo controlProperty in propInfo)
  5.     {
  6.         Type propertyType = controlProperty.PropertyType;
  7.         getProperties(propertyType);
  8.     } 
  9.  
  10. }
  11.  
and call it with the type you want to start from:

Expand|Select|Wrap|Line Numbers
  1. getProperties(typeof(PlayMessage));
  2.  
Obviously you'd need to add additional code to actually store or do something with the harvested information.
Dec 12 '08 #4

P: 71
Thanks a lot man.

That works for me.
:)
Dec 15 '08 #5

P: 71
I hope this follow up question can reach you.

inside the recursion I need to get the value,
i am adding this line of code:

Expand|Select|Wrap|Line Numbers
  1. foreach (PropertyInfo controlProperty in propInfo) 
  2.     { 
  3.         Type propertyType = controlProperty.PropertyType; 
  4.         object val = controlProperty.GetValue( , null);
  5.         getProperties(propertyType); 
  6.     }  
  7.  
  8.  
but I seem can't find any object to put as the parameter in the GetValue.

I am trying this one:
Expand|Select|Wrap|Line Numbers
  1. resultClass = Activator.CreateInstance(controlProperty.DeclaringType);
  2.  
  3.  
then
but I think it just gave me new object without any value inside.

Do you have any idea?
Dec 15 '08 #6

nukefusion
Expert 100+
P: 221
Your absolutely right. Your second code sample just creates a new instance of that object, so the value returned will just be the default value for a new object of that type.

In order to return a value for the property you need to have a reference to an actual instance of the object you want to get the value for.

What exactly is it that you are trying to acheive? It may be that there is a simpler way.
Dec 15 '08 #7

P: 71
gee..thanks.
My main task is to convert that structure into a string(comma separated)then send it using WM_COPYDATA. The sender and reciever are both in C#.
I want to loop on all the properties and get their names and values.
The format will be: PropertyName=Value
Ex. MessageID=01,GameID=100,etc...

Hope you can help me..thanks in advance
Dec 15 '08 #8

nukefusion
Expert 100+
P: 221
Ah ok. Well there are a number of different ways to communicate between two different processes, a more .NET way for example might be to use remoting, or maybe even named pipes, but I'm not sure that they're simpler to implement than what you're trying to do with WM_COPYDATA. If you're going to want to do this sort of communication extensively you might want to look into those alternative methods.

I think something like the following could acheive what you are after, based on the work you have done already:

Expand|Select|Wrap|Line Numbers
  1. private void getProperties(Type type, object instance)
  2. {
  3.     PropertyInfo[] propInfo = type.GetProperties();
  4.     foreach (PropertyInfo controlProperty in propInfo)
  5.     {
  6.         Type propertyType = controlProperty.PropertyType;
  7.         object propertyValue = controlProperty.GetValue(instance, null);
  8.         getProperties(propertyType, propertyValue);
  9.     } 
  10. }
  11.  
In this sample propertyType will contain your type and propertyValue will contain the value. You'll need to kick off the function with a reference to the instance you want to get the values for, i.e:

Expand|Select|Wrap|Line Numbers
  1. PlayMessage myPlayMessage = new PlayMessage();
  2. ...
  3. // Init object here
  4. ...
  5. getProperties(myPlayMessage.GetType(), myPlayMessage);
  6.  
Dec 15 '08 #9

P: 71
Wow..can't thank you much.
I run it initially and it works great.
I hope not to find another bug on it..whew..

You save me there man.

thank you so much.

*I wish I have time to implement the Named Pipes and Remoting. But as of now I am stuck here because this is the fastest way I can achieve what I need. The .Net way may still need some learning curve but i am on my on to that.

cheers!
:-)
Dec 16 '08 #10

P: 71
Hi, my problem has become bigger..lolz
I hope there is still an answer for this.
Now I have an array property but I don't know how can I get into its inner values.
Expand|Select|Wrap|Line Numbers
  1.  
  2. if (propertyType.IsArray)
  3. {
  4.     int max = (int)CheckPropVal(typeof(int) , propvalue.ToString());
  5.     for (int arrayctr = 0; arrayctr <= max; arrayctr++)
  6.     {
  7.         //i am stuck here
  8.     }
  9.  
  10. //CheckPropVal is a converter function that I created that uses TryParse inside to convert into proper type, the return value is object.
  11.  
hope you can still help me...thanks in advance.
Dec 18 '08 #11

nukefusion
Expert 100+
P: 221
You should be able to use the same method as before. Again, you'll need an instance to work with. I've modified my previous code sample to work with an array. You should be able to tailor it to suit your needs.

Expand|Select|Wrap|Line Numbers
  1. private void getProperties(Type type, object instance) 
  2.     PropertyInfo[] propInfo = type.GetProperties(); 
  3.     foreach (PropertyInfo controlProperty in propInfo) 
  4.     { 
  5.         Type propertyType = controlProperty.PropertyType; 
  6.         if (propertyType.IsArray)
  7.         {
  8.             Array arr = (Array)controlProperty.GetValue(m, null);
  9.             for (int i = 0; i < arr.Length; i++)
  10.             {
  11.                 object value = arr.GetValue(i);
  12.                 // Do something with the array value here
  13.             }
  14.         }
  15.     }  
  16.  
  17.  
Dec 18 '08 #12

P: 71
Great! I got it working..thanks you so much
Really learned a lot from you.
Dec 18 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.