C#, C♯, C#相談室 Part46 at TECH
[2ch|▼Menu]
70:デフォルトの名無しさん
08/05/01 21:45:22
>>68
こういうユーティリティクラスを作っておくとか。
URLリンク(d.hatena.ne.jp)
public static class Reflector
{
  static PropertyInfo GetPropertyInfo(this Expression expression)
  {
    var findprop = default(Func<Expression, PropertyInfo>);
    findprop = expr =>
    {
      switch (expr.NodeType)
      {
        case ExpressionType.Lambda:
          return findprop((expr as LambdaExpression).Body);
        case ExpressionType.Quote:
          return findprop((expr as UnaryExpression).Operand);
        case ExpressionType.MemberAccess:
          return (expr as MemberExpression).Member as PropertyInfo;
        default:
          return null;
      }
    };
    return findprop(expression);
  }
  public static PropertyInfo Property<TArg, TResult>(Expression<Func<TArg, TResult>> expr)
  {
    return expr.GetPropertyInfo();
  }
}
使い方。
var defaultValue = Reflector.Property((MyComponent2 comp) => comp.MyProperty).GetCustomAttributes(typeof(DefaultValueAttribute), false)[0];
これでリファクタリングしても一緒に名前が変わるはず。


次ページ
続きを表示
1を表示
最新レス表示
スレッドの検索
類似スレ一覧
話題のニュース
おまかせリスト
▼オプションを表示
暇つぶし2ch

4185日前に更新/244 KB
担当:undef