Accessing C# property name or attributes

前端 未结 7 2216
花落未央
花落未央 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:55

    Not 100% sure if this will get you what you're looking for, this will fetch all properties with [Column] attribute inside your class: In the datacontext I have:

     public ReadOnlyCollection ColumnNames( )
        {
            return this.Mapping.MappingSource.GetModel(typeof(DataContext)).GetMetaType(typeof(TEntity)).DataMembers;
        }
    

    Fetching the table column-names that are properties inside the class:

           MyDataContext db = GetDataContext(); 
           var allColumnPropertyNames =   db.ColumnNames().Where(n => n.Member.GetCustomAttributes(typeof(System.Data.Linq.Mapping.ColumnAttribute), false).FirstOrDefault() != null).Select(n => n.Name);
    

提交回复
热议问题