C#中的非延迟静态初始化块

时间:2010-12-21 04:46:08

标签: c# static initialization

我需要运行一些代码来注册工厂模式的类型。我会在Java中使用静态初始化块或在带有静态构造函数的C ++中执行此操作。

你如何在C#中做到这一点?那个静态构造函数懒得运行,因为代码中永远不会引用该类型,所以永远不会注册。

编辑:我尝试过测试,看看注册码是否有效。这似乎不起作用。

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)
        {
        }
    }
}

什么都没打印出来。

1 个答案:

答案 0 :(得分:4)

assembly level attribute的构造函数中怎么样?

实施例

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

    public RegisterToFactoryAttribute(Type typeToRegister)
    {
        TypeToRegister = typeToRegister;

        // Registration code
    }
}

用法:

[assembly:RegisterToFactory(typeof(MyClass))]

- 对装配级别属性进行编辑 -

在做了一些研究之后,我发现它只会在查询时加载程序集属性:

实施例

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

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

不知道为什么,但是将这段代码加载到程序加载中应该这样做。

- 编辑 -

我差点忘了:

您是否考虑过使用MEF?这是解决这个问题的好方法。

实施例

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
{ }