4
A
답변
8
이 중첩 된 형태의 탐사 사용하여 수행 할 수 있습니다 :
public IEnumerable<PropertyInfo> GetRequiredProperties()
{
var nestedTypes = typeof(Address).GetNestedTypes(BindingFlags.NonPublic);
var nestedType = nestedTypes.First(); // It can be done for all types
var requiredProperties =
nestedType.GetProperties()
.Where(property =>
property.IsDefined(typeof(RequiredAttribute), false));
return requiredProperties;
}
사용 예 : 엘리사의 솔루션 적은 다음 우아하지만
[Test]
public void Example()
{
var requiredProperties = GetRequiredProperties();
var propertiesNames = requiredProperties.Select(property => property.Name);
Assert.That(propertiesNames, Is.EquivalentTo(new[] { "Address1", "Zip" }));
}
0
을, 그러나 또한 작동합니다 :)
귀하의 속성 :
(210)일부 클래스 :
class Class1
{
[Required]
public string Address1 { get; set; }
public string Address2 { get; set; }
[Required]
public string Address3 { get; set; }
}
사용법 :
Class1 c = new Class1();
RequiredAttribute ra = new RequiredAttribute();
Type class1Type = c.GetType();
PropertyInfo[] propInfoList = class1Type.GetProperties();
foreach (PropertyInfo p in propInfoList)
{
object[] a = p.GetCustomAttributes(true);
foreach (object o in a)
{
if (o.GetType().Equals(ra.GetType()))
{
richTextBox1.AppendText(p.Name + " ");
}
}
}
0
여기 내 솔루션입니다 usinjg AssociatedMetadataTypeTypeDescriptionProvider
:
var entity = CreateAddress();
var type = entity.GetType();
var metadata = (MetadataTypeAttribute)type.GetCustomAttributes(typeof(MetadataTypeAttribute), true).FirstOrDefault();
var properties = new AssociatedMetadataTypeTypeDescriptionProvider(type, metadata.MetadataClassType).GetTypeDescriptor(type).GetProperties();
bool hasAttribute = HasMetadataPropertyAttribute(properties, "Name", typeof(RequiredAttribute));
private static bool HasMetadataPropertyAttribute(PropertyDescriptorCollection properties, string name, Type attributeType)
{
var property = properties[name];
if (property == null)
return false;
var hasAttribute = proeprty.Attributes.Cast<object>().Any(a => a.GetType() == attributeType);
return hasAttribute;
}
관련 문제
- 1. 클래스 경로 및 반영
- 2. Java 반영 서브 클래스
- 3. 친구 클래스
- 4. DataAnnotation이 ErrorMessage를 표시하지 않습니다.
- 5. 친구 클래스 정보를 얻으시겠습니까?
- 6. Objective-C의 친구 클래스
- 7. 앞으로 클래스 친구 선언
- 8. C++ 친구 클래스
- 9. 코드의 반영
- 10. 클래스 아키텍처, 친구 허용하지 않음
- 11. C++ 친구 클래스 표준 : : 벡터
- 12. 친구
- 13. 스칼라 반영
- 14. ExpandoObject에 반영
- 15. 중첩 네임 스페이스 반영
- 16. HTML의 .NET 반영/추상화?
- 17. Java에서 메서드 동작 반영
- 18. 자바 인터페이스 반영 대안
- 19. 친구 클래스 및 접근 자 섹션의 정의
- 20. SQL Server : 친구 클래스 "facebook like"
- 21. 친구 클래스 asp.net MVC에서보기로 리디렉션하는 방법은 무엇입니까?
- 22. C# 친구 클래스 및 OOP 구성
- 23. C++의 사용되지 않는 친구 클래스
- 24. 친구, 친구, 친구 등을위한 SQL 테이블
- 25. 온라인 친구 친구 FBML
- 26. PHP : 친구 클래스 및 ungreedy 호출자 함수/클래스
- 27. C#의 항아리 분석/반영
- 28. MongoDB에서 친구 관계의 친구 모델링
- 29. 친구 (foaf) 그래프의 친구 시각화
- 30. C# 속성 PropertyType 반영
니스! 고마워. –
이것이 작동하는 동안 'buddy'(메타 데이터) 클래스의 관계가 속성을 통해 정의 된대로 잘못 가정됩니다. 다시 말해, 이상적인 해결책은'MetadataTypeAttribute'가'GetNestedTypes()'가 아닌 버디 유형을 얻기 위해 루트 유형을 검사하는 것입니다. 이 메소드는 중첩되지 않는 한 그러한 모든 유형의 클래스에 유효하지 않습니다. 또한 중첩 된 다른 클래스의 메타 데이터 클래스에 정의되지 않은 속성에 대해서도'RequiredAttribute'를 반환합니다 (존재하는 경우) – JoeBrockhaus