/// <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}"); }