member-name as string

  • Thread starter Thread starter Ruben
  • Start date Start date
R

Ruben

Hi!

I am looking for a way to get the name of a class-member as string, as
example:

class A
{
public string a;
}

[...]

string MemberName = GiveMeTheNameOf(A.a);

What I want is: MemberName == "A.a"

Is there any way to do that?

Thank you!
Ruben
 
I am looking for a way to get the name of a class-member as string, as
example:

class A
{
public string a;
}
[...]

string MemberName = GiveMeTheNameOf(A.a);

What I want is: MemberName == "A.a"

Is there any way to do that?

What exactly are you trying to achieve with this? If you are able to pass
the member into a method, you obviously know its name. Could you explain
the circumstances a little more clearly?

Best Regards,
Dustin Campbell
Developer Express Inc.
 
The name of the class member is pointless in the way you are describing it.

If you want to be able to access the member, based on string name, that is
different.

Such as Get(A, "a"). That requires reflection.
 
I need the name of a member in a string to use this string for a
SQL-database-query.
It would be possible to hardcode this string but this would make the
code very unflexible.

I hope this explains my post...

Thank you!
Ruben
 
Ruben said:
I need the name of a member in a string to use this string for a
SQL-database-query.
It would be possible to hardcode this string but this would make the
code very unflexible.

I hope this explains my post...

Thank you!
Ruben

Ruben,

Are you not hard-coding it when you're passing in A.a?

But, by the by, no you can't do it like this. You can either enumerate
across the fields in a class, or you can request the field info, given a
string representation of the name. You cannot get a string representation
of the name, giving a fully qualified reference to the field, since a
fully-qualified reference evaluates to the value of that field.
 
Ruben,

What you're looking for are te "Reflection" classes in System.Reflection. I
adapted the sample from the MSDN library with your "A" class:

using System;
using System.Reflection;

public class FieldInfoClass
{
// the class you're interested in:
public class TestClass
{
public string a;
}

// the sample from MSDN, modified to use the test class
public static void Main()
{
FieldInfo[] myFieldInfo;
TestClass A = new TestClass();

Type myType = A.GetType();

// Get the type and fields of TestClass
myFieldInfo = myType.GetFields(BindingFlags.NonPublic |
BindingFlags.Instance
| BindingFlags.Public);

Console.WriteLine("\nThe fields of " +
"FieldInfoClass are \n");
// Display the field information of FieldInfoClass.
for(int i = 0; i < myFieldInfo.Length; i++)
{
Console.WriteLine("\nName : {0}",
myFieldInfo.Name);
Console.WriteLine("Declaring Type : {0}",
myFieldInfo.DeclaringType);
Console.WriteLine("IsPublic : {0}",
myFieldInfo.IsPublic);
Console.WriteLine("MemberType : {0}",
myFieldInfo.MemberType);
Console.WriteLine("FieldType : {0}",
myFieldInfo.FieldType);
Console.WriteLine("IsFamily : {0}",
myFieldInfo.IsFamily);
}
}
}
 
andrew said:
What you're looking for are te "Reflection" classes in System.Reflection. I
adapted the sample from the MSDN library with your "A" class:
[...]

Ok, I see it is possible to traverse threw all members of a class.
But this is not enough for my purpose, is there a way to get explicitly
the name of a certain member?

But since extensive googling did not bring up something nice, I fear
there is no solution for my problem...

Thank you all!
Ruben
 
Ruben said:
andrew said:
What you're looking for are te "Reflection" classes in System.Reflection. I
adapted the sample from the MSDN library with your "A" class:
[...]

Ok, I see it is possible to traverse threw all members of a class.
But this is not enough for my purpose, is there a way to get explicitly
the name of a certain member?

But since extensive googling did not bring up something nice, I fear
there is no solution for my problem...

It seems that what you're trying to write is something that I believe
is called an OPF, an Object Persistence Framework, or an ORM, an
object-relational mapping. In simpler terms, a standard way to store
object information into a relational database and get it back again
later.

You should probably read up on OPFs and ORMs. I'm sure that there are
lots of clever people out there who have written up how to build these
things, and their designs are better than anything your or I could
produce. Perhaps you should take a step back and see if there is an
established solution to the larger problem you're facing.
 
Ruben said:
andrew said:
What you're looking for are te "Reflection" classes in System.Reflection.
I
adapted the sample from the MSDN library with your "A" class:
[...]

Ok, I see it is possible to traverse threw all members of a class.
But this is not enough for my purpose, is there a way to get explicitly
the name of a certain member?

But since extensive googling did not bring up something nice, I fear
there is no solution for my problem...

Thank you all!
Ruben

Ah, I see. One bizarre und unsavory way of achieving that would be to stash
the old value of your member, set it to some recognizable test value, then
iterate through all fields in the reflection info of the class, call
"GetValue" and then see which one it is. At that point you know which member
it is and extract the string. To be safe you'd probably have to try at least
two test values to avoid finding a field that happens to already have your
test value.

Andrew
 

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

Back
Top