일반 속성을 사용하는 클래스가 있습니다. 예를 들면 :C# 일반 속성으로 리플렉션 사용
class Person
{
public MyGenericProperty<string> Field1
{
get { return field1; }
set { field1 = value; }
}
private MyGenericProperty<string> field1= new MyInheritedGenericProperty<string>("Alan1");
}
내가 다른 클래스에서 반사와이 클래스를 사용하려면 그리고 난 내가 가진 그
public void DoSomethingWithProperty(object sourceobject)
{
foreach (var aProperty in sourceobject.GetType().GetProperties())
{
*if(aProperty.PropertyType == typeof(MyGenericProperty<>))*
{
*var obj = (MyGenericProperty<>)aProperty.GetValue(sourceobject, null);*
}
}
return null;
}
같은 방법이 두 가지 문제 유형 검사를 할 수있는 방법
1 일반 재산. 이 예제 코드에서는 if(aProperty.PropertyType == typeof(MyGenericProperty<>))
이 작동하지 않습니다. MyGenericProperty의
2 T는 클래스가 될 수있는 방법과 도움에 대한
var obj = (MyGenericProperty<>)aProperty.GetValue(sourceobject, null);
감사로 반사에 의해 알고있는 T 않고 MyGenericProperty 클래스를 캐스팅 할 수 있습니다.