代码之家  ›  专栏  ›  技术社区  ›  David

属性/方法/成员是否有C#等价的typeof?

  •  18
  • David  · 技术社区  · 15 年前

    Type 元数据可以通过多种方式获得。其中两个是:

    var typeInfo = Type.GetType("MyClass")

    var typeInfo = typeof(MyClass)

    第二种方法的优点是,编译器会捕捉到输入错误,IDE可以理解我所说的内容(允许重构等功能在不破坏代码的情况下工作)

    对于元数据和反射,是否存在一种等效的方法来强引用成员/属性/方法?我可以替换:

    var propertyInfo = typeof(MyClass).GetProperty("MyProperty")

    比如:

    var propertyInfo = property(MyClass.MyProperty)

    4 回复  |  直到 15 年前
        1
  •  19
  •   Jon Skeet    15 年前

    不,不幸的是没有。有人讨论过,甚至命名为: infoof (发音为“in foof”表示喜剧价值)但它没有被实现。。。然而埃里克·利珀特有一个 blog post about it .

        2
  •  11
  •   Alexander Mavrinsky    13 年前

    我刚刚使用syst.Linq.Expressions实现了一个等价的构造“propertyof”“methodof”“fieldof”

    var mi = typeof (string).GetMethod("Concat", new[] {typeof (object), typeof (object)});
    

    您可以使用:

    var mi = ReflectionHelper.MethodOf(() => string.Concat(new object(), new object()));
    

    我们为什么需要这个?因为现在我们可以安全地重构方法,我们使用via反射

    助手类的列表(您可能需要在方法中添加一些有用的异常):

    /// <summary>
    /// Represents a set of helpers for .net reflection
    ///  </summary>
    public static class ReflectionHelper
    {
        #region Public methods
    
        /// <summary>
        /// Gets a MethodInfo object from specified expression
        ///  </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="methodExpression"></param>
        /// <returns></returns>
        public static MethodInfo MethodOf<TResult>(Expression<Func<TResult>> methodExpression)
        {
            return ((MethodCallExpression)methodExpression.Body).Method;
        }
    
        /// <summary>
        /// Gets a MethodInfo object from specified expression
        ///  </summary>
        /// <param name="methodExpression"></param>
        /// <returns></returns>
        public static MethodInfo MethodOf(Expression<Action> methodExpression)
        {
            return ((MethodCallExpression)methodExpression.Body).Method;
        }
    
        /// <summary>
        /// Gets a MethodInfo object from specified expression
        ///  </summary>
        /// <param name="methodExpression"></param>
        /// <returns></returns>
        public static MethodInfo MethodOf<TInstance, TResult>(Expression<Func<TInstance, TResult>> methodExpression)
        {
            return ((MethodCallExpression)methodExpression.Body).Method;
        }
    
        /// <summary>
        /// Gets a MethodInfo object from specified expression
        ///  </summary>
        /// <param name="methodExpression"></param>
        /// <returns></returns>
        public static MethodInfo MethodOf<TInstance>(Expression<Action<TInstance>> methodExpression)
        {
            return ((MethodCallExpression)methodExpression.Body).Method;
        }
    
        /// <summary>
        /// Gets a PropertyInfo object from specified expression
        ///  </summary>
        /// <param name="propertyGetExpression"></param>
        /// <returns></returns>
        public static PropertyInfo PropertyOf<TProperty>(Expression<Func<TProperty>> propertyGetExpression)
        {
            return ((MemberExpression)propertyGetExpression.Body).Member as PropertyInfo;
        }
    
        /// <summary>
        /// Gets a PropertyInfo object from specified expression
        ///  </summary>
        /// <param name="propertyGetExpression"></param>
        /// <returns></returns>
        public static PropertyInfo PropertyOf<TInstance, TProperty>(Expression<Func<TInstance, TProperty>> propertyGetExpression)
        {
            return ((MemberExpression)propertyGetExpression.Body).Member as PropertyInfo;
        }
    
        /// <summary>
        /// Gets a FieldInfo object from specified expression
        ///  </summary>
        /// <param name="fieldAccessExpression"></param>
        /// <returns></returns>
        public static FieldInfo FieldsOf<TProperty>(Expression<Func<TProperty>> fieldAccessExpression)
        {
            return ((MemberExpression)fieldAccessExpression.Body).Member as FieldInfo;
        }
    
        //TODO: ConstructorOf(...)
    
        #endregion //Public methods
    }
    

    据我所知,我们不能使用相同的方法获取参数信息或事件信息

    Jb Evain介绍了另一种方法,请参见: http://evain.net/blog/articles/2010/05/05/parameterof-propertyof-methodof?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+jbevain+%28Jb+in+a+nutshell%29

        3
  •  8
  •   Richard Szalay    8 年前

    在c#6中仍然没有 infoof nameof

    var propertyInfo = typeof(MyClass).GetProperty(nameof(MyClass.MyProperty))
    

    它当然不是更简洁,但至少它是重构友好的。

        4
  •  2
  •   4b0 Agit    4 年前

    现在在c#,我们有 nameof()

    裁判 link

        5
  •  0
  •   Andrew Bezzub    15 年前

    不,c#中没有这样的语法。