zoukankan      html  css  js  c++  java
  • .NET复习笔记-反射至获取类型特性

        /// <summary>
        /// 定义一个特性类
        /// </summary>
        class HL7MessageAttribute : Attribute
        {
            public string hl7MsgType { get; set; }
            public string ackMsgType { get; set; }
        }
        /// <summary>
        /// 使用自定义特性
        /// </summary>
        [HL7Message(ackMsgType ="ACKA01", hl7MsgType ="ADTA01")]
        class PatientManager
        {
            private object dataLayer = null;
            public string GetPatientName(int patientId)
            {
                return string.Empty;
            }
        }
    private void showCustomAttributeInfo()
    {
    
        PatientManager patientManager = new PatientManager();
    
        Type patientMngType = patientManager.GetType();
        Type patientMngAttrType = typeof(HL7MessageAttribute);
        HL7MessageAttribute attribute = null;
        if (patientMngType.IsDefined(patientMngAttrType, true))
        {
            attribute = patientMngType.GetCustomAttributes(patientMngAttrType, true).FirstOrDefault() as HL7MessageAttribute;
        }
        Debug.WriteLine($"Attribute name:{attribute.GetType().Name}");
        Debug.WriteLine($"HL7MsgType:{attribute.hl7MsgType}, AckMsgType:{attribute.ackMsgType}");
    }
  • 相关阅读:
    Python Package(转)
    22. 分数化小数 decimal
    21. 子序列的和 subsequence
    20. 倒三角形 triangle
    19. 韩信点兵hanxin
    18. 水仙花数daffodil
    17. 数据统计2
    16. 数据统计
    15. 阶乘之和
    14. 近似计算
  • 原文地址:https://www.cnblogs.com/ironcrow/p/10999442.html
Copyright © 2011-2022 走看看