I want to make a static class that would load some settings from XML file and apply those settings to its own properties.
I am trying to use the following code but I don't really know what to give to the SetValue method since the class for which we want to set the property is static.
// 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; }
}
Is it even possible to set properties on static classes with reflection?
A static class can only have static members — you cannot declare instance members (methods, variables, properties, etc.)
Reflection provides objects (of type Type) that describe assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.
Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object. Static classes cannot contain an instance constructor. However, they can contain a static constructor.
The scope of a static class is limited to the application domain. Each app domain will have its own copy of any static variables you might have. If your "processes" are threads within the same app domain, then they will share the static values.
Just pass null
for the instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With