使用 Activator.CreateInstance() 创建的对象不会等待整个对象在 Release 上初始化

Objects created with Activator.CreateInstance() do not wait for the entire object to be initialized on Release

我的主要项目使用设备加载库并为它们创建实例。在Debug模式下发布后,项目运行正常,在Release模式下发布时出现问题。我不知道为什么,但它不会等待它创建的对象进行初始化。

这是一个代码片段:

try
{
    AssemblyName name = AssemblyName.GetAssemblyName(module);
    Type[] iLoadTypes = (from t in Assembly.Load(name).GetExportedTypes()
                         where !t.IsInterface && !t.IsAbstract
                         where typeof(IDevice).IsAssignableFrom(t)
                         select t).ToArray();
    if (iLoadTypes.Length > 0)
    {
        IDevice[] instantiatedDevices =
            iLoadTypes.Select(t => (IDevice)Activator.CreateInstance(t)).ToArray();
        foreach (var device in instantiatedDevices)
        {
            Devices.Add(device);
        }
    }
    iLoadTypes = (from t in Assembly.Load(name).GetExportedTypes()
                  where !t.IsInterface && !t.IsAbstract
                  where typeof(IDeviceToolProvider).IsAssignableFrom(t)
                  select t).ToArray();
    IDeviceToolProvider[] instantiatedProviders =
        iLoadTypes.Select(t => (IDeviceToolProvider)Activator.CreateInstance(t)).ToArray();
    foreach (var provider in instantiatedProviders)
    {
        var device = Devices.Find(dev => dev.GetType() == provider.DeviceType);
        if (device == null)
            continue;
        provider.Device = device;
        if (!DeviceToolProviders.ContainsKey(device))
            DeviceToolProviders[device] = new List<IDeviceToolProvider>();
        DeviceToolProviders[device].Add(provider);
        provider.Initialize();
    }
}
catch (Exception ex)
{
}

创建的对象之一:

public class WZWCDeviceTesterToolProvider : IDeviceToolProvider
{
    internal static readonly UserSettingEntry<ConnectionDescriptorBuilder> ConnectorDescriptorBuilderEntry =
        UserSettingEntry.Register("ConnectorDescriptorBuilderEntry", typeof(WZWCDeviceTesterToolProvider),
            new UserSettingEntryMetadata<ConnectionDescriptorBuilder>());

    public string Name { get { return "WZW"; } } 

    ...   
    public void Initialize()
    {

    }
}

并且它没有等待属性 ConnectorDescriptorBuilderEntry。我做了一个测试并将此初始化添加到 Initialize() 方法,但它也没有在那里初始化。它只会在一段时间后发生。

我也不知道怎么弄的,不过以前用过,这个项目是我从别人那里接过来的。我没有更改主项目中的任何内容。我刚刚添加了一个新设备。它在初始化期间不会抛出任何错误。它最终会初始化所有内容。这里没有异步。

净 4.5

我不知道发布的代码量是否足以解决我的问题,但也许有人可以告诉我在哪里寻找解决方案以及为什么它适用于 Debug 而不是 Release?

根据 C# specification,如果没有静态构造函数,则无法保证静态字段何时准确初始化,只能在首次使用之前发生。

15.5.6.2 Static field initialization

The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration (§15.5.6.1). Within a partial class, the meaning of "textual order" is specified by §15.5.6.1. If a static constructor (§15.12) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class.

但是,如果有一个静态构造函数,则可以保证所有静态字段在它运行之前都已初始化,如果您实例化 class 的对象或访问静态成员,则可以保证发生这种情况第一次。

所以要强制初始化你的字段,你可以添加一个静态构造函数:

static WZWCDeviceTesterToolProvider()
{
}