本文介绍了托管C ++静态构造函数不叫.NET4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近提出一个项目,我的工作从.NET 3.5我使用C#,托管C ++和非托管C ++到.NET 4。

在我的托管C ++(互操作),我有一个静态构造函数中的一个:

 公开引用类STATICPOOL:公共BaseStaticPools
{
上市:
    静态STATICPOOL()
    {
        InitializePools();
    }

    静态池化^出列()
    {
        返回(池化^)出列(池化:: typeid的);
    }

私人:
    静态无效InitializePools()
    {
        BaseStaticPools :: CreatePool(池化:: typeid的);
    }
};
 

在.NET 3.5中,一旦出列()已被要求在第一时间将触发静态初始化,它运行的静态构造函数。有一次,我搬到了.NET 4.0,静态构造函数永远不会被调用。

我知道,在静态初始化在.NET 4.0中已经改变,但根据我所读它应该很好地工作。

解决方案

在.NET中,类初始化可能只能被称为第一次现场访问。这是由 [BeforeFieldInit] 属性控制。

我提交了错误报告,该报告仅提供给测试者,尽管被标记为公开。

下面是来自微软,你可以找到有用的解释:


I've recently moved a project I'm working on from .NET 3.5 to .NET 4. I'm using C#, Managed C++ and Unmanaged C++.

In one of my Managed C++ (interop) I'm having a static constructor:

public ref class StaticPool : public BaseStaticPools
{
public:
    static StaticPool()
    {
        InitializePools();
    }

    static Poolable^ Dequeue()
    {
        return (Poolable^)Dequeue(Poolable::typeid);
    }

private:
    static void InitializePools()
    {
        BaseStaticPools::CreatePool(Poolable::typeid);
    }
};

In .NET 3.5 once Dequeue() had been called for the first time it would trigger the static initialization, which runs the static constructor. Once I moved to .NET 4.0, the static constructor was never called.

I know there have been changes in static initializations in .NET 4.0, but according to all I read it should work fine.

解决方案

In .NET, type initializers may only be called the first time a field is accessed. This is controlled by the [BeforeFieldInit] attribute.

I filed a bug report, which is only available to beta testers, despite being marked "Public".

Here's the explanation from Microsoft, which you may find helpful:


这篇关于托管C ++静态构造函数不叫.NET4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:59