Tôi muốn tạo một lớp tĩnh có thể tải một số thiết lập từ tệp XML và áp dụng các thiết lập đó cho các thuộc tính riêng của nó.Thiết lập các thuộc tính với sự phản chiếu trên các lớp tĩnh
Tôi đang cố gắng sử dụng mã sau đây nhưng tôi thực sự không biết phải đưa ra phương thức SetValue vì lớp mà chúng tôi muốn đặt thuộc tính là tĩnh.
// 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;
}
}
Là nó thậm chí có thể thiết lập các thuộc tính trên lớp tĩnh với sự phản ánh?
'Cài đặt' là **' một phần nội bộ được đóng kín Cài đặt' **? –