Re-initializing a static class?

I have a class in a web application I'm working on that contains client settings. For some background, I do not own this class, and changing it is not an option. Recently, we added some logic to store the settings in the database, and I was instructed to create a page for editing them, fairly fair.

Here is my problem; settings are stored in a static class and are themselves static, read-only. for instance

public static class Settings { public static readonly setting1 = SettingmanagerClass.GetSetting("setting1"); public static readonly setting2 = SettingmanagerClass.GetSetting("setting2"); public static readonly setting3 = SettingmanagerClass.GetSetting("setting3"); } 

Now, for example, on the page I wrote, we change the value for parameter2 to "Happy variable"; it saves the database just fine, but now I need it to be reflected in the web application as a new value. Since this is a static readonly property of a static class, it is only ever called when the application first connects and cannot be manually installed.

To repeat, I do not have the original class, so "just create properties that can be written" is not (currently) valid. Usually I just talk about it with my boss, and he makes a decision and maybe allows me to change another class, but I am not able to make this call and it does not work for a week.

So basically; is there any way to reinitialize a static class after launching a web application? I just need to reload all its properties, as if the application was just rebuilt and started again.

+6
source share
9 answers

If the above code reflects the situation you are in, you will not be able to reinitialize the code unless you do something especially hacked with reflection (this is not recommended by the way).

Edit: Oh wait - I did not understand that this is a web application. You can programmatically bounce the application :

 System.Web.HttpRuntime.UnloadAppDomain 
+2
source
  ConstructorInfo constructor = typeof(Settings).GetConstructor(BindingFlags.Static | BindingFlags.NonPublic,null, new Type[0], null); constructor.Invoke(null, null); 
+14
source

You can use reflection:

 var prop = typeof(Settings).GetField("setting1", BindingFlags.Static | BindingFlags.Public); prop.SetValue(null, "Bar"); string currentValue = Settings.setting1; //Bar 
+4
source

The only option I need requires a lot of work:

  • Create Another AppDomain
  • Loading an assembly in another domain
  • Use Remoting to Get Data
  • If the settings have changed, unload AppDomain and perform steps 1 to 3 again.
+1
source

I would use reflection

 var info = typeof(Settings) .GetField("Settings",BindingFlags.Static|BindingFlags.Public); info.SetValue(null, "setting4"); 
+1
source
 public static class Settings { public static name = ""; static Settings() { ReInitialize(); } public static void ReInitialize() { name = "My name is re-initialized"; } } Settings.name = "My name has changed"; // Console.WriteLine(Settings.name); Settings.ReInitialize(); //name is "My name is re-initialized" // Console.WriteLine(Settings.name); 
+1
source

Hmm, do you want to find a way to crack a class? even if it exists with reflection and something like that, it is not a good way to solve this problem.

Fast workaround. I can suggest creating your own, not just static properties, initializing these static variables and using them everywhere.

But it is better to use cache stores or applications instead of static variables

Hope this helps

0
source

An old thread that I know, but one thing I did is create an initialize method (public static void) that sets all the variables (they are all public). This method makes database calls and sets class variables. Then in the code, anytime you want to update the variables (i.e. when you call SaveChanges ()), you can call Class.Initialize (), and you're done.

I use this to cache general lists of search information that are subject to change, and we need to synchronize them when the database is updated from the application.

0
source

Change your values ​​to properties:

 public static class Settings { public static setting1 { get { return SettingmanagerClass.GetSetting("setting1"); } } public static setting2 { get { return SettingmanagerClass.GetSetting("setting2"); } public static setting3 { get { return SettingmanagerClass.GetSetting("setting3"); } } 

Also, this does not change the signature of your code.

-2
source

Source: https://habr.com/ru/post/913949/


All Articles