Accessing C# property name or attributes

前端 未结 7 2132
花落未央
花落未央 2021-01-31 21:39

I would like to automatically generate SQL statements from a class instance. The method should look like Update(object[] Properties, object PrimaryKeyProperty). The method is pa

7条回答
  •  日久生厌
    2021-01-31 22:03

    You can get the name (I assume that's what you meant by ID) of a property using PropertyInfo.Name. Just loop through the PropertyInfo[] returned from typeof(className).GetProperties()

    foreach (PropertyInfo info in typeof(MyClass).GetProperties())
    {
        string name = info.Name;
        // use name here
    }
    

提交回复
热议问题