2

So I want to have a class library that only exposes 2 attributes. If the user code uses those attributes on any method, I want some code to be run (preferably first thing at run-time).

The purpose is to do some checks on the methods the attributes were set on and alert the user if the checks fail.

The checks only depend on data that should in theory be available straight after building.

I don't need the code to be run ONLY if the attributes are set. It can be run anyways. I'll check if the attributes are set anywhere manually.

Static initializers on the attribute classes do nothing since the actual initializers are only run if you inspect the custom attributes.

I suppose the question is: How do I run code once if my class library is referenced if I can't use static constructors because my library only exposes attributes?

Luka Horvat
  • 4,283
  • 3
  • 30
  • 48
  • Can't you expose a static method, like `MyLibraryConfiguration.Configure()`? The user can then place that in their application startup routine. You [can't|don't want to](http://stackoverflow.com/questions/505237/net-running-code-when-assembly-is-loaded) run code when your assembly is loaded. – CodeCaster Jan 06 '14 at 13:50
  • Well, I'm aware that it's not the best idea to run code when the assembly is loaded and in any other case I could use something like that. But in my case the user was explicit enough when he assigned attributes to methods. Attributes that have only one purpose and that is to mark methods for checks when the application is run. I don't want to force the user to also call static methods manually because there isn't any reason not to run the check as soon as possible. – Luka Horvat Jan 06 '14 at 13:58

2 Answers2

2

Remember, the developer who is using/referencing the class library ultimate gets to decide at what point a piece of code is executed. Your class library can expose an Initialization method and you instructed the developer he needs to call that method first.

If you only want some code to execute once, you need to follow the Singleton design pattern.

using System;

public class Class1
{
    private static readonly Class1 _myInstance = new Class1();

    private Class1()
    {
        // do your once custom code here
        // and possible do reflection to check if your custom attributes
        // are in use

    }

    public static Class1 GetInstance()
    {
        get {
            return _myInstance;
        }
    }

}
Black Frog
  • 11,595
  • 1
  • 35
  • 66
  • When do you think the static constructor would be called? – Steve Ruble Jan 06 '14 at 13:43
  • Unfortunately, if I don't use that class in user code, it's symbols aren't even loaded so the constructor doesn't run. – Luka Horvat Jan 06 '14 at 13:46
  • The common language runtime takes care of the variable initialization, but not until the class is first referenced. – Black Frog Jan 06 '14 at 13:51
  • let's say you have a piece of information like a database connection string, and you have 5 classes that all need this information in your library. do you expect a user to call init on each class, or is there a better way? – Julien Apr 10 '15 at 18:44
  • In this example, you would not call `init` at all. You could call the `GetInstance()` function which would take care of initializing the Singleton one time. Then you call the function that returns the connection string or `SQLConnection` object. – Black Frog Apr 10 '15 at 19:25
-1

Are you looking for this:

public class MyClass
{
    private static string _prop1;
    public static string Prop1
    {
        get
        {
            // Your initial code to run whenever value is retrived (like........... something = Prop2;)
            // { 
            //   code block
            // }
            return _prop1;
        }
        set
        {
            // Your initial code to run whenever something is assigned (like........... Prop2 = something;)
            // { 
            //   code block
            // }
            _prop1 = value;
        }
    }

    private static string _prop2;
    public static string Prop2
    {
        get
        {
            // Your initial code to run whenever value is retrived (like........... something = Prop2;)
            // { 
            //   code block
            // }
            return _prop2;
        }
        set
        {
            // Your initial code to run whenever something is assigned (like........... Prop2 = something;)
            // { 
            //   code block
            // }
            _prop2 = value;
        }
    }
}
Khurram Hassan
  • 1,494
  • 1
  • 13
  • 21