Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-Lazy Static Initialization Block in C#

I need to run some code to register a type for a factory pattern. I would do this in Java with a static initialization block or in C++ with a static constructor.

How do you do this in C#? That static constructor gets run lazily and since the type will never be referred to in the code, never gets registered.

EDIT: I tried a test to see the registration code work. This doesn't seem to be working though.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

[assembly: AssemblyTest.RegisterToFactory("hello, world!")]

namespace AssemblyTest
{
    [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = true)]
    sealed class RegisterToFactoryAttribute : Attribute
    {
        public RegisterToFactoryAttribute(string name)
        {
            Console.WriteLine("Registered {0}", name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Nothing gets printed.

like image 254
Jonathan Sternberg Avatar asked Oct 13 '22 19:10

Jonathan Sternberg


1 Answers

How about in a constructor for an assembly level attribute?

Example:

[AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = true)]
sealed class RegisterToFactoryAttribute : Attribute
{
    public Type TypeToRegister { get; set; }

    public RegisterToFactoryAttribute(Type typeToRegister)
    {
        TypeToRegister = typeToRegister;

        // Registration code
    }
}

Usage:

[assembly:RegisterToFactory(typeof(MyClass))]

--EDIT on Assembly Level Attributes--

After doing some research, I figured it will only load the assembly attributes if they are queried:

Example:

object[] attributes =
    Assembly.GetExecutingAssembly().GetCustomAttributes(
        typeof(RegisterToFactoryAttribute), false);

or

object[] attributes =
    Assembly.GetExecutingAssembly().GetCustomAttributes(false);

Don't know why but putting this code @ the program load should do it.

--EDIT--

I almost forgot:

Have you considered using MEF?? It's a great solution to this problem.

Example:

class MyFactory
{
    [ImportMany("MyFactoryExport")]
    public List<Object> Registrations { get; set; }

    public MyFactory()
    {
        AssemblyCatalog catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
        CompositionContainer container = new CompositionContainer(catalog);
        container.ComposeParts(this);
    }
}

[Export("MyFactoryExport")]
class MyClass1
{ }

[Export("MyFactoryExport")]
class MyClass2
{ }

[Export("MyFactoryExport")]
class MyClass3
{ }
like image 99
decyclone Avatar answered Nov 15 '22 09:11

decyclone