Can I reset a static / generic class?

I have a class shared( staticin C #) that basically contains some settings data that any class in an application can read and sometimes write. There are also some static properties that contain some internal states.

Now I want to return this class to the initial stage. With all default variables, etc. Suppose the user wants to reset the current state and start without restarting the application.

In a singleton model, I would just update it like this:

Public Sub Reset() 
    _Instance = New MyClass()
End Sub

However, this is not possible in the classroom shared. Are there any ideas on how I can do this? Or do I need to switch back to Singleton?

+5
source share
5 answers

It is not possible to do this in the same way as you specified only a singleton model. The reason is that there is no backup data store until "reset". What you could do is simulate this using an explicit method to initialize all of our data.

Public Module MyClass

  Public Sub Reset()
    Field1 = 42
    Field2 = "foo"
  End Sub

  Public Shared Field1 As Integer
  Public Shared Field2 As String
End Module

Version with class and module

Public Class MyClass
  Shared Sub New()
    Reset
  End Sub
  Private Sub New()
    ' Prevent instantiation of the class
  End Sub

  Public Sub Reset()
    Field1 = 42
    Field2 = "foo"
  End Sub

  Public Shared Field1 As Integer
  Public Shared Field2 As String
End Class
+7
source

You cannot do this in a static class, since there is no instance of a static class.

These two parameters are to switch (back) to single.

In addition, you may have a method that discards each of the static members of the class.

+3
source

, , .

+1

, .

public static class ClassA
{
     public static int id=0;
     public static string name="";

     public static void ResetValues()
     {
         // Here you want to reset to the old initialized value
         id=0;
         name="";
     }
}

reset

1 -

ClassA.ResetValues();

2 -

Type t1 = Type.GetType("Namespace1.ClassA");
MethodInfo methodInfo1 = t1.GetMethod("ResetValues");
if (methodInfo1 != null)
{
     object result = null;
     result = methodInfo1.Invoke(null, null);                
}

3 - /

foreach (var Ass in AppDomain.CurrentDomain.GetAssemblies())
{
     // Use the above "If" condition if you want to filter from only one Dll
     if (Ass.ManifestModule.FullyQualifiedName.EndsWith("YourDll.dll"))
     {
            List<Type> lstClasses = Ass.GetTypes().Where(t => t.IsClass && t.IsSealed && t.IsAbstract).ToList();
            foreach (Type type in lstClasses)
            {
                  MethodInfo methodInfo = type.GetMethod("ResetValues");
                  if (methodInfo != null)
                  {
                       object result = null;
                       result = methodInfo.Invoke(null, null);                                
                  }
             }
             break;
    }
}
0
  • - reset, , .

  • , . , .

/, , .. .

/ null/default, reset .

, .

MyStaticClass. Init .

var fields = typeof(MyStaticClass).GetFields(BindingFlags.Static | BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Public);

foreach (var field in fields)
{
    var type = field.GetType();
    field.SetValue(null, null);
}

Init(); // if there are values to be initialized

, GetProperties, reset.

0

All Articles