-2

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.

nastassiar
  • 1,545
  • 2
  • 24
  • 43
  • 2
    Assuming the example doesn't generate any exceptions when setting the `Object` property, then the set should have been successful. Perhaps the equality comparison for your `IComplexObject`s is what's misbehaving? Step-through debugging can be helpful here. If it is the equality, then you could try replacing the comparison with `ReferenceEquals` for starters – Sabre Sep 11 '15 at 19:58
  • 2
    http://stackoverflow.com/questions/10283206/c-sharp-setting-getting-the-class-properties-by-string-name – MethodMan Sep 11 '15 at 19:59
  • 1
    Why is your update method async? It doesn't seem to use a task. – Travis J Sep 11 '15 at 20:01
  • 2
    Post a short but complete, resproducable code... – Eser Sep 11 '15 at 20:02
  • Updated the code and question to be more clear. My issue is SetValue isn't setting Object it just remains null without any exceptions. – nastassiar Sep 11 '15 at 20:38

2 Answers2

0

Your code is needlessly complicated, but it works completely fine. I've taken your code and expanded it into a complete example that runs.

The output is this.

Name: asdf, Number: A1B2, Object: hello this is complexobject
It was not null

This shows that the Object property is no different from any of the others. "Complex object" is not a term that really means anything in .net. Additionally your use of async seems unnecessary and confusing.

async void Main() {
    Test t = new Test();
    Type type = typeof(Test);

    await t.TryUpdate(type.GetProperty(nameof(t.Name)), "asdf");
    await t.TryUpdate(type.GetProperty(nameof(t.Number)), "A1B2");
    await t.TryUpdate(type.GetProperty(nameof(t.Object)), (object)new ComplexObject());

    Console.WriteLine(t.ToString());

    PropertyInfo prop = type.GetProperty(nameof(t.Object));

    if (prop.GetValue(t) == null) {
        Console.WriteLine("It was null");
    } else {
        Console.WriteLine("It was not null");
    }
}

public class Test {
    public string Name { get; set; }
    public string Number { get; set; }
    public IComplexObject Object { get; set; }

    // Added for the purpose if showing contents
    public override string ToString() => $"Name: {Name}, Number: {Number}, Object: {Object}";

    // Why is this async?  Your code does not await
    public async Task<bool> TryUpdate(PropertyInfo prop, object value) {
        await Task.Delay(0); // Added to satisfy async
        try {
            prop.SetValue(this, value);

            // If not set probably a complex type
            if (value != prop.GetValue(this)) {
                //... Don't know what to do
            }

            return true;
        }
        catch {
            return false;
        }
    }

}

public interface IComplexObject { }
class ComplexObject : IComplexObject {
    public override string ToString() => "hello this is complexobject";
}
recursive
  • 83,943
  • 34
  • 151
  • 241
  • Updated the code and question to be more clear. My issue is SetValue isn't setting Object it just remains null without any exceptions. What I am looking for is what to put into the if statement to set Object since set Value doesn't work. – nastassiar Sep 11 '15 at 20:38
  • Your question is still not clear. The code in my example shows that setting `Object` *does* work. The property is set to the instance of `ComplexObject`, and is no longer null. – recursive Sep 11 '15 at 20:43
  • Then I'm just confused this doesn't work for me. If it was : if (prop.GetValue(this) == null) { return false } which would mean that the value wasn't set mine returns false. – nastassiar Sep 11 '15 at 20:46
  • I added some code to my example that does `if (prop.GetValue(this) == null)`, and it correctly shows that the value was set and is no longer null. The code works, and I can't reproduce the problem based on your code. In fact, the code in my example shows the opposite. Please include enough code to reproduce the problem. – recursive Sep 11 '15 at 21:26
0

This example works. I've put it for a reference.
I've changed it to await the task and extract the return value to the result variable so you could see that it returns true.

public class Test
{
    public string Name { get; set; }

    public string Number { get; set; }

    public IComplexObject Object { get; set; }

    public async Task<bool> TryUpdate(PropertyInfo prop, object value) {
        try {
            prop.SetValue(this, value);
            return true;
        }
        catch (Exception) {

        }

        return false;
    }

}

public class ComplexObject : IComplexObject
{
}

public interface IComplexObject
{

}
static class  Program
{

    static void Main() {
        TestMethod();
    }

    static async void TestMethod() {
        var test = new Test();
        var result = await test.TryUpdate(test.GetType().GetProperty("Object"), new ComplexObject());
    }
}
Sagi
  • 8,972
  • 3
  • 33
  • 41