如何在c#中设置一个类的属性值

本文关键字:一个 属性 设置 | 更新日期: 2023-09-27 18:12:27

我正在尝试设置一个属性的值,这个属性是一个类。

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

名称和数字设置得很好,但如果我尝试设置一个类的实例,从对象上继承iccomplexobject,没有错误,它只是保持为空。

是否有一个简单的方法来设置一个类的实例作为属性?

例如,如果我传入prop为{iccomplexobject Object}而传入Object为

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

最后没有错误,但是Object仍然为空并且没有设置为ComplexObject的实例。它需要是通用的,所以我可以在任何类传递和属性将被更新。

如何在c#中设置一个类的属性值

这个例子可以工作。我把它放在这里供参考。
我将其更改为等待任务并将返回值提取到结果变量,以便您可以看到它返回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());
    }
}

你的代码是不必要的复杂,但它工作得很好。我把你的代码扩展成一个完整的例子,可以运行。

输出如下:

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