如果 属性 名称定义为 "name",如何使用反射获取 属性 值
How to get a property value using Reflection if the property name is defined as "name"
我有以下代码来比较两个集合...
//code invocation
CollectionComparer comp = new CollectionComparer("name", "ASC");
this.InnerList.Sort(comp);
class
public class CollectionComparer : IComparer
{
private String _property;
private String _order;
public CollectionComparer(String Property, String Order)
{
this._property = Property;
this._order = Order;
}
public int Compare(object obj1, object obj2)
{
int returnValue;
Type type = obj1.GetType();
PropertyInfo propertie1 = type.GetProperty(_property); // returns null here
Type type2 = obj2.GetType();
PropertyInfo propertie2 = type2.GetProperty(_property); // returns null here
object finalObj1 = propertie1.GetValue(obj1, null); // Null Reference Exception thrown here, because propertie1 is null
object finalObj2 = propertie2.GetValue(obj2, null);
IComparable Ic1 = finalObj1 as IComparable;
IComparable Ic2 = finalObj2 as IComparable;
if (_order == "ASC")
{
returnValue = Ic1.CompareTo(Ic2);
}
else
{
returnValue = Ic2.CompareTo(Ic1);
}
return returnValue;
}
}
代码似乎工作正常,除非我尝试对名为 "name" 的 属性 进行排序。当比较 属性 时,变量 propertie1
和 propertie2
都为空,代码因此抛出异常。
所以我的问题是如何使用反射来获取名称为"name"的属性的值?
对了,_property变量设置了吗?
这个扩展方法怎么样:
public static object GetProperty(this object instance, string name)
{
if (instance == null)
throw new ArgumentNullException("instance");
if (name == null)
throw new ArgumentNullException("name");
Type type = instance.GetType();
PropertyInfo property = type.GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
if (property == null)
throw new InvalidOperationException(string.Format("Type {0} does not have a property {1}", type, name));
object result = property.GetValue(instance, null);
return result;
}
好的,我想通了...我想在进行反射时大写字母很重要...
我需要将代码调用更改为...
//code invocation
CollectionComparer comp = new CollectionComparer("Name", "ASC");
this.InnerList.Sort(comp);
因为 属性 实际上被称为 "Name" 而不是 "name"
我有以下代码来比较两个集合...
//code invocation
CollectionComparer comp = new CollectionComparer("name", "ASC");
this.InnerList.Sort(comp);
class
public class CollectionComparer : IComparer
{
private String _property;
private String _order;
public CollectionComparer(String Property, String Order)
{
this._property = Property;
this._order = Order;
}
public int Compare(object obj1, object obj2)
{
int returnValue;
Type type = obj1.GetType();
PropertyInfo propertie1 = type.GetProperty(_property); // returns null here
Type type2 = obj2.GetType();
PropertyInfo propertie2 = type2.GetProperty(_property); // returns null here
object finalObj1 = propertie1.GetValue(obj1, null); // Null Reference Exception thrown here, because propertie1 is null
object finalObj2 = propertie2.GetValue(obj2, null);
IComparable Ic1 = finalObj1 as IComparable;
IComparable Ic2 = finalObj2 as IComparable;
if (_order == "ASC")
{
returnValue = Ic1.CompareTo(Ic2);
}
else
{
returnValue = Ic2.CompareTo(Ic1);
}
return returnValue;
}
}
代码似乎工作正常,除非我尝试对名为 "name" 的 属性 进行排序。当比较 属性 时,变量 propertie1
和 propertie2
都为空,代码因此抛出异常。
所以我的问题是如何使用反射来获取名称为"name"的属性的值?
对了,_property变量设置了吗?
这个扩展方法怎么样:
public static object GetProperty(this object instance, string name)
{
if (instance == null)
throw new ArgumentNullException("instance");
if (name == null)
throw new ArgumentNullException("name");
Type type = instance.GetType();
PropertyInfo property = type.GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
if (property == null)
throw new InvalidOperationException(string.Format("Type {0} does not have a property {1}", type, name));
object result = property.GetValue(instance, null);
return result;
}
好的,我想通了...我想在进行反射时大写字母很重要...
我需要将代码调用更改为...
//code invocation
CollectionComparer comp = new CollectionComparer("Name", "ASC");
this.InnerList.Sort(comp);
因为 属性 实际上被称为 "Name" 而不是 "name"