编译器警告(等级 1)C4691

“type”:未引用的程序集“file”中应使用引用的类型,但使用了当前翻译单元中定义的类型

未引用包含初始类型定义的元数据文件,并且编译器正在使用本地类型定义。

在要重新生成 file 的情况下,可使用 pragma 警告忽略或关闭 C4691。 也就是说,如果正在生成的文件是编译器应查找类型定义的文件,则可以忽略 C4691。

但是,如果编译器使用的定义不来自元数据中引用的程序集,则可能会发生非预期行为;CLR 类型不仅按类型名称划分类型,还按程序集划分类型。 也就是说,程序集 z.dll 中的类型 Z 与程序集 y.dll 中的类型 Z 不同。

示例

此示例包含初始类型定义。

// C4691_a.cpp
// compile with: /clr /LD /W1
public ref class Original_Type {};

此示例引用 C4691_a.dll 并声明类型字段 Original_Type。

// C4691_b.cpp
// compile with: /clr /LD
#using "C4691_a.dll"
public ref class Client {
public:
   Original_Type^ ot;
};

以下示例生成 C4691。 请注意,此示例包含 Original_Type 的定义并且不引用 C4691a.dll。

要解决此问题,请引用包含初始类型定义的元数据文件并删除本地声明和定义。

// C4691_c.cpp
// compile with: /clr /LD /W1
// C4691 expected

// Uncomment the following line to resolve.
// #using "C4691_a.dll"
#using "C4691_b.dll"

// Delete the following line to resolve.
ref class Original_Type;

public ref class MyClass : Client {};