Elton da Costa
Elton da Costa

Reputation: 167

Obtain members' name and value

I have the following method to return a Dictionary<string, string> with the names of all public members (fields and properties) of an object as the dictionary key. I can get the name of the members, but I can't get their values. Could anyone tell me how to achieve this in the method below:

 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;
}

Error:

Object does not match target type. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Reflection.TargetException: Object does not match target type.

Upvotes: 0

Views: 109

Answers (2)

Ann L.
Ann L.

Reputation: 13975

A note, here:

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

}

You are assuming that ALL your properties are strings. Are they?

If they aren't, but you want strings anyway, you can use this code:

 object objValue = propertyInfo.GetValue(objeto, null);     
 value = (objValue == null) ? null : objValue.ToString();

The above code also takes into consideration that property values may be null. I didn't take into consideration the possibility of indexed properties, but if you have any, you'll need to accommodate them.

Also, as Lasse V. Karlsen has pointed out, by passing this instead of objeto, you are trying to pull property values from the parent class of the method, not objeto. If they aren't the same object, you won't get the results you want; if they aren't even the same type of object, then you'll get an error.

Finally, you've used the term "attributes", which refers to something other than properties in .NET, and also you've referred to class variables, which are also not properties. Are the properties actually what you want, as opposed to "fields" or attributes applied to the definition of the class?

Upvotes: 1

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391734

Two things here:

  1. You're passing in this, instead of objeto, which means you're trying to read the properties off of the wrong object.
  2. You're not ensuring that you're only trying to read properties that aren't indexers.

Try changing the foreach to this:

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

Upvotes: 2

Related Questions