저는 주어진 클래스에 대해 속성이 null인지 확인하는 유일한 방법을 작성했습니다. 속성이 null이면 속성의 새 인스턴스를 만듭니다. 실제로 값을 설정하고있어 어디 부분에 갇히지 해요 :C# Reflection, null 속성의 새 인스턴스 설정
public static void CheckIfPropertyIsNull<TEntity>(SomeBusinessEntity someBusinessEntity) where TEntity : new()
{
var properties = typeof(SomeBusinessEntity).GetProperties();
foreach (PropertyInfo propertyInfo in properties)
{
Type currentType = propertyInfo.PropertyType;
if (currentType == typeof(TEntity))
{
var propertyData = propertyInfo.GetValue(someBusinessEntity, null);
if (propertyData == null)
{
object instance = Activator.CreateInstance(typeof(TEntity));
// And then?
//propertyInfo.SetValue(null, instance);
return;
}
}
}
}
내가 SetValue는() 메소드하지만 행운을 사용하려고합니다.
당신이 제공 한 모든 타입에 매개 변수없는 생성자가 있다는 것을 제네릭 시스템에 요청하는 번거 로움이 있습니다 - Activator.CreateInstance (...)가 아닌'new TEntity()'가 아닌 이유는 무엇입니까? ? –
나는 약간의 생각 과정을 가지고 있었고 단지 코드로 주위를 두들 켰다. 전에 새로운 TEntity()를 사용하고있었습니다. –