473,472 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Create 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(Property p in MyProperties)
{
DataColumn dc = new DataColum(p.Name, p.DataType);
dt.Columns.Add(dc);
}

Can someone help me out
Thanks in Advance

Nov 17 '05 #1
4 1453
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.com

"Curious" <th****@mail.global.net.mt> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.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(Property p in MyProperties)
{
DataColumn dc = new DataColum(p.Name, 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.Reflection.MemberInfo[] members =
typeof(MyProperties).GetProperties();
foreach(System.Reflection.MemberInfo m in members)
Console.WriteLine("{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.Reflection.PropertyInfo[] properties =
typeof(MyProperties).GetProperties();
foreach(System.Reflection.PropertyInfo p in properties)
Console.WriteLine("{0} {1}",p.Name, p.PropertyType);

Nov 17 '05 #4
Curious,

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

System.Reflection.PropertyInfo[] properties =
typeof(MyProperties).GetProperties();

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

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

System.Reflection.MemberInfo[] members =
typeof(MyProperties).GetProperties();
foreach(System.Reflection.MemberInfo m in members)
Console.WriteLine("{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
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....
9
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,...
19
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....
13
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
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,...
1
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...
0
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...
0
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...
0
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 ...

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.