C#, C♯, C#相談室 Part46
at TECH
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