473,657 Members | 2,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extracting DataTypes for propeties from a class

Hi,

I have a class with properties and each property has specific
datatypes.

Is it possible to extract the datatypes of each property found in a
class?

I would like to extract these datatypes so that I can build a dataset
schema structure at runtime.

Is this possible?

public class MyProperties
{
private int age;
private string name;

public int Age
{
get{...}
set{...}
}

public string Name
{
get{...}
set{...}
}
}

DataTable dt = new DataTable();
foreach(Propert y p in MyProperties)
{
DataColumn dc = new DataColum(p.Nam e, p.DataType);
dt.Columns.Add( dc);
}

Can someone help me out
Thanks in Advance

Nov 17 '05 #1
4 1461
Curious,

You will want to use reflection for this. You can get the Type instance
that represents the type of the class by using the GetType method on Type
(or the object itself, if you have an instance of it), or, if you know the
type at compile-type, you can use the typeof() operator.

Once you have that, you can call GetProperties to get the properties on
the type. The PropertyInfo instances returned will have a PropertyType
property which will return the Type of the property, as well as the Name
property, which returns the name of the property.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Curious" <th****@mail.gl obal.net.mt> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Hi,

I have a class with properties and each property has specific
datatypes.

Is it possible to extract the datatypes of each property found in a
class?

I would like to extract these datatypes so that I can build a dataset
schema structure at runtime.

Is this possible?

public class MyProperties
{
private int age;
private string name;

public int Age
{
get{...}
set{...}
}

public string Name
{
get{...}
set{...}
}
}

DataTable dt = new DataTable();
foreach(Propert y p in MyProperties)
{
DataColumn dc = new DataColum(p.Nam e, p.DataType);
dt.Columns.Add( dc);
}

Can someone help me out
Thanks in Advance

Nov 17 '05 #2
I managed to get the name of the properties, but not that of the
datatype.

System.Reflecti on.MemberInfo[] members =
typeof(MyProper ties).GetProper ties();
foreach(System. Reflection.Memb erInfo m in members)
Console.WriteLi ne("{0} {1}",m.Name, m.DeclaringType );

Am I on the correct path?

Nov 17 '05 #3
Thanks for your help, I managed to get it out!!!

System.Reflecti on.PropertyInfo[] properties =
typeof(MyProper ties).GetProper ties();
foreach(System. Reflection.Prop ertyInfo p in properties)
Console.WriteLi ne("{0} {1}",p.Name, p.PropertyType) ;

Nov 17 '05 #4
Curious,

Almost, the GetProperties method returns an array of PropertyInfo[]:

System.Reflecti on.PropertyInfo[] properties =
typeof(MyProper ties).GetProper ties();

foreach(Propert yInfo p in properties)
Console.WriteLi ne("{0} {1}", p.Name, m.PropertyType) ;
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Curious" <th****@mail.gl obal.net.mt> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
I managed to get the name of the properties, but not that of the
datatype.

System.Reflecti on.MemberInfo[] members =
typeof(MyProper ties).GetProper ties();
foreach(System. Reflection.Memb erInfo m in members)
Console.WriteLi ne("{0} {1}",m.Name, m.DeclaringType );

Am I on the correct path?

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
6135
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
9
3248
by: Mark Rae | last post by:
Hi, I posted a couple of days ago about the possibility of "simulating" in ..NET1.1 the nullable datatypes available in .NET2.0 - I'm nearly there, but require a bit more guidance. Basically, I have a class file which contains the various class definitions, as follows: using System;
19
626
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
13
3725
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of these methods EXTRACT the data at one point or another. The other problem is there appears to be NO WAY to get at the actual buffer pointer (char*) of the characters in the stream. There is a way to get the streambuf object associated with the...
0
8823
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.