c#-如果一个类具有另一个类的属性,如何判断内部类的属性是否匹配
发布时间:2022-08-09 15:57:16 286
相关标签: # flask
我正在研究一种能够查看 2 个对象是否匹配的解决方案。重要的是,对于 2 个对象,如果它们共享一个属性,则这些属性的值是匹配的
对于两个类 SC 和 DI,Test 属性正确地评估为 True。但是 Little 类将评估为假,因为 SC 和 DI 使用 Little 对象的不同实例,即使 Little 类的信息是正确的。
namespace HelloWorld
{
using System;
using System.Reflection;
public class Little
{
public string L1 { get; set; }
public string L2 { get; set; }
}
public class Solution
{
public string Name { get; set; }
public string Test { get; set; }
public Little L {get; set; }
}
public class Device
{
public string Name { get; set; }
public string Test { get; set; }
public Little L {get; set; }
}
public class Program
{
static void Main(string[] args)
{
var Li = new Little {
L1 = "L1111",
L2 = "L2222"
};
var Li2 = new Little {
L1 = "L1111",
L2 = "L2222"
};
var SC = new Solution {
Name = "SC",
Test = "T1",
L = Li2
};
var DI = new Device {
Name = "DI",
Test = "T1",
L = Li
};
foreach (var property in SC.GetType().GetProperties())
{
var deviceProperty = DI.GetType().GetProperty(property.Name);
if (deviceProperty != null)
{
if (deviceProperty.GetValue(DI) != property.GetValue(SC))
{
// it is registered as flase since DI and SC have different Little objects - even though thouse object values are the same
// Best way to determine if the values are of type Little (or any other me-defined class) - because wont .IsClass evaluate string to True as well?
// What if there was another class inside Litte? Do I have to recursively / loop until I can see everything is a string or int?
}
else
{
Console.WriteLine("We have a match");
}
}
}
}
}
}
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报