I am trying to set the value of a property that is a class.
protected bool TryUpdate(PropertyInfo prop, object value)
{
try
{
prop.SetValue(this, value);
// If not set probably a complex type
if (value != prop.GetValue(this))
{
//... Don't know what to do
}
// If still not set update failed
if (value != prop.GetValue(this))
{
return false;
}
return true;
}
}
I'm calling this method on a number of properties from a variety of classes. This issue is when I have a class like the following:
public class Test
{
public string Name { get; set; }
public string Number { get; set; }
public IComplexObject Object { get; set; }
}
Name and Number are set just fine, but if I try and set an instance of a class that inherits from IComplexObject on Object there is no errors it just remains null.
Is there an easy way to set an instance of a class as property?
So for example if I pass in prop as {IComplexObject Object} and object as
var object = (object) new ComplexObject
{
prop1 = "Property"
prop2 = "OtherProperty"
}
At the end there are no errors but Object remains null and is not set to the instance of ComplexObject. It needs to be generic so I can pass in any class and the property will be updated.