خرید بک لینک

Vote count: 0

Given the following code

[Table("Bar")]
public class Foo {
  [Column("BarID")]
  public int Id { get; set; }
}
public static class MyExtensions {
  public static string TableName(this Type type) {
    var attrib = type.GetCustomAttribute<TableAttribute>(false);
    retu attrib?.Name ?? type.Name;
  }
  public static string ColumnName<TType, TMember>(this Type t, Expression<Func<TType, TMember>> accessor) {
    var member = accessor.Body as MemberExpression;
    if (member != null) {
      var field = member.Member;
      var attrib = field.GetCustomAttribute<ColumnAttribute>();
      retu attrib?.Name ?? field.Name;
    }
    retu null; //how can I get the member name from the accessor?
  }
}

I can then write code like typeof(Foo).TableName(); to get the table name

For the column name I can use typeof(Foo).ColumnName((Foo f) => f.Id)

How can I get the ColumnName extension method to infer TType in the accessor expression so that I can simply code as type(Foo).ColumnName(f => f.Id)

asked 32 secs ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 3 مرداد 1395 ساعت: 2:40

صفحه بندی