member-name as string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ruben

    member-name as string

    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

  • Dustin Campbell

    #2
    Re: member-name as string

    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.


    Comment

    • Mr Icekirby

      #3
      RE: member-name as string

      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.

      "Ruben" wrote:
      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
      >
      >

      Comment

      • Ruben

        #4
        Re: member-name as string

        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

        Comment

        • andrew queisser

          #5
          Re: member-name as string

          Ruben,

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

          using System;
          using System.Reflecti on;

          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.GetField s(BindingFlags. NonPublic |
          BindingFlags.In stance
          | BindingFlags.Pu blic);

          Console.WriteLi ne("\nThe fields of " +
          "FieldInfoC lass are \n");
          // Display the field information of FieldInfoClass.
          for(int i = 0; i < myFieldInfo.Len gth; i++)
          {
          Console.WriteLi ne("\nName : {0}",
          myFieldInfo[i].Name);
          Console.WriteLi ne("Declaring Type : {0}",
          myFieldInfo[i].DeclaringType) ;
          Console.WriteLi ne("IsPublic : {0}",
          myFieldInfo[i].IsPublic);
          Console.WriteLi ne("MemberType : {0}",
          myFieldInfo[i].MemberType);
          Console.WriteLi ne("FieldType : {0}",
          myFieldInfo[i].FieldType);
          Console.WriteLi ne("IsFamily : {0}",
          myFieldInfo[i].IsFamily);
          }
          }
          }


          "Ruben" <MrPugh@gmx.dew rote in message
          news:1162922824 .099359.228600@ m73g2000cwd.goo glegroups.com.. .
          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
          >

          Comment

          • Tom Spink

            #6
            Re: member-name as string

            Ruben wrote:
            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.

            --
            Hope this helps,
            Tom Spink

            Google first, ask later.

            Comment

            • Ruben

              #7
              Re: member-name as string

              andrew queisser schrieb:
              What you're looking for are te "Reflection " classes in System.Reflecti on. 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

              Comment

              • andrew queisser

                #8
                Re: member-name as string

                "Ruben" <MrPugh@gmx.dew rote in message
                news:1162929483 .671284.258220@ i42g2000cwa.goo glegroups.com.. .
                andrew queisser schrieb:
                >What you're looking for are te "Reflection " classes in System.Reflecti on.
                >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




                Comment

                • Bruce Wood

                  #9
                  Re: member-name as string


                  Ruben wrote:
                  andrew queisser schrieb:
                  What you're looking for are te "Reflection " classes in System.Reflecti on. 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.

                  Comment

                  Working...