Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获得会员';名称和值_C#_Reflection - Fatal编程技术网

C# 获得会员';名称和值

C# 获得会员';名称和值,c#,reflection,C#,Reflection,我使用以下方法返回一个字典,其中包含一个对象的所有公共成员(字段和属性)的名称作为字典键。我可以获取成员的名称,但无法获取其值。谁能告诉我如何通过以下方法实现这一点: public Dictionary<String, String> ObjectProperty(object objeto) { Dictionary<String, String> dictionary = new Dictionary<String, String>();

我使用以下方法返回一个
字典
,其中包含一个对象的所有公共成员(字段和属性)的名称作为字典键。我可以获取成员的名称,但无法获取其值。谁能告诉我如何通过以下方法实现这一点:

 public Dictionary<String, String> ObjectProperty(object objeto)
 {
    Dictionary<String, String> dictionary = new Dictionary<String, String>();

    Type type = objeto.GetType();
    FieldInfo[] field = type.GetFields();
    PropertyInfo[] myPropertyInfo = type.GetProperties();

    String value = null;

    foreach (var propertyInfo in myPropertyInfo)
    {
        value = (string)propertyInfo.GetValue(this, null); //Here is the error
        dictionary.Add(propertyInfo.Name.ToString(), value);
    }

    return dictionary;
}
公共字典对象属性(对象对象对象)
{
字典=新字典();
Type Type=objeto.GetType();
FieldInfo[]field=type.GetFields();
PropertyInfo[]myPropertyInfo=type.GetProperties();
字符串值=null;
foreach(myPropertyInfo中的var propertyInfo)
{
value=(string)propertyInfo.GetValue(this,null);//这是错误
Add(propertyInfo.Name.ToString(),value);
}
返回字典;
}
错误:

对象与目标类型不匹配。 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源

异常详细信息:System.Reflection.TargetException:对象与目标类型不匹配。

这里有两件事:

  • 您传入的是
    this
    ,而不是
    objeto
    ,这意味着您试图读取错误对象的属性
  • 您不能确保只读取非索引器的属性
  • 尝试将foreach更改为:

    foreach (var propertyInfo in myPropertyInfo)
    {
        if (propertyInfo.GetIndexParameters().Length == 0)
        {
            value = (string) propertyInfo.GetValue(objeto, null);
            dictionary.Add(propertyInfo.Name.ToString(), value);
        }
    }
    
    这里有两件事:

  • 您传入的是
    this
    ,而不是
    objeto
    ,这意味着您试图读取错误对象的属性
  • 您不能确保只读取非索引器的属性
  • 尝试将foreach更改为:

    foreach (var propertyInfo in myPropertyInfo)
    {
        if (propertyInfo.GetIndexParameters().Length == 0)
        {
            value = (string) propertyInfo.GetValue(objeto, null);
            dictionary.Add(propertyInfo.Name.ToString(), value);
        }
    }
    
    请注意:

    foreach (var propertyInfo in myPropertyInfo)
    {
        value = (string) propertyInfo.GetValue(this, null); //Here is the error
        dictionary.Add(propertyInfo.Name.ToString(), value);
    
    }
    
    假设所有属性都是字符串。是吗

    如果它们不是,但您仍然需要字符串,则可以使用以下代码:

     object objValue = propertyInfo.GetValue(objeto, null);     
     value = (objValue == null) ? null : objValue.ToString();
    
    上面的代码还考虑到属性值可能为null。我没有考虑索引属性的可能性,但是如果您有索引属性,您需要容纳它们

    另外,正如Lasse V.Karlsen所指出的,通过传递
    this
    而不是
    objeto
    ,您试图从方法的父类中提取属性值,而不是
    objeto
    。如果它们不是同一个对象,你就不会得到你想要的结果;如果它们甚至不是相同类型的对象,那么您将得到一个错误

    最后,您使用了术语“attributes”,它指的是.NET中的属性以外的其他内容,也指的是类变量,它们也不是属性。与应用于类定义的“字段”或属性相反,属性实际上是您想要的吗

    请注意:

    foreach (var propertyInfo in myPropertyInfo)
    {
        value = (string) propertyInfo.GetValue(this, null); //Here is the error
        dictionary.Add(propertyInfo.Name.ToString(), value);
    
    }
    
    假设所有属性都是字符串。是吗

    如果它们不是,但您仍然需要字符串,则可以使用以下代码:

     object objValue = propertyInfo.GetValue(objeto, null);     
     value = (objValue == null) ? null : objValue.ToString();
    
    上面的代码还考虑到属性值可能为null。我没有考虑索引属性的可能性,但是如果您有索引属性,您需要容纳它们

    另外,正如Lasse V.Karlsen所指出的,通过传递
    this
    而不是
    objeto
    ,您试图从方法的父类中提取属性值,而不是
    objeto
    。如果它们不是同一个对象,你就不会得到你想要的结果;如果它们甚至不是相同类型的对象,那么您将得到一个错误


    最后,您使用了术语“attributes”,它指的是.NET中的属性以外的其他内容,也指的是类变量,它们也不是属性。与应用于类定义的“字段”或属性相反,属性实际上是您想要的吗

    “错误”?哪个错误?您不应该传入对象的实例以获取属性的值吗!=属性在GetValue方法
    propertyInfo.GetValue中使用objecto(objeto,null)“错误”?哪个错误?您不应该传入对象的实例以获取属性的值吗!=属性在GetValue方法
    propertyInfo.GetValue中使用objecto(objeto,null)