C#: determine if a class has already been initialized



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















I have many classes in an assembly that I can't or don't want to modify. At some point of runtime, I want to know which of them have already been "initialized": static initializer (= static constructor) has run.



Is there a way to do it with reflection or something else?



For information, not every class in an assembly is initialized when the assembly is loaded. This can be easily observed with this piece of code:



public static class Foo

static Foo() MainClass.Value = "Something";
public static void DoSomething() Thread.Sleep(100);


public static class MainClass

public static string Value = "Nothing";

public static void Main()

Console.WriteLine(Value);
Foo.DoSomething();
Console.WriteLine(Value);




Displays:



Nothing
Something









share|improve this question



















  • 2





    "Is there a way to do it with reflection or something else?" No, unless you introduce a flag that you set in the static constructor. But do all your classes even have such a constructor?

    – HimBromBeere
    Nov 15 '18 at 13:18







  • 3





    Please read the question entirely.

    – Benoit Sanchez
    Nov 15 '18 at 13:20






  • 6





    @BenoitSanchez: sounds like a XY-question. What are you actually trying to do, why you think you need to know that? If you need to know if your class is initialized(so something in it is ready to use), you have to introduce a flag that is set in the initialization method.

    – Rango
    Nov 15 '18 at 13:21







  • 4





    Another funny fact is, that even if there was a way to indicate this, you´d shoot yourself in your own foot, because a static constructor is invoked the very first time you use the class in any way. Using reflection to access the class will therefor also run the static constructor, if not already done. So if you ask your class if it was initialized (whatever that actually means), it´ll allways answers: "yes".

    – HimBromBeere
    Nov 15 '18 at 13:29







  • 2





    @Christopher: you really haven't read the question ;-)

    – Rango
    Nov 15 '18 at 13:36

















2















I have many classes in an assembly that I can't or don't want to modify. At some point of runtime, I want to know which of them have already been "initialized": static initializer (= static constructor) has run.



Is there a way to do it with reflection or something else?



For information, not every class in an assembly is initialized when the assembly is loaded. This can be easily observed with this piece of code:



public static class Foo

static Foo() MainClass.Value = "Something";
public static void DoSomething() Thread.Sleep(100);


public static class MainClass

public static string Value = "Nothing";

public static void Main()

Console.WriteLine(Value);
Foo.DoSomething();
Console.WriteLine(Value);




Displays:



Nothing
Something









share|improve this question



















  • 2





    "Is there a way to do it with reflection or something else?" No, unless you introduce a flag that you set in the static constructor. But do all your classes even have such a constructor?

    – HimBromBeere
    Nov 15 '18 at 13:18







  • 3





    Please read the question entirely.

    – Benoit Sanchez
    Nov 15 '18 at 13:20






  • 6





    @BenoitSanchez: sounds like a XY-question. What are you actually trying to do, why you think you need to know that? If you need to know if your class is initialized(so something in it is ready to use), you have to introduce a flag that is set in the initialization method.

    – Rango
    Nov 15 '18 at 13:21







  • 4





    Another funny fact is, that even if there was a way to indicate this, you´d shoot yourself in your own foot, because a static constructor is invoked the very first time you use the class in any way. Using reflection to access the class will therefor also run the static constructor, if not already done. So if you ask your class if it was initialized (whatever that actually means), it´ll allways answers: "yes".

    – HimBromBeere
    Nov 15 '18 at 13:29







  • 2





    @Christopher: you really haven't read the question ;-)

    – Rango
    Nov 15 '18 at 13:36













2












2








2


0






I have many classes in an assembly that I can't or don't want to modify. At some point of runtime, I want to know which of them have already been "initialized": static initializer (= static constructor) has run.



Is there a way to do it with reflection or something else?



For information, not every class in an assembly is initialized when the assembly is loaded. This can be easily observed with this piece of code:



public static class Foo

static Foo() MainClass.Value = "Something";
public static void DoSomething() Thread.Sleep(100);


public static class MainClass

public static string Value = "Nothing";

public static void Main()

Console.WriteLine(Value);
Foo.DoSomething();
Console.WriteLine(Value);




Displays:



Nothing
Something









share|improve this question
















I have many classes in an assembly that I can't or don't want to modify. At some point of runtime, I want to know which of them have already been "initialized": static initializer (= static constructor) has run.



Is there a way to do it with reflection or something else?



For information, not every class in an assembly is initialized when the assembly is loaded. This can be easily observed with this piece of code:



public static class Foo

static Foo() MainClass.Value = "Something";
public static void DoSomething() Thread.Sleep(100);


public static class MainClass

public static string Value = "Nothing";

public static void Main()

Console.WriteLine(Value);
Foo.DoSomething();
Console.WriteLine(Value);




Displays:



Nothing
Something






c# reflection






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 11 '18 at 3:33









Cœur

19.3k10116155




19.3k10116155










asked Nov 15 '18 at 13:17









Benoit SanchezBenoit Sanchez

296113




