C#库是从c ++ / cli代码中使用的。在库中是一个包含Nullable类型的私有字段的类,其中Struct对于类是私有的。当从c ++ / cli代码访问此类的任何方法时,会出现编译错误:
2>cliuse.cpp(5): error C2248: 'csstruct::Foo::MyStruct': cannot access private class declared in class 'csstruct::Foo'
2> cliuse.cpp(5): note: see reference to class generic instantiation 'System::Nullable<csstruct::Foo::MyStruct>' being compiled
2> cliuse.cpp(5): note: This diagnostic occurred while importing type 'csstruct::Foo ' from assembly 'csstruct, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
如果struct
更改为class
(因此我不必使用Nullable),则不会发生错误,这就是修复方法,但感觉不对如果这样微妙的细节影响设计,如果有办法解决它而不改变库,那么了解它将是非常好的。
代码(简化):
C#库:
using System;
namespace csstruct
{
public class Foo
{
public static Foo Instance() { return new Foo() { myStruct = null }; }
public void Bar() { Console.Out.WriteLine("Bar: {0}", this.myStruct); }
private Nullable<MyStruct> myStruct;
private struct MyStruct
{
}
}
}
c ++ / cli代码:
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
auto obj = csstruct::Foo::Instance();
obj->Bar();
}