开发者

Convert object namespace and name to object

开发者 https://www.devze.com 2023-02-25 02:41 出处:网络
I need to call SetSettings() and using the 3 elements in splitSettings, set EncodeAudio to False. How would I go about doing that?Convert the property of a object to who\'s name I have in a string.

I need to call SetSettings() and using the 3 elements in splitSettings, set EncodeAudio to False. How would I go about doing that? Convert the property of a object to who's name I have in a string. I realize I could do with with a switch 开发者_JAVA百科statement of all my settings but there has to be a more dynamic way to go about doing this.

namespace SettingsLib
{
  public class Settings
  {
    public Boolean EncodeAudio { get; set; }
  }
}
namespace Service
{
   void SetSettings()
   {
     string[] splitSettings = { "SettingsLib.Settings", "EncodeAudio", "False" };
     // Need to set EncodeAudio to False in SettingsLib.Settings
   }
}

Yes I have a instance of Settings

Say:

Settings settingManager = new Settings();

I am trying to do is dynamically set EncodeAudo to False by using elements of splitSettings

settingManager.EncodeAudio = False;

Thanks to the help of TBohnen.jnr I came to this answer:

public void setProperty(object containingObject, string propertyName, object newValue)
{
    foreach (PropertyInfo p in containingObject.GetType().GetProperties())
    {
        if (p.Name == propertyName)
        {
            p.SetValue(containingObject, Convert.ChangeType(newValue, p.PropertyType), null);
        }
    }
}


EDIT Tested it with int, bool, double and string and it worked, also added a check to make sure that the property exists and throws an exception of it doesn't (Might want to change Exception type)

EDIT 2: Temporary solution, will add more typenames to the convert method or alternatively if somebody can suggest a more dynamic way of casting it (If not then I assume you will have to know all of the types that will be used)?

EDIT3 Stole the convert method from another answer in question (Chris Taylor ), thanks :-)

public void setProperty(object containingObject, string propertyName, object newValue)
    {
        if (containingObject.GetType().GetProperties().Count(c => c.Name == propertyName) > 0)
        {
            var type = containingObject.GetType().GetProperties().First(c => c.Name == propertyName).PropertyType;
            object val = Convert(type,(string)newValue);
            containingObject.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, containingObject, new object[] { val });
        }
        else
        {
            throw new KeyNotFoundException("The property: " + propertyName + " was not found in: " + containingObject.GetType().Name);
        }
    }

    public object convert(System.Type type, string value)
    {
        return Convert.ChangeType(value, type);

    }

Taken from http://www.haslo.ch/blog/setproperty-and-getproperty-with-c-reflection/

Was interested to see if this works, create a quick test:

class testSettings
{
    public bool SetBool { get; set; }

    public void setProperty(object containingObject, string propertyName, object newValue) 
    {
         if (containingObject.GetType().GetProperties().Count(c => c.Name == propertyName) > 0)
        {
            containingObject.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, containingObject, new object[] { newValue });
        }
        else
        {
            throw new KeyNotFoundException("The property: " + propertyName + " was not found in: " + containingObject.GetType().Name);
        }
    }
}

static void Main(string[] args)
{
    testSettings ts = new testSettings();
    ts.SetBool = false;
    ts.setProperty(ts, "SetBool", true);
    Console.WriteLine(ts.SetBool.ToString());
    Console.Read();
}

The output is true, not entirely sure if it will convert all types correctly though.


As others have mentioned, you should consider making your SettingsLib class static. And you might also need to handle the conversion of values from strings to the target types. Here is a simple example how this would work.

namespace Service
{
  class Program
  {
    static void Main(string[] args)
    {
      string[] splitSettings = { "SettingsLib.Settings", "EncodeAudio", "False" };
      SetProperty(splitSettings[0], splitSettings[1], splitSettings[2]);  
    }

    static void SetProperty(string typeName, string propertyName, object value)
    {
      var type = Type.GetType(typeName);
      if (type == null) 
      {
        throw new ArgumentException("Unable to get type", "typeName");
      }

      var pi = type.GetProperty(propertyName);
      if (pi == null) 
      {
        throw new ArgumentException("Unable to find property on type", "propertyName");
      }

      object propertyValue = value;

      if (propertyValue != null)
      {
        // You might need more elaborate testing here to ensure that you can handle 
        // all the various types, you might need to special case some types here 
        // but this will work for the basics.
        if (pi.PropertyType != propertyValue.GetType())
        {
          propertyValue = Convert.ChangeType(propertyValue, pi.PropertyType);
        }
      }

      pi.SetValue(null, propertyValue, null);
    }
  }
}

namespace SettingsLib
{
  public static class Settings
  {
    public static bool EncodeAudio { get; set; }    
  }
}


Maybe you should mark your settable properties as static and then try to set the values using Reflection:

namespace SettingsLib
{
  public static class Settings
  {
    public static bool EncodeAudio { get; set; }
  }
}
namespace Service
{
   void SetSettings()
   {
     string[] splitSettings = { "SettingsLib.Settings", "EncodeAudio", "False" };
     dynamic property = Type.GetType(splitSettings[0]).GetProperty(splitSettings[1]);
     property = splitSettings[2];
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号