将对象命名空间和名称转换为对象
我需要调用 SetSettings()
并使用 splitSettings
中的 3 个元素,将 EncodeAudio
设置为 False
。 我该怎么做呢?将对象的属性转换为我在字符串中拥有的名字。 我意识到我可以使用所有设置的 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 的实例
说:
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);
}
}
}
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 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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑使用 int、bool、double 和 string 对其进行了测试,它有效,还添加了一个检查以确保该属性存在并抛出异常(可能想要更改异常类型)
编辑2:临时解决方案,将向转换方法添加更多类型名称,或者如果有人可以建议一种更动态的方式来转换它(如果没有,那么我假设您必须知道所有类型)将被使用)?
EDIT3 从另一个有问题的答案(Chris Taylor)窃取了转换方法,谢谢:-)
取自http://www.haslo.ch/blog/setproperty-and-getproperty-with-c-reflection/
有兴趣看看这是否有效,创建一个快速测试:
输出是正确的,但不完全确定它是否会正确转换所有类型。
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 :-)
Taken from http://www.haslo.ch/blog/setproperty-and-getproperty-with-c-reflection/
Was interested to see if this works, create a quick test:
The output is true, not entirely sure if it will convert all types correctly though.
正如其他人提到的,您应该考虑将 SettingsLib 类设为静态。您可能还需要处理值从字符串到目标类型的转换。这是一个简单的例子,它是如何工作的。
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.
也许您应该将可设置属性标记为静态,然后尝试使用反射设置值:
Maybe you should mark your settable properties as
static
and then try to set the values using Reflection: