我想创建一个静态类,它将从XML文件中加载一些设置,并将这些设置应用于它自己的属性。
我尝试使用下面的代码,但我不知道该为SetValue方法提供什么,因为我们要为其设置属性的类是静态的。
// some code removed ...
Type settingsType = typeof(Settings); // Settings is a static class
foreach (PropertyInfo propertyInformation in settingsType.GetProperties(BindingFlags.Public |
BindingFlags.Static))
{
//------------------------------------------------------------
// Determine if configured setting matches current setting based on name
//------------------------------------------------------------
if (propertyInformation.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
{
//------------------------------------------------------------
// Attempt to apply configured setting
//------------------------------------------------------------
try
{
if (propertyInformation.CanWrite)
{
propertyInformation.SetValue(this, Convert.ChangeType(value, propertyInformation.PropertyType, CultureInfo.CurrentCulture), null);
}
}
catch
{
}
break;
}
}
有没有可能用反射来设置静态类的属性?
https://stackoverflow.com/questions/3460745
复制相似问题