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

后端 未结 7 677
春和景丽
春和景丽 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: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();
    

提交回复
热议问题