반영-속성의 속성 이름 및 값 가져 오기
수업이 있는데 이름이라는 속성으로 Book이라고 부릅니다. 해당 속성과 관련된 속성이 있습니다.
public class Book
{
[Author("AuthorName")]
public string Name
{
get; private set;
}
}
내 주요 방법에서 리플렉션을 사용하고 있으며 각 속성에 대한 각 속성의 키 값 쌍을 가져 오려고합니다. 따라서이 예에서는 속성 이름으로 "Author"가 표시되고 속성 값으로 "AuthorName"이 표시됩니다.
질문 : Reflection을 사용하여 속성의 속성 이름과 값을 얻으려면 어떻게합니까?
인스턴스 typeof(Book).GetProperties()
배열을 얻는 데 사용 PropertyInfo
합니다. 그런 다음 GetCustomAttributes()
각각 PropertyInfo
에 Author
속성 유형 이 있는지 확인하십시오 . 그렇다면 속성 정보에서 속성 이름을 얻고 속성에서 속성 값을 얻을 수 있습니다.
특정 속성 유형이있는 특성의 유형을 스캔하고 사전에 데이터를 리턴하기 위해 다음 행을 따라야합니다 (유형을 루틴에 전달하여 더 동적으로 만들 수 있음).
public static Dictionary<string, string> GetAuthors()
{
Dictionary<string, string> _dict = new Dictionary<string, string>();
PropertyInfo[] props = typeof(Book).GetProperties();
foreach (PropertyInfo prop in props)
{
object[] attrs = prop.GetCustomAttributes(true);
foreach (object attr in attrs)
{
AuthorAttribute authAttr = attr as AuthorAttribute;
if (authAttr != null)
{
string propName = prop.Name;
string auth = authAttr.Name;
_dict.Add(propName, auth);
}
}
}
return _dict;
}
사전에서 속성의 모든 속성을 얻으려면 다음을 사용하십시오.
typeof(Book)
.GetProperty("Name")
.GetCustomAttributes(false)
.ToDictionary(a => a.GetType().Name, a => a);
에서 변경해야 false
하는 true
당신이 아니라 inheritted 특성을 포함 할 경우.
하나의 특정 속성 값만 원하면 디스플레이 속성과 같이 다음 코드를 사용할 수 있습니다.
var pInfo = typeof(Book).GetProperty("Name")
.GetCustomAttribute<DisplayAttribute>();
var name = pInfo.Name;
Generic Extension Property Attribute Helper를 작성하여 비슷한 문제를 해결했습니다.
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
public static class AttributeHelper
{
public static TValue GetPropertyAttributeValue<T, TOut, TAttribute, TValue>(
Expression<Func<T, TOut>> propertyExpression,
Func<TAttribute, TValue> valueSelector)
where TAttribute : Attribute
{
var expression = (MemberExpression) propertyExpression.Body;
var propertyInfo = (PropertyInfo) expression.Member;
var attr = propertyInfo.GetCustomAttributes(typeof(TAttribute), true).FirstOrDefault() as TAttribute;
return attr != null ? valueSelector(attr) : default(TValue);
}
}
용법:
var author = AttributeHelper.GetPropertyAttributeValue<Book, string, AuthorAttribute, string>(prop => prop.Name, attr => attr.Author);
// author = "AuthorName"
당신은 사용할 수 있습니다 GetCustomAttributesData()
및 GetCustomAttributes()
:
var attributeData = typeof(Book).GetProperty("Name").GetCustomAttributesData();
var attributes = typeof(Book).GetProperty("Name").GetCustomAttributes(false);
"하나의 매개 변수를 사용하는 특성의 경우 attribute-names 및 parameter-value를 나열하십시오"를 의미하는 경우 .NET 4.5에서 CustomAttributeData
API 를 통해 더 쉽습니다 .
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
public static class Program
{
static void Main()
{
PropertyInfo prop = typeof(Foo).GetProperty("Bar");
var vals = GetPropertyAttributes(prop);
// has: DisplayName = "abc", Browsable = false
}
public static Dictionary<string, object> GetPropertyAttributes(PropertyInfo property)
{
Dictionary<string, object> attribs = new Dictionary<string, object>();
// look for attributes that takes one constructor argument
foreach (CustomAttributeData attribData in property.GetCustomAttributesData())
{
if(attribData.ConstructorArguments.Count == 1)
{
string typeName = attribData.Constructor.DeclaringType.Name;
if (typeName.EndsWith("Attribute")) typeName = typeName.Substring(0, typeName.Length - 9);
attribs[typeName] = attribData.ConstructorArguments[0].Value;
}
}
return attribs;
}
}
class Foo
{
[DisplayName("abc")]
[Browsable(false)]
public string Bar { get; set; }
}
private static Dictionary<string, string> GetAuthors()
{
return typeof(Book).GetProperties()
.SelectMany(prop => prop.GetCustomAttributes())
.OfType<AuthorAttribute>()
.ToDictionary(attribute => attribute.Name, attribute => attribute.Name);
}
괴롭힘.
.NET 2.0을 계속 유지해야하거나 LINQ없이 수행하려는 경우 :
public static object GetAttribute(System.Reflection.MemberInfo mi, System.Type t)
{
object[] objs = mi.GetCustomAttributes(t, true);
if (objs == null || objs.Length < 1)
return null;
return objs[0];
}
public static T GetAttribute<T>(System.Reflection.MemberInfo mi)
{
return (T)GetAttribute(mi, typeof(T));
}
public delegate TResult GetValue_t<in T, out TResult>(T arg1);
public static TValue GetAttributValue<TAttribute, TValue>(System.Reflection.MemberInfo mi, GetValue_t<TAttribute, TValue> value) where TAttribute : System.Attribute
{
TAttribute[] objAtts = (TAttribute[])mi.GetCustomAttributes(typeof(TAttribute), true);
TAttribute att = (objAtts == null || objAtts.Length < 1) ? default(TAttribute) : objAtts[0];
// TAttribute att = (TAttribute)GetAttribute(mi, typeof(TAttribute));
if (att != null)
{
return value(att);
}
return default(TValue);
}
사용법 예 :
System.Reflection.FieldInfo fi = t.GetField("PrintBackground");
wkHtmlOptionNameAttribute att = GetAttribute<wkHtmlOptionNameAttribute>(fi);
string name = GetAttributValue<wkHtmlOptionNameAttribute, string>(fi, delegate(wkHtmlOptionNameAttribute a){ return a.Name;});
또는 단순히
string aname = GetAttributValue<wkHtmlOptionNameAttribute, string>(fi, a => a.Name );
public static class PropertyInfoExtensions
{
public static TValue GetAttributValue<TAttribute, TValue>(this PropertyInfo prop, Func<TAttribute, TValue> value) where TAttribute : Attribute
{
var att = prop.GetCustomAttributes(
typeof(TAttribute), true
).FirstOrDefault() as TAttribute;
if (att != null)
{
return value(att);
}
return default(TValue);
}
}
용법:
//get class properties with attribute [AuthorAttribute]
var props = typeof(Book).GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(AuthorAttribute)));
foreach (var prop in props)
{
string value = prop.GetAttributValue((AuthorAttribute a) => a.Name);
}
또는:
//get class properties with attribute [AuthorAttribute]
var props = typeof(Book).GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(AuthorAttribute)));
IList<string> values = props.Select(prop => prop.GetAttributValue((AuthorAttribute a) => a.Name)).Where(attr => attr != null).ToList();
위의 가장 많이 대답 된 답변은 확실히 작동하지만 경우에 따라 약간 다른 접근법을 사용하는 것이 좋습니다.
클래스에 항상 동일한 속성을 가진 여러 속성이 있고 이러한 속성을 사전으로 정렬하려면 다음과 같이하십시오.
var dict = typeof(Book).GetProperties().ToDictionary(p => p.Name, p => p.GetCustomAttributes(typeof(AuthorName), false).Select(a => (AuthorName)a).FirstOrDefault());
여전히 캐스트를 사용하지만 "AuthorName"유형의 사용자 정의 속성 만 가져 오므로 캐스트가 항상 작동합니다. 위의 속성이 여러 개인 경우 캐스트 예외가 발생합니다.
foreach (var p in model.GetType().GetProperties())
{
var valueOfDisplay =
p.GetCustomAttributesData()
.Any(a => a.AttributeType.Name == "DisplayNameAttribute") ?
p.GetCustomAttribute<DisplayNameAttribute>().DisplayName :
p.Name;
}
이 예에서는 값으로 표시 될 'DisplayName'이라는 필드가 있으므로 Author 대신 DisplayName을 사용했습니다.
MaxLength 또는 다른 속성을 얻는 데 사용할 수있는 정적 메서드는 다음과 같습니다.
using System;
using System.Linq;
using System.Reflection;
using System.ComponentModel.DataAnnotations;
using System.Linq.Expressions;
public static class AttributeHelpers {
public static Int32 GetMaxLength<T>(Expression<Func<T,string>> propertyExpression) {
return GetPropertyAttributeValue<T,string,MaxLengthAttribute,Int32>(propertyExpression,attr => attr.Length);
}
//Optional Extension method
public static Int32 GetMaxLength<T>(this T instance,Expression<Func<T,string>> propertyExpression) {
return GetMaxLength<T>(propertyExpression);
}
//Required generic method to get any property attribute from any class
public static TValue GetPropertyAttributeValue<T, TOut, TAttribute, TValue>(Expression<Func<T,TOut>> propertyExpression,Func<TAttribute,TValue> valueSelector) where TAttribute : Attribute {
var expression = (MemberExpression)propertyExpression.Body;
var propertyInfo = (PropertyInfo)expression.Member;
var attr = propertyInfo.GetCustomAttributes(typeof(TAttribute),true).FirstOrDefault() as TAttribute;
if (attr==null) {
throw new MissingMemberException(typeof(T).Name+"."+propertyInfo.Name,typeof(TAttribute).Name);
}
return valueSelector(attr);
}
}
정적 방법 사용 ...
var length = AttributeHelpers.GetMaxLength<Player>(x => x.PlayerName);
또는 인스턴스에서 선택적 확장 방법을 사용하여 ...
var player = new Player();
var length = player.GetMaxLength(x => x.PlayerName);
또는 다른 속성 (예 : StringLength)에 대해 전체 정적 메소드 사용 ...
var length = AttributeHelpers.GetPropertyAttributeValue<Player,string,StringLengthAttribute,Int32>(prop => prop.PlayerName,attr => attr.MaximumLength);
Mikael Engver의 답변에서 영감을 얻었습니다.
열거 형에서 속성을 얻으려면 다음을 사용하고 있습니다.
public enum ExceptionCodes
{
[ExceptionCode(1000)]
InternalError,
}
public static (int code, string message) Translate(ExceptionCodes code)
{
return code.GetType()
.GetField(Enum.GetName(typeof(ExceptionCodes), code))
.GetCustomAttributes(false).Where((attr) =>
{
return (attr is ExceptionCodeAttribute);
}).Select(customAttr =>
{
var attr = (customAttr as ExceptionCodeAttribute);
return (attr.Code, attr.FriendlyMessage);
}).FirstOrDefault();
}
// 사용
var _message = Translate(code);
이 코드를 넣을 적절한 장소를 찾으십시오.
다음과 같은 속성이 있다고 가정 해 봅시다.
[Display(Name = "Solar Radiation (Average)", ShortName = "SolarRadiationAvg")]
public int SolarRadiationAvgSensorId { get; set; }
그리고 ShortName 값을 얻고 싶습니다. 넌 할 수있어:
((DisplayAttribute)(typeof(SensorsModel).GetProperty(SolarRadiationAvgSensorId).GetCustomAttribute(typeof(DisplayAttribute)))).ShortName;
또는 일반적인 것으로 만들기 :
internal static string GetPropertyAttributeShortName(string propertyName)
{
return ((DisplayAttribute)(typeof(SensorsModel).GetProperty(propertyName).GetCustomAttribute(typeof(DisplayAttribute)))).ShortName;
}
참고 URL : https://stackoverflow.com/questions/6637679/reflection-get-attribute-name-and-value-on-property
'Programing' 카테고리의 다른 글
jQuery Datepicker onchange 이벤트 문제 (0) | 2020.04.21 |
---|---|
string.Equals ()와 == 연산자가 실제로 동일합니까? (0) | 2020.04.21 |
명령 줄을 통해 unittest.TestCase에서 단일 테스트 실행 (0) | 2020.04.21 |
CSS : before / : after pseudo-elements에서 이미지의 높이를 변경할 수 있습니까? (0) | 2020.04.21 |
배경색을 기준으로 글꼴 색 결정 (0) | 2020.04.21 |