1 Replies - 5740 Views - Last Post: 13 August 2013 - 05:01 PM Rate Topic: -----

#1 geewhiz   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 56
  • Joined: 20-August 07

PropertyInfo.SetValue does not like array of values?

Posted 13 August 2013 - 01:51 PM

Hello all.

Let's say I have a class of static properties that I am trying to set from another ReadTextFile routine by using reflection. Basically I have a class of properties who's values I want to set are in a text file. Thus far I have been successful in setting single values from the text file to single item properties. However I have been unsuccessful with a List or array of items to a property.

Here is some of my stripped down code. So have a class of properties.

public class FileSettings
{
  private static string m_SingleSetting;
  private static int[] m_ListOfSettings;

  public static string SingleSetting
  {
    get { return m_SingleSetting; }
    set { m_SingleSetting = value; }
  }

  public static int[] ListOfSettings
  {
    get { return m_ListOfSettings; }
    set { m_ListOfSettings = value; }
  }
}



And I have a routine that will dig through my text file, get the values, perform casting, and then load the data into the corresponding property. The routine will set my FileSettings class.

Here are the couple of execution statements from my main().

FileSettings ProgramSettings = new FileSettings();
ProgramSettings = FileHandlingClass.ReadFile(filelocation, ProgramSettings);



My settings file will look something like this...

Quote

[SingleSetting]
Setting1
[ListOfSettings]
Setting2
Setting3
Setting4


In the next routine I'm not going to give all my code here for simplicity. Just the main points. So after a few things I will have an array of all my line items of the text file and an array of the properties within the FileSettings class.

public static FileSettings ReadFile(string FileLocation, FileSettings programSettings)
{
  // Some code for chopping up my text file and getting class properties
  string FileLines[] = FileContent.Split(blah blah);
  Type TypeOfClass = programSettings.GetType();
  System.Reflection.PropertyInfo[] ClassProperties= TypeOfClass.GetProperties();

  while ( Not End of File)
  {

    // ** Some logic code for determining which lines of my text file are a property header (ie. [SingleSetting])
    // ** Code will also get the single text line or a list of text lines (Properties) 

    // ** Code for filling ListProperties
    List<string> ListProperties = new List<string>();

    // ** Step through all of the properties of my FileSettings class and match Property Name's with text file property line headers.
    foreach (System.Reflection.PropertyInfo info in ClassProperties)
    {
      // ** Some code for matching file headers with my FileSettings class properties

      // ** Code for casting my string values from the text file to actual variable types, simple case switch
      string VariableType = info.PropertyType.ToString();
      // Quick list for adding casted properties
      List<object> CastedProperties = new List<object>();
      foreach (string Setting in ListProperties)
      {
        switch (VaraibleType)
        {
          case "System.Int32":
            CastProperties.Add(Convert.ToInt32(Setting)));
            break;
          case "System.String":  // Trivial case since data from a text file is string...
            CastProperties.Add(Convert.ToString(Setting));
            break;
          case "..." // And so on...
            break;
          default:
            CastProperties.Add(null);
            break;
        }
      }

      object[] ArrayProperties = CastProperties.ToArray();

      // ** I get to the point where it is time to set my values to my class FileSettings Properties.
      // ** So I loop here to step through all of my ArrayProperties and load them to the corresponding class property

      for (int PropertyCount = 0; PropertyCount < ArrayProperties.Length; PropertyCount++)
      {
        if (ArrayProperties[PropertyCount] != null)
        {
          if (ArrayProperties.Length > 1)  // I have more than one property
          {
            info.SetValue(programSettings, ArrayProperty[PropertyCount], null);
          }
          else  // Single property to fill
          {
            info.SetValue(programSettings, ArrayProperty[PropertyCount], null);
          }
        }
      }
    break;
  }
}



Hopefully I didn't mess up too much of the logic there for you guys to see.
So my code properly processes all text file single items and is able to set them to a corresponding single FileSettings property. I have problems when it gets to the list of properties.

But basically where I perform the .SetValue methods, I am having 2 particular issues that I haven't been able to work out. In the code above, whether I have more than one property or a single item, the same .SetValue method is used.
And I get the following error when the code gets to a list (array) of properties.

Quote

System.ArgumentException: Object of type 'System.Int32' cannot be converted to type 'System.Int32[]'


Which I guess makes sense because I am trying to put a Int32 into my FileSettings property of type Int32[].
Maybe there is a simple way to perform the correct type change to an array of values?

So anyway, I tried another .SetValue overload:

SetValue(Object, Object, Object[])



Third item Object[] is optional index values for indexed properties. This value should be null for non-indexed properties.

Ok. Looks better for indexed properties? I'm not clear on this...

if (Properties.Length > 1)
{
  info.SetValue(programSettings, Property[PropertyCount], new object[] { Properties.Count() });
}
else
{
  info.SetValue(programSettings, Property[PropertyCount], null);
}



Now all is good until I get to my list of properties again.
At "info.SetValue(programSettings, Property[PropertyCount], new object[] { Properties.Count() });"

Quote

Exception: Parameter count mismatch.


When I execute this the value of is equal to:

Quote

new object[] { Properties.Count() } = {object[1]} with the value of 3.


Having the value of 3 seems right to me because I have 3 properties to load.
Can anyone help with setting data of a particular type into an array of values?

Is This A Good Question/Topic? 0
  • +

Replies To: PropertyInfo.SetValue does not like array of values?

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: PropertyInfo.SetValue does not like array of values?

Posted 13 August 2013 - 05:01 PM

Way too much work on your part.
Just serialize your settings class to XML.
Then you deserialze the file back to a new SettingsClass (or whatever) class instance.
Let the framework do the work for you: Work smarter not harder.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1