■ Attribute 클래스의 IsDefined 정적 메소드를 사용해 객체 속성의 어트리뷰트 설정 여부를 구하는 방법을 보여준다.
▶ CustomAttribute.cs
1 2 3 4 5 6 7 8 9 10 |
namespace TestProject; /// <summary> /// 커스텀 어트리뷰트 /// </summary> public class CustomAttribute : Attribute { } |
▶ Product.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
namespace TestProject; /// <summary> /// 제품 /// </summary> public class Product { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 제품 코드 - ProductCode /// <summary> /// 제품 코드 /// </summary> public string ProductCode { get; set; } #endregion #region 명칭 - Name /// <summary> /// 명칭 /// </summary> [Custom] public string Name { get; set; } #endregion #region 가격 - Price /// <summary> /// 가격 /// </summary> public decimal Price { get; set; } #endregion } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System.Reflection; namespace TestProject; /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Type sourceType = typeof(Product); PropertyInfo propertyInfo = sourceType.GetProperty("Name"); bool hasAttribute = Attribute.IsDefined(propertyInfo, typeof(CustomAttribute)); Console.WriteLine(hasAttribute); } #endregion } |