How do I get the name of a property from a property in C# (2.0)

后端 未结 7 672
春和景丽
春和景丽 2020-12-10 17:24

I know I could have an attribute but that\'s more work than I want to go to... and not general enough.

I want to do something like

class Whotsit
{
          


        
相关标签:
7条回答
  • 2020-12-10 17:32

    The GetProperties on the Type class will give you the list of properties on that type.

    Type t = whotsit.GetType();
    PropertyInfo[] pis = t.GetProperties();
    
    0 讨论(0)
  • 2020-12-10 17:36

    Kpollack, you said in an earlier comment:

    which still won't give me the ability to get the name of a property from an instance of it.

    This leads me to believe that you somehow have a reference to a property. How did you get this reference? What is its type? Could you provide a code sample? If it's a PropertyInfo object, you already have what you need; since this doesn't appear to be the case, we're dealing with something else, and I'd be very interested to see what it is that you do have to work with.

    P.S. Forgive me for seeming obtuse: it's early, I haven't had enough coffee, and I don't have my IDE in front of me. :-/

    0 讨论(0)
  • 2020-12-10 17:45

    What you are trying to do is not possible. Using reflection you can:

    • Get a property, field or method's details from it's string name.
    • Get a list of all properties, fields or methods for a given type.

    Your GetName method, when called, is passed a string. The GetMethod will know about the string but retains no source property meta data.

    Out of interest, why are you trying to do this?

    0 讨论(0)
  • 2020-12-10 17:53

    FYI, I tried to serialize it to see if, by chance, that contains the property name, but no luck.

    Non-working code below:

    Whotsit w = new Whotsit();
    XmlSerializer xs = new XmlSerializer(w.TestProp.GetType());
    TextWriter sw = new StreamWriter(@"c:\TestProp.xml");
    xs.Serialize(sw, w.TestProp);
    sw.Close();
    
    0 讨论(0)
  • 2020-12-10 17:55

    I don't think it's possible, the only way to do this is to iterate over properties:

    class TestClass
    {
        private string _field;
    
        public string MyProperty
        {
            get { return _field; }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            TestClass test = new TestClass();
            PropertyInfo[] info = test.GetType().GetProperties();
            foreach(PropertyInfo i in info)
                Console.WriteLine(i.Name);
            Console.Read();
        }
    }
    
    0 讨论(0)
  • 2020-12-10 17:56

    No, there's nothing to do this. The expression whotsit.TestProp will evaluate the property. What you want is the mythical "infoof" operator:

    // I wish...
    MemberInfo member = infoof(whotsit.TestProp);
    

    As it is, you can only use reflection to get the property by name - not from code. (Or get all the properties, of course. It still doesn't help you with your sample though.)

    One alternative is to use an expression tree:

    Expression<Func<string>> = () => whotsit.TestProp;
    

    then examine the expression tree to get the property.

    If none of this helps, perhaps you could tell us more about why you want this functionality?

    0 讨论(0)
提交回复
热议问题