dynamic property discovery?

J

jason

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
 
J

Jason Newell

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
 
S

Steve Walker

jason said:
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.
 
M

Matt

jason said:
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
 
J

jason

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!
 
J

jason

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!
 
M

Matt

jason said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top