[英]How to set value of a property that is a class in C#
我正在尝试设置作为类的属性的值。
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 继承的类的实例,则没有错误,它只是保持为空。
有没有一种简单的方法可以将类的实例设置为属性?
因此,例如,如果我传入 prop 作为 {IComplexObject Object} 和 object 作为
var object = (object) new ComplexObject
{
prop1 = "Property"
prop2 = "OtherProperty"
}
最后没有错误,但 Object 保持 null 并且没有设置为 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";
}
这个例子有效。 我把它作为参考。
我已将其更改为等待任务并将返回值提取到结果变量中,以便您可以看到它返回 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());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.