获取不同对象中名称相同属性的方法,可以通过反射实现。以下是C#实现该方法的步骤和示例说明:
步骤
步骤一:获取对象类型
使用GetType()
方法获取对象的类型,返回Type
类型的实例。
Type objectType = objectInstance.GetType();
步骤二:获取属性信息
使用GetProperties()
方法获取对象的所有属性,返回PropertyInfo
类型的数组。
PropertyInfo[] properties = objectType.GetProperties();
步骤三:过滤重复属性,获取名称相同的属性信息
遍历属性数组,通过属性名称筛选出名称相同的属性信息。
List<PropertyInfo> duplicateProperties = new List<PropertyInfo>();
List<string> propertyNames = new List<string>();
foreach (PropertyInfo propertyInfo in properties)
{
if (propertyNames.Contains(propertyInfo.Name))
{
duplicateProperties.Add(propertyInfo);
}
else
{
propertyNames.Add(propertyInfo.Name);
}
}
步骤四:获取属性值
遍历属性信息数组,使用GetValue()
方法获取对象的该属性值。
foreach (PropertyInfo propInfo in duplicateProperties)
{
object propertyValue = propInfo.GetValue(objectInstance, null);
Console.WriteLine("{0} = {1}", propInfo.Name, propertyValue);
}
示例
假设有以下两个类:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public class Animal
{
public string Name { get; set; }
public int LegCount { get; set; }
}
示例一
创建两个实例,分别为Person
和Animal
,并使用上述方法获取名称相同的属性值。
Person person = new Person { Name = "Lucy", Age = 18 };
Animal animal = new Animal { Name = "Tom", LegCount = 4 };
object[] objects = new object[] { person, animal };
foreach (object obj in objects)
{
Console.WriteLine("Type: {0}", obj.GetType().Name);
Console.WriteLine("-------------");
Type objectType = obj.GetType();
PropertyInfo[] properties = objectType.GetProperties();
List<PropertyInfo> duplicateProperties = new List<PropertyInfo>();
List<string> propertyNames = new List<string>();
foreach (PropertyInfo propertyInfo in properties)
{
if (propertyNames.Contains(propertyInfo.Name))
{
duplicateProperties.Add(propertyInfo);
}
else
{
propertyNames.Add(propertyInfo.Name);
}
}
foreach (PropertyInfo propInfo in duplicateProperties)
{
object propertyValue = propInfo.GetValue(obj, null);
Console.WriteLine("{0} = {1}", propInfo.Name, propertyValue);
}
Console.WriteLine("-------------");
}
输出结果:
Type: Person
-------------
Name = Lucy
-------------
Type: Animal
-------------
Name = Tom
-------------
示例二
创建两个字典,分别为personDic
和animalDic
,并使用上述方法获取名称相同的属性值。
Dictionary<string, object> personDic = new Dictionary<string, object>();
personDic.Add("Name", "Lucy");
personDic.Add("Age", 18);
Dictionary<string, object> animalDic = new Dictionary<string, object>();
animalDic.Add("Name", "Tom");
animalDic.Add("LegCount", 4);
Dictionary<string, object>[] dictionaries = new Dictionary<string, object>[] { personDic, animalDic };
foreach (Dictionary<string, object> dictionary in dictionaries)
{
Console.WriteLine("Type: {0}", dictionary.GetType().Name);
Console.WriteLine("-------------");
Type objectType = dictionary.GetType().GetGenericArguments()[0];
PropertyInfo[] properties = objectType.GetProperties();
List<PropertyInfo> duplicateProperties = new List<PropertyInfo>();
List<string> propertyNames = new List<string>();
foreach (PropertyInfo propertyInfo in properties)
{
if (propertyNames.Contains(propertyInfo.Name))
{
duplicateProperties.Add(propertyInfo);
}
else
{
propertyNames.Add(propertyInfo.Name);
}
}
foreach (PropertyInfo propInfo in duplicateProperties)
{
object propertyValue = dictionary[propInfo.Name];
Console.WriteLine("{0} = {1}", propInfo.Name, propertyValue);
}
Console.WriteLine("-------------");
}
输出结果:
Type: Dictionary`2
-------------
Name = Lucy
-------------
Type: Dictionary`2
-------------
Name = Tom
-------------
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#实现获取不同对象中名称相同属性的方法 - Python技术站