VC++ (MFC を使用) と C++/CLI クラスの両方を含む混合モード アセンブリがあります。これは MFC 拡張 dll であり、実行時に MFC 実行可能ファイルに読み込まれ、すべて正常に動作します。
別の C++/CLI アセンブリからそこにあるアンマネージ クラスの単体テストを行うと、アンマネージ クラスのインスタンスを (new 経由で) 作成しようとするたびに、次の例外が発生します。
Exception
System.TypeInitializationException: The type initializer for '<Module>' threw an exception. ---> <CrtImplementationDetails>.ModuleLoadExceptionHandlerException: A nested exception occurred after the primary exception that caused the C++ module to fail to load.
---> System.Runtime.Serialization.SerializationException: Serialization error.
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at <CrtImplementationDetails>.DoCallBackInDefaultDomain(IntPtr function, Void* cookie)
at <CrtImplementationDetails>.DoCallBackInDefaultDomain(IntPtr , Void* )
at <CrtImplementationDetails>.LanguageSupport.InitializeDefaultAppDomain(LanguageSupport* ) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 518
at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* ) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 721
at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* ) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 875
--- End of inner exception stack trace ---
at <CrtImplementationDetails>.ThrowNestedModuleLoadException(Exception innerException, Exception nestedException)
at <CrtImplementationDetails>.ThrowNestedModuleLoadException(Exception , Exception )
at <CrtImplementationDetails>.LanguageSupport.Cleanup(LanguageSupport* , Exception innerException) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 841
at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* ) in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 883
at .cctor() in f:\dd\vctools\crt_bld\self_x86\crt\src\mstartup.cpp:line 922
--- End of inner exception stack trace ---
at MSMContactDataTests.LoadUnknownItemTest()
これは、テスト ランナー (この場合は Gallio.Echo) によるアセンブリの読み込みに失敗したようです。
小さな C++/CLI コンソール アプリも作成し、効果的に同じことを試しました。アセンブリに含まれる ref クラスのインスタンスを正しく作成できますが、アンマネージ クラスを新しく作成しようとすると、同じ例外が発生します。
何か案は?
編集
ここで壊れたコンソール アプリのコードを投稿するつもりでしたが、再コンパイルしたところ、動作するようになりました。ここにあります:
#include "stdafx.h"
using namespace System;
#include "SCacheAssignment.h"
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
SCacheAssignment* assignment = new SCacheAssignment(NULL, false);
assignment->id = 2;
Console::WriteLine(L"Hello World 2 " + assignment->id);
return 0;
}
彼のコードを単体テストとして使用する場合:
#include "stdafx.h"
#include "PBSMSDataStoreUnitTests2.h"
#include "SCacheAssignment.h"
using namespace PBSMSDataStoreUnitTests2;
void Class1::SomeTest()
{
Console::WriteLine(L"Hello World");
SCacheAssignment* assignment = new SCacheAssignment(NULL, false);
assignment->id = 2;
Console::WriteLine(L"Hello World 2 " + assignment->id);
}
壊れます。これは、テスト アセンブリ内の唯一のテストです。
コンソール アプリは、テスト アセンブリが CLR クラス ライブラリにある CLR コンソール アプリケーションです。私が知る限り、それらは同じコンパイラ オプションを使用しています。1 つは機能し、もう 1 つは機能しないのはなぜですか?