728x90
반응형
728x170
▶ CustomAttribute.cs
namespace TestProject;
/// <summary>
/// 커스텀 어트리뷰트
/// </summary>
public class CustomAttribute : Attribute
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 텍스트 - Text
/// <summary>
/// 텍스트
/// </summary>
public string Text { get; protected set; }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - CustomAttribute(text)
/// <summary>
/// 생성자
/// </summary>
/// <param name="text">텍스트</param>
public CustomAttribute(string text)
{
Text = text;
}
#endregion
}
728x90
▶ Product.cs
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
}
300x250
▶ Program.cs
using System.Linq.Expressions;
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");
Expression<Func<Product, string>> expression = p => p.Name;
MemberExpression memberExpression = expression.Body as MemberExpression;
object[] customAttributeArray = memberExpression.Member.GetCustomAttributes(typeof(CustomAttribute), false);
if(customAttributeArray.Length > 0)
{
Console.WriteLine((customAttributeArray[0] as CustomAttribute).Text);
}
}
#endregion
}
728x90
반응형
그리드형(광고전용)
댓글을 달아 주세요