Accessing C# property name or attributes

前端 未结 7 2126
花落未央
花落未央 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 21:47

    Let's say (from the first sample, method update of a class MyClass):

    public class MyClass {
    
    public int iMyStatusProperty { get; set; }
    public int iMyKey { get; set; }
    
    public int UpdateStatusProperty(int iValue){
    this.iMyStatusProperty = iValue;
    return _Update( new[iMyStatusProperty ], iMyKey); // this should generate SQL: "UPDATE MyClass set iMyStatusProperty = {iMyStatusProperty} where iMyKey = {iMyKey}"
    }
    

    {iMyStatusProperty} and {iMyKey} are property values of a class instance.

    So, the problem is how to get property name (reflection) from a property without using names of properties as strings (to avoid field name typos).

提交回复
热议问题