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

C#-APP:Getting type at runtime

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
12 2520
nukefusion
221 Expert 100+
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
dantz
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
221 Expert 100+
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
dantz
71
Thanks a lot man.

That works for me.
:)
Dec 15 '08 #5
dantz
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
221 Expert 100+
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
dantz
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
221 Expert 100+
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
dantz
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
dantz
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
221 Expert 100+
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
dantz
71
Great! I got it working..thanks you so much
Really learned a lot from you.
Dec 18 '08 #13

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

Similar topics

2
by: ksskumar2000 | last post by:
Hi, I have added following two reference under COM tab, Microsoft Office 11.0 Object Library Microsoft Word 11.0 Object Library The software I have used: Visual studio 1.14 Microsoft Office...
26
by: Michael McGarry | last post by:
Hi, I am pretty sure this is not possible, but maybe somehow it is. Given a variable, can I tell what type it is at runtime? Michael
2
by: Larry | last post by:
I'm having trouble finding the right method to determine at runtime the data type of the fields in a class. here's what I've got so far: Public Class ImgSubmissionRecord Public fileName As...
6
by: Sameh Ahmed | last post by:
Hello there I need to get the "PwdLastSet" of a user object to know when he last set his password. I am using DirectoryServices.DirectoryEntry to bind to the user object, but it either gives...
20
by: Shawnk | last post by:
I would like to get the class INSTANCE name (not type name) of an 'object'. I can get the object (l_obj_ref.GetType()) and then get the (l_obj_typ.Name) for the class name. I there any way of...
2
by: MSK | last post by:
Hi, Continued to my earlier post regaring "Breakpoints are not getting hit" , I have comeup with more input this time.. Kindly give me some idea. I am a newbie to .NET, recently I installed...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
5
by: SunnyDrake | last post by:
HI! I wrting some program part of it is XML config parser which contains some commands(for flexibility of engenie). how do i more simple(if it possible not via System.Reflection or...
2
by: nassegris | last post by:
Hello! I need to somehow extract the TypeLib ID from a COM-dll compiled using VB6. Is is possible or do I have to look it up in the registry somehow (ie by iterating over TypeLibIDs in...
0
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.