zoukankan      html  css  js  c++  java
  • C# 特性参数(注解属性加在参数前面)

    C# 特性参数(注解属性加在参数前面)

    特性参数

    webapi 框架里有很多特性参数,为了解除一些新人的疑惑,写个小例子分享下。
    
    
      class Program
    {
        static void Main(string[] args)
        {
            var message = new MessageData {
    
                Header="header...",
                Body="body....",
                Footer="footer...",
            };
    
            Type objT = typeof(Program);
            Type fromBodyT = typeof(FromBodyAttribute);
            MethodInfo method = objT.GetMethod("Test");
    
            ParameterInfo[] paramsInfo = method.GetParameters();
            var parameters= new List<object>(paramsInfo.Length);
            foreach (ParameterInfo parameterInfo in paramsInfo)
            {
                var parameter = new object();
                if (parameterInfo.CustomAttributes.Any(i => i.AttributeType == fromBodyT))
                    parameter = message.Body;
                parameters.Add(parameter);
            }
    
    
            object result = method.Invoke(null, parameters.ToArray());
            Console.WriteLine(result);
    
    
        }
        public class FromBodyAttribute : Attribute
        {
        }
        public static string Test([FromBody] string body)
        {
            return body;
        }
        class MessageData
        {
    
            public string Body { get; set; }
            public string Header { get; set; }
            public string Footer { get; set; }
    
        }
    
    }
  • 相关阅读:
    扩展卢卡斯定理
    扩展中国剩余定理
    扩展欧拉定理
    拓展BSGS
    删边最短路
    树 上 差分
    P4568 JLOI 飞行路线 分层最短路板子
    最短路相关
    P3758 TJOI2017 可乐
    bzoj4173 数学
  • 原文地址:https://www.cnblogs.com/grj001/p/12224622.html
Copyright © 2011-2022 走看看