• CSharp扩展方法应用之获取特性


             例如在Asp.net MVC Web Application中的,我们想快速了解某个Action上是否有某个Attribute. 那我们可以使用这样的扩展方法:

        
    /// <summary>
    /// Gets the method.
    /// </summary>
    /// <typeparam name="T">Type</typeparam>
    /// <param name="instance">The instance.</param>
    /// <param name="methodSelector">The method selector.</param>
    /// <returns>MethodInfo</returns>
    public static MethodInfo GetMethod<T>(this T instance, Expression<Func<T, object>> methodSelector)
    {
        // it is not work all method
        return ((MethodCallExpression)methodSelector.Body).Method;
    }
    
    /// <summary>
    /// Gets the method.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="instance">The instance.</param>
    /// <param name="methodSelector">The method selector.</param>
    /// <returns>MethodInfo</returns>
    public static MethodInfo GetMethod<T>(this T instance,
        Expression<Action<T>> methodSelector)
    {
        return ((MethodCallExpression)methodSelector.Body).Method;
    }
    
    /// <summary>
    /// Determines whether the specified member has attribute.
    /// </summary>
    /// <typeparam name="TAttribute">The type of the attribute.</typeparam>
    /// <param name="member">The member.</param>
    /// <returns>
    ///   <c>true</c> if the specified member has attribute; otherwise, <c>false</c>.
    /// </returns>
    public static bool HasAttribute<TAttribute>(
        this MemberInfo member)
        where TAttribute : Attribute
    {
        return GetAttributes<TAttribute>(member).Length > 0;
    }
    
    /// <summary>
    /// Gets the attributes.
    /// </summary>
    /// <typeparam name="TAttribute">The type of the attribute.</typeparam>
    /// <param name="member">The member.</param>
    /// <returns></returns>
    public static TAttribute[] GetAttributes<TAttribute>(
        this MemberInfo member)
        where TAttribute : Attribute
    {
        var attributes =
            member.GetCustomAttributes(typeof(TAttribute), true);
    
        return (TAttribute[])attributes;
    }



    如何使用,请看下面的代码,我们使用lambda表达式获取某个方法,然后获取其上面的Attribute:

    [Fact]
    public void GetHttpPostAttributeFromCreateAction()
    {
        // Arrange
        var controller = GetEmployeeController(new MemeoryEmployeeBoService());
        
        //Act
        bool hasPostAttribute =controller.GetMethod(e => e.Create(new Employee()))
            .HasAttribute<HttpPostAttribute>();
    
        // Assert
        Assert.True(hasPostAttribute);
    }


    希望对您开发有帮助。


    您可能感兴趣的文章:

    用扩展方法来为Enum类型加入业务逻辑
    用扩展方法来扩展IDataReader接口
    为IEnumerable<T>增加Combine的扩展方法
    用扩展方法生成分割符字符串
    Orderby的扩展方法



    作者:Petter Liu
    出处:http://www.cnblogs.com/wintersun/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    该文章也同时发布在我的独立博客中-Petter Liu Blog

  • 相关阅读:
    asp.net中的Application概述
    Android布局
    Content Provider
    Service
    进程和线程Processes and Threads
    Android模拟器
    Fragment
    Ui Event
    Loader
    sqlite3命令
  • 原文地址:https://www.cnblogs.com/wintersun/p/2367832.html
Copyright © 2020-2023  润新知