Jon*_*erg 5 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)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
什么都没打印出来.
在 an 的构造函数中怎么样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
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
[assembly:RegisterToFactory(typeof(MyClass))]
Run Code Online (Sandbox Code Playgroud)
经过一些研究后,我认为它只会在查询时加载程序集属性:
object[] attributes =
Assembly.GetExecutingAssembly().GetCustomAttributes(
typeof(RegisterToFactoryAttribute), false);
Run Code Online (Sandbox Code Playgroud)
或者
object[] attributes =
Assembly.GetExecutingAssembly().GetCustomAttributes(false);
Run Code Online (Sandbox Code Playgroud)
不知道为什么,但将此代码放在程序加载中应该可以做到。
- 编辑 -
我差点忘了:
您考虑过使用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
{ }
Run Code Online (Sandbox Code Playgroud)