zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    shell 案例
    linux 软链接和硬链接区别
    mac安装使用nginx
    Leetcode SQL_#176_第二高的薪水
    南邮ctf web题记录(上)
    CTFHub Web技能树
    XCTF web 新手练习区
    诊断工具--arthas使用教程
    prometheus--监控工具
    无状态状态机--cola stateMachine
  • 原文地址:https://www.cnblogs.com/wintersun/p/2367832.html
Copyright © 2011-2022 走看看