locked
How to decorate my custom attribute with a function RRS feed

  • Question

  • suppose i have a routine like

    function updateFiles()
    {
    
    }

    now i want to create attribute which i will decorate above function like

    [RunOnce]
    function updateFiles()
    {
    
    }
    

    so tell me how could i develop a routine called [RunOnce] which will allow or prevent UpdateFiles routine to run?

    please help me with sample code.

    Sunday, April 28, 2019 12:10 PM

Answers

  • Hi Studip_inn,

    Thank you for posting here.

    For your question, if you want to create attribute, you could try the code below.

     class Program
        {
            static void Main(string[] args)
            {
    
    
            }
            [RunOnce]
            public static void Method()
            {
    
            }
            [RunOnce(RunOnceText = "Hello")]
            public static void Method2()
            {
    
            }
        }
        class RunOnce : Attribute
        {
            public string RunOnceText { get; set; }
    
        }

    For more details about attribute, you could refer to the link below.

    https://www.codeproject.com/Articles/827091/Csharp-Attributes-in-minutes

    >>so tell me how could i develop a routine called [RunOnce] which will allow or prevent UpdateFiles routine to run?

    Sorry for that, as I know, you could not do that.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Monday, April 29, 2019 7:00 PM
    Monday, April 29, 2019 8:57 AM

All replies

  • so tell me how could i develop a routine called [RunOnce] which will allow or prevent UpdateFiles routine to run?

    No, you can't do that with an attribute. You can write code that reads the attribute using Reflection. Then that code can make a decision based on what it has read. So if you are calling updateFiles from somewhere, then that somewhere can make a decision about whether to call the function based on its attribute. But the function itself will not be affected by the presence of the atribute. The attribute is not "triggered" or "executed" in any way when the function is invoked.
    Sunday, April 28, 2019 2:31 PM
  • Hi Studip_inn,

    Thank you for posting here.

    For your question, if you want to create attribute, you could try the code below.

     class Program
        {
            static void Main(string[] args)
            {
    
    
            }
            [RunOnce]
            public static void Method()
            {
    
            }
            [RunOnce(RunOnceText = "Hello")]
            public static void Method2()
            {
    
            }
        }
        class RunOnce : Attribute
        {
            public string RunOnceText { get; set; }
    
        }

    For more details about attribute, you could refer to the link below.

    https://www.codeproject.com/Articles/827091/Csharp-Attributes-in-minutes

    >>so tell me how could i develop a routine called [RunOnce] which will allow or prevent UpdateFiles routine to run?

    Sorry for that, as I know, you could not do that.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Monday, April 29, 2019 7:00 PM
    Monday, April 29, 2019 8:57 AM