0

我需要调用SetSettings()并使用 中的 3 个元素splitSettings,设置EncodeAudioFalse. 我该怎么做呢?将对象的属性转换为字符串中的姓名。我意识到我可以用我所有设置的 switch 语句来做,但必须有一种更动态的方式来做这件事。

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

是的,我有一个设置实例

说:

Settings settingManager = new Settings();

我想做的是通过使用 splitSettings 的元素将 EncodeAudo 动态设置为 False

settingManager.EncodeAudio = False;

感谢 TBohnen.jnr 的帮助,我得到了这个答案:

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

3 回答 3

0

也许您应该将可设置属性标记为static,然后尝试使用反射设置值:

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];
   }
}
于 2011-04-16T19:52:41.077 回答
0

编辑用 int、bool、double 和 string 对其进行了测试,它工作正常,还添加了一个检查以确保该属性存在并抛出它不存在的异常(可能想要更改异常类型)

编辑 2:临时解决方案,将向 convert 方法添加更多类型名,或者如果有人可以建议一种更动态的转换方式(如果没有,那么我假设你必须知道将使用的所有类型)?

EDIT3从另一个有问题的答案(克里斯泰勒)偷了转换方法,谢谢:-)

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

    }

取自http://www.haslo.ch/blog/setproperty-and-getproperty-with-c-reflection/

有兴趣看看这是否有效,创建一个快速测试:

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

输出为真,但不完全确定它是否会正确转换所有类型。

于 2011-04-16T19:54:12.543 回答
0

正如其他人所提到的,您应该考虑将 SettingsLib 类设为静态。您可能还需要处理从字符串到目标类型的值转换。这是一个如何工作的简单示例。

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; }    
  }
}
于 2011-04-16T20:00:20.367 回答