296113







  • 2





    "Is there a way to do it with reflection or something else?" No, unless you introduce a flag that you set in the static constructor. But do all your classes even have such a constructor?

    – HimBromBeere
    Nov 15 '18 at 13:18







  • 3





    Please read the question entirely.

    – Benoit Sanchez
    Nov 15 '18 at 13:20






  • 6





    @BenoitSanchez: sounds like a XY-question. What are you actually trying to do, why you think you need to know that? If you need to know if your class is initialized(so something in it is ready to use), you have to introduce a flag that is set in the initialization method.

    – Rango
    Nov 15 '18 at 13:21







  • 4





    Another funny fact is, that even if there was a way to indicate this, you´d shoot yourself in your own foot, because a static constructor is invoked the very first time you use the class in any way. Using reflection to access the class will therefor also run the static constructor, if not already done. So if you ask your class if it was initialized (whatever that actually means), it´ll allways answers: "yes".

    – HimBromBeere
    Nov 15 '18 at 13:29







  • 2





    @Christopher: you really haven't read the question ;-)

    – Rango
    Nov 15 '18 at 13:36












  • 2





    "Is there a way to do it with reflection or something else?" No, unless you introduce a flag that you set in the static constructor. But do all your classes even have such a constructor?

    – HimBromBeere
    Nov 15 '18 at 13:18







  • 3





    Please read the question entirely.

    – Benoit Sanchez
    Nov 15 '18 at 13:20






  • 6





    @BenoitSanchez: sounds like a XY-question. What are you actually trying to do, why you think you need to know that? If you need to know if your class is initialized(so something in it is ready to use), you have to introduce a flag that is set in the initialization method.

    – Rango
    Nov 15 '18 at 13:21







  • 4





    Another funny fact is, that even if there was a way to indicate this, you´d shoot yourself in your own foot, because a static constructor is invoked the very first time you use the class in any way. Using reflection to access the class will therefor also run the static constructor, if not already done. So if you ask your class if it was initialized (whatever that actually means), it´ll allways answers: "yes".

    – HimBromBeere
    Nov 15 '18 at 13:29







  • 2





    @Christopher: you really haven't read the question ;-)

    – Rango
    Nov 15 '18 at 13:36







2




2





"Is there a way to do it with reflection or something else?" No, unless you introduce a flag that you set in the static constructor. But do all your classes even have such a constructor?

– HimBromBeere
Nov 15 '18 at 13:18






"Is there a way to do it with reflection or something else?" No, unless you introduce a flag that you set in the static constructor. But do all your classes even have such a constructor?

– HimBromBeere
Nov 15 '18 at 13:18





3




3





Please read the question entirely.

– Benoit Sanchez
Nov 15 '18 at 13:20





Please read the question entirely.

– Benoit Sanchez
Nov 15 '18 at 13:20




6




6





@BenoitSanchez: sounds like a XY-question. What are you actually trying to do, why you think you need to know that? If you need to know if your class is initialized(so something in it is ready to use), you have to introduce a flag that is set in the initialization method.

– Rango
Nov 15 '18 at 13:21






@BenoitSanchez: sounds like a XY-question. What are you actually trying to do, why you think you need to know that? If you need to know if your class is initialized(so something in it is ready to use), you have to introduce a flag that is set in the initialization method.

– Rango
Nov 15 '18 at 13:21





4




4





Another funny fact is, that even if there was a way to indicate this, you´d shoot yourself in your own foot, because a static constructor is invoked the very first time you use the class in any way. Using reflection to access the class will therefor also run the static constructor, if not already done. So if you ask your class if it was initialized (whatever that actually means), it´ll allways answers: "yes".

– HimBromBeere
Nov 15 '18 at 13:29






Another funny fact is, that even if there was a way to indicate this, you´d shoot yourself in your own foot, because a static constructor is invoked the very first time you use the class in any way. Using reflection to access the class will therefor also run the static constructor, if not already done. So if you ask your class if it was initialized (whatever that actually means), it´ll allways answers: "yes".

– HimBromBeere
Nov 15 '18 at 13:29





2




2





@Christopher: you really haven't read the question ;-)

– Rango
Nov 15 '18 at 13:36





@Christopher: you really haven't read the question ;-)

– Rango
Nov 15 '18 at 13:36












1 Answer
1






active

oldest

votes


















0














As said HimBromBeere, and as confirmed in this article,




A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.




So you need to use your class once, a property, a method, whatever, to initialize it... You have no control on it, you can't call it programatically and, if you want to know if the static constructor has been used, you'll use the class, so it could be called at this point...



As said Rango, you can use a flag in your constructor, if you really absolutly need to know...






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320375%2fc-determine-if-a-class-has-already-been-initialized%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    As said HimBromBeere, and as confirmed in this article,




    A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.




    So you need to use your class once, a property, a method, whatever, to initialize it... You have no control on it, you can't call it programatically and, if you want to know if the static constructor has been used, you'll use the class, so it could be called at this point...



    As said Rango, you can use a flag in your constructor, if you really absolutly need to know...






    share|improve this answer





























      0














      As said HimBromBeere, and as confirmed in this article,




      A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.




      So you need to use your class once, a property, a method, whatever, to initialize it... You have no control on it, you can't call it programatically and, if you want to know if the static constructor has been used, you'll use the class, so it could be called at this point...



      As said Rango, you can use a flag in your constructor, if you really absolutly need to know...






      share|improve this answer



























        0












        0








        0







        As said HimBromBeere, and as confirmed in this article,




        A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.




        So you need to use your class once, a property, a method, whatever, to initialize it... You have no control on it, you can't call it programatically and, if you want to know if the static constructor has been used, you'll use the class, so it could be called at this point...



        As said Rango, you can use a flag in your constructor, if you really absolutly need to know...






        share|improve this answer















        As said HimBromBeere, and as confirmed in this article,




        A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.




        So you need to use your class once, a property, a method, whatever, to initialize it... You have no control on it, you can't call it programatically and, if you want to know if the static constructor has been used, you'll use the class, so it could be called at this point...



        As said Rango, you can use a flag in your constructor, if you really absolutly need to know...







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 2 at 11:57

























        answered Mar 2 at 11:50









        Cerebro 6363Cerebro 6363

        365




        365





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320375%2fc-determine-if-a-class-has-already-been-initialized%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20