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

dynamic property discovery?

hello everyone,

is there any way in C# to perform dynamic property discovery? i know
such a thing was possible in C++ template code, but it was kind of an
ugly mess. i'm just curious if there is a way to do such a thing in C#,
not suggesting that C# should be able to do things that C++ did (no
need to start any wars here)

for example, if i have:

public class MyClass
{
MyClass() { }

public int P1;
public string P2;
}

can i somehow discover these properties at runtime? such as:

MyClass obj = new MyClass();

// #discovery psuedo-code:
// for each property in obj do
Console.WriteLine(Convert.ToString(/*property*/));

thanks for any help!

jason

Nov 17 '05 #1
6 1698
Is this what you're wanting?

Type f = typeof(MyClass);
PropertyInfo[] properties = f.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine(property.Name);
}

Jason Newell, MCAD
Software Engineer
jason wrote:
hello everyone,

is there any way in C# to perform dynamic property discovery? i know
such a thing was possible in C++ template code, but it was kind of an
ugly mess. i'm just curious if there is a way to do such a thing in C#,
not suggesting that C# should be able to do things that C++ did (no
need to start any wars here)

for example, if i have:

public class MyClass
{
MyClass() { }

public int P1;
public string P2;
}

can i somehow discover these properties at runtime? such as:

MyClass obj = new MyClass();

// #discovery psuedo-code:
// for each property in obj do
Console.WriteLine(Convert.ToString(/*property*/));

thanks for any help!

jason

Nov 17 '05 #2
In message <11*********************@g47g2000cwa.googlegroups. com>, jason
<ia****@yahoo.com> writes
hello everyone,

is there any way in C# to perform dynamic property discovery? i know
such a thing was possible in C++ template code, but it was kind of an
ugly mess. i'm just curious if there is a way to do such a thing in C#,
not suggesting that C# should be able to do things that C++ did (no
need to start any wars here)


Yes, very easily. Google for C# and Reflection.

--
Steve Walker
Nov 17 '05 #3

jason wrote:
hello everyone,

is there any way in C# to perform dynamic property discovery? i know
such a thing was possible in C++ template code, but it was kind of an
ugly mess. i'm just curious if there is a way to do such a thing in C#,
not suggesting that C# should be able to do things that C++ did (no
need to start any wars here)

for example, if i have:

public class MyClass
{
MyClass() { }

public int P1;
public string P2;
}

can i somehow discover these properties at runtime? such as:

MyClass obj = new MyClass();

// #discovery psuedo-code:
// for each property in obj do
Console.WriteLine(Convert.ToString(/*property*/));


Sure, look at "reflection" in the c# documentation, or on
codeproject.com. You should find everything you need there. If you need
an example, let me know, and I'll go dig one up.

Matt

Nov 17 '05 #4
Thanks a lot. This is indeed exactly what I was looking for. No need
for examples, but thanks! By the way, do you know if by chance this can
be exposed through COM Interop? I'm probably getting too optimistic,
but if there's a way that we can use reflection in ASP classic, that
would be awesome.

I suppose I could whip up a class that accepted an object reference and
reported the reflection information easily enough. Except that I don't
think my previous attempts to pass an object reference from ASP Classic
into a COM Interop object method have been very successful.

Thanks again for this info though! Even if we can only use it in our
..NET-only apps, this is very useful!

Nov 17 '05 #5
exactly what i was looking for. thanks!

do you by chance have any experiences trying to expose this
functionality through COM Interop? this would be even more useful if we
could access it in ASP Classic somehow. i have another comment
elsewhere in this thread on this line of questioning if you're
interested.

thanks a lot!

Nov 17 '05 #6

jason wrote:
Thanks a lot. This is indeed exactly what I was looking for. No need
for examples, but thanks! By the way, do you know if by chance this can
be exposed through COM Interop? I'm probably getting too optimistic,
but if there's a way that we can use reflection in ASP classic, that
would be awesome.

I suppose I could whip up a class that accepted an object reference and
reported the reflection information easily enough. Except that I don't
think my previous attempts to pass an object reference from ASP Classic
into a COM Interop object method have been very successful.

Thanks again for this info though! Even if we can only use it in our
.NET-only apps, this is very useful!


Reflection should be available in any .NET language. I'm not an ASP
guy, but I'm pretty sure I saw some examples of reflection with ASP
when I was doing research for it originally.

Matt

Nov 17 '05 #7

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

Similar topics

0
by: Anurag | last post by:
Hi, I understand that the dbm cfg parameter "DISCOVER" controls whether Search or Known discovery is applicable. Additionally, Search discovery provides a superset of Known discovery. I completely...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
0
by: Chris | last post by:
Hi, I have created a vsdisco-file on a webserver (http://MyHost/MyWebServices/test.vsdisco), activated dynamic-discovery by adding an entry in <httpHandlers> in machine.config. But then what...
15
by: Nak | last post by:
Hi there, Is it possible to load a web service dynamically? i.e. If the web service were to exist on a system with a non static IP address, an application could download a "locator" file that...
7
by: Mike Livenspargar | last post by:
We have an application converted from v1.1 Framework to v2.0. The executable references a class library which in turn has a web reference. The web reference 'URL Behavior' is set to dynamic. We...
0
by: serge calderara | last post by:
Dear all, Let say that I have developped a web service which is host on Server1. Then one of my customer will like to used that service in its own web site which is not based on .NET platform. ...
7
by: Chris Mullins | last post by:
I'm in the process of building a number of (Web) Services using .NET 3.0 and WCF. These services are intended to be deployed within the Intranet of a very, very large orginization. I need to...
2
by: HockeyFan | last post by:
I have a web service that has been working, but I recently changed it so that the reference would be dynamic. Locally, within Visual Studio, it works fine. However, once deployed, the dll I use...
0
by: news.microsoft.com | last post by:
Hey All, Sorry to post to several groups but wasn't sure which development group would be best for the question. I was wondering if there are any developers out there that have significant...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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.