C# employee.GetType().GetProperty(property.Name)上的System.Reflection.TargetException
我遇到了一个我似乎无法解决的问题。我确信对此有一个简单的解释,但我不明白为什么我在尝试从employee对象(在本例中)获取属性时会得到System.Reflection.TargetException:“对象与目标类型不匹配”C# employee.GetType().GetProperty(property.Name)上的System.Reflection.TargetException,c#,system.reflection,C#,System.reflection,我遇到了一个我似乎无法解决的问题。我确信对此有一个简单的解释,但我不明白为什么我在尝试从employee对象(在本例中)获取属性时会得到System.Reflection.TargetException:“对象与目标类型不匹配” employee.GetType().GetProperty(property.Name) 搜索错误会返回许多描述调用Set/GetValue方法时出现问题的结果,但我还没有找到解决此问题的方法 我在抛出异常的地方设置了一个断点,它显示property.Name确实是
employee.GetType().GetProperty(property.Name)
搜索错误会返回许多描述调用Set/GetValue方法时出现问题的结果,但我还没有找到解决此问题的方法
我在抛出异常的地方设置了一个断点,它显示property.Name确实是一个值,并且是对象的真实属性。我还尝试手动指定我知道存在的属性。还是一样
有什么建议吗
编辑:尝试了以下操作:
Type type = typeof (Employee); //Throws the TargetException
PropertyInfo theProperty = type.GetProperty(property.Name);
现在在上面的第一行抛出相同的异常
编辑:添加了关于我正在构建的应用程序的代码和更多详细信息
Employee的类定义(为了简化到此类“表示”的JSON数据的映射,类/字段使用挪威语-这是数据的格式/语言,抱歉:-)
“Ansatt”=雇员。“Ansattnummer”=雇员
[JsonObject]
public class Ansatt
{
public int Ansattnummer { get; set; }
public string Fornavn { get; set; }
public string Etternavn { get; set; }
public int Pin { get; set; }
public string Adresse { get; set; }
public int Postnummer { get; set; }
public string Poststed { get; set; }
public int TlfPrivat { get; set; }
public int MobilTlf { get; set; }
public string EpostAdresse { get; set; }
public DateTime Fodt { get; set; }
}
我的应用程序从web服务检索给定的数据集—它可以是员工、项目或其他一些可能的数据集。要获取的数据在运行时由用户决定。用户还可以通过URL查询指定他/她想要的数据集的哪些部分,例如列。然后,程序使用选定的数据创建一个csv文件
以下是我使用的代码:
if (records != null && records.Count != 0) //records contains the chosen dataset - in this case Employees (Ansatt).
{
if (records.GetType() == typeof (List<Ansatt>))
{
foreach (var model in records as List<Ansatt>)
{
var temp = new Ansatt();
foreach (var property in model.GetType().GetProperties())
{
var currentProperty = model.GetType().GetProperty(property.Name);
if (currentProperty != null)
{
Type type = typeof (Ansatt); //Throws System.Reflection.TargetException: 'Object does not match target type'
PropertyInfo tempProperty = type.GetProperty(property.Name);
tempProperty.SetValue(temp, currentProperty.GetValue(property.Name));
}
}
csv.WriteRecord(temp);
}
}
}
if(records!=null&&records.Count!=0)//记录包含所选的数据集-在本例中为Employees(Ansatt)。
{
if(records.GetType()==typeof(List))
{
foreach(记录中的var模型作为列表)
{
var temp=新的Ansatt();
foreach(model.GetType().GetProperties()中的var属性)
{
var currentProperty=model.GetType().GetProperty(property.Name);
如果(currentProperty!=null)
{
Type Type=typeof(Ansatt);//抛出System.Reflection.TargetException:'对象与目标类型不匹配'
PropertyInfo tempProperty=type.GetProperty(property.Name);
SetValue(temp,currentProperty.GetValue(property.Name));
}
}
csv.写入记录(临时);
}
}
}
您需要指定属性的名称
PropertyInfo value=employee.GetType().GetProperty(“名称”);
您需要指定属性的名称
PropertyInfo value=employee.GetType().GetProperty(“名称”);
要通过反射获取对象的属性,请确保属性名是getter和setter的public,否则将返回null
前
请阅读一些信息。要通过反射获取对象的属性,请确保属性名是getter和setter的public,否则将返回null 前 请阅读一些信息。按照惯例,您应该这样使用:
class MyClass {
private int myProperty;
// Declare MyProperty.
public int MyProperty {
get {
return myProperty;
}
set {
myProperty = value;
}
}
}
public class MyTypeClass {
public static void Main(string[] args) {
try {
// Get the Type object corresponding to MyClass.
Type myType = typeof(MyClass);
// Get the PropertyInfo object by passing the property name.
PropertyInfo myPropInfo = myType.GetProperty("MyProperty");
// Display the property name.
Console.WriteLine("The {0} property exists in MyClass.", myPropInfo.Name);
// Instantiate MyClass
var myObject = new MyClass()
{
MyProperty = 5
};
// Get value using reflection
Console.WriteLine("My property value for my object is {0}.", myPropInfo.GetValue(myObject));
} catch (NullReferenceException e) {
Console.WriteLine("The property does not exist in MyClass." + e.Message);
}
}
}
对于您的代码,当您想要获取对象实例的属性值时,应该将对象作为引用传递给PropertyInfo.GetValue(object)函数。
与此相反:
tempProperty.SetValue(temp, currentProperty.GetValue(property.Name));
这样做:
tempProperty.SetValue(temp, currentProperty.GetValue(model));
按照惯例,您应该这样使用它:
class MyClass {
private int myProperty;
// Declare MyProperty.
public int MyProperty {
get {
return myProperty;
}
set {
myProperty = value;
}
}
}
public class MyTypeClass {
public static void Main(string[] args) {
try {
// Get the Type object corresponding to MyClass.
Type myType = typeof(MyClass);
// Get the PropertyInfo object by passing the property name.
PropertyInfo myPropInfo = myType.GetProperty("MyProperty");
// Display the property name.
Console.WriteLine("The {0} property exists in MyClass.", myPropInfo.Name);
// Instantiate MyClass
var myObject = new MyClass()
{
MyProperty = 5
};
// Get value using reflection
Console.WriteLine("My property value for my object is {0}.", myPropInfo.GetValue(myObject));
} catch (NullReferenceException e) {
Console.WriteLine("The property does not exist in MyClass." + e.Message);
}
}
}
对于您的代码,当您想要获取对象实例的属性值时,应该将对象作为引用传递给PropertyInfo.GetValue(object)函数。
与此相反:
tempProperty.SetValue(temp, currentProperty.GetValue(property.Name));
这样做:
tempProperty.SetValue(temp, currentProperty.GetValue(model));
什么是
property.Name
?property.Name是int32 EmployeeNo。GetProperty()将属性的名称作为字符串,而不是int32。是的,对不起。Name返回“EmployeeNo”-因此我确实为GetProperty()提供了一个字符串。“EmployeeNo”的值是一个整数,向我们显示有关异常(StackTace,InnerException)的更多信息什么是属性。Name
?property.Name是int32 EmployeeNo。GetProperty()将属性的名称作为字符串,而不是int32。是的,抱歉。Name返回“EmployeeNo”-因此我确实为GetProperty()提供了一个字符串。“EmployeeNo”的值是一个intShow,告诉我们有关异常的更多信息(StackTace,InnerException)谢谢,但是我已经在属性上有了一个公共getter和setter。你的属性.Name与你想要获得的属性匹配吗?你能发布你的员工和属性的代码吗?Name?仅仅是一个简单的反映似乎很奇怪。谢谢,但是我已经在属性上有了一个公共的getter和setter。你的property.Name与你想要得到的属性匹配吗?你能发布你的员工和属性的代码吗?仅仅是为了一个简单的反映,这似乎很奇怪。我试着这样做,现在当我调用'Type Type=typeof(Employee)时抛出异常;请发布您的员工类定义代码。Kerem:更新了问题并提供了更多详细信息相应地更新了我的答案。我试着这样做,现在当我调用'Type Type=typeof(Employee)时抛出异常;请发布您的员工类别定义代码。凯伦:更新了问题的更多细节,并相应地更新了我的答案。