리플렉션을 사용하여 부모 객체 내부의 childObject 참조를 제거해야합니다.리플렉션을 사용하여 parentObject 내의 childObject를 C#
var property = parentObject.GetType().GetProperty("ChildObject"); // get the property
property.SetValue(parentObject, null, null);
그것은 나를 왜 의문 않습니다 가정
public class ParentObject{
public object ChildObject {get;set;}
}
/* Implementation */
ParentObject parentObject = new ParentObject();
object childObject = new Object();
//I set this using reflection (PropertyInfo SetValue operation)
parentObject.ChildObject = childObject ;
...
//I want to remove the reference to the child object using reflection
RemoveObjectUsingReflection(parentObject, childObject);
Assert.IsNull(parentObject.ChildObject); //returns true
function RemoveObjectUsingReflection(object parentObject, object childObject)
{
//Appreciate your help here
}
현재 RemoveObjectUsingReflection 메서드에 다른 매개 변수 (parentObject의 childObject 속성 이름)를 추가하고 있습니다. –