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; }
    
        }
    
    }
  • 相关阅读:
    CSS快速入门
    Kafka (一) 核心概念
    软件工程模型
    函数式编程
    spark计算操作整理
    HBase 文件合并
    HBase 数据存储结构
    目的论浅谈
    PHP8的注解
    JS的移入移除
  • 原文地址:https://www.cnblogs.com/grj001/p/12224622.html
Copyright © 2011-2022 走看看