Good day.

The other day I encountered the following problem:

public void SomeMethod() { //здесь действия со статическим классом A } public void AnotherMethod() { //те же действия, но со статическим классом B } public void SomeAnotherMethod() { //опять те же действия, но со статическим классом C } 

I repeat the same actions only with different classes, if it were an ordinary class, I would create a generalized method and not be steamed, but the classes are static. Maybe there is a way to somehow get rid of repetitive actions without getting rid of static classes?

Or how to achieve a global scope for instances of non-static classes, if that is easier?

    1 answer 1

    I do not see the problem at all. If I want to work with static classes in this form, I would do this: I would declare a static object field in the App class and switch it at the beginning of each such method. If anything, then you can turn on dynamic for currentObject like this

     class App { public static object currentObject; //public static dynamic object currentObject; public static object classA, classB, classC; } class MyClass { public void SomeMethod() { App.CurrentObject = App.classA; DoWithCurrentClass(); } public void AnotherMethod() { App.CurrentObject = App.classB; DoWithCurrentClass(); } public void SomeAnotherMethod() { App.CurrentObject = App.classC; DoWithCurrentClass(); } void DoWithCurrentClass() { //Действия с CurrentObject } } 
    • Yes, and really a good idea, thank you - Specter