如何在C#

时间:2015-09-11 19:52:15

标签: c# reflection properties set

我正在尝试设置属性类的值。

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;

    }

}

我在各种类的许多属性上调用此方法。这个问题是我有一个类如下的类:

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

    public string Number { get; set; }

    public IComplexObject Object { get; set; }
}

Name和Number设置得很好但是如果我尝试设置一个继承自Object上的IComplexObject的类的实例,则没有错误它只是保持为null。

有没有一种简单的方法可以将类的实例设置为属性?

例如,如果我将prop作为{IComplexObject Object}传递,而将对象传递为

var object = (object) new ComplexObject
{
    prop1 = "Property"
    prop2 = "OtherProperty"
}

最后没有错误,但Object仍然为null,并且未设置为ComplexObject的实例。它必须是通用的,所以我可以传入任何类,并且属性将被更新。

2 个答案:

答案 0 :(得分:0)

你的代码是不必要的复杂,但它完全正常。我已经把你的代码扩展为一个完整的例子。

输出就是这个。

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

这表明Object属性与其他属性没有区别。 “复杂对象”不是一个真正意味着.net的术语。此外,您对async的使用似乎没有必要且令人困惑。

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";
}

答案 1 :(得分:0)

此示例有效。我把它作为参考 我已将其更改为等待任务并将返回值提取到结果变量,以便您可以看到它返回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());
    }
}