4

我正在使用基于 .net 4.5 的 Visual Studio 2013 创建一个新的 dll 应用程序。当尝试Guid像这样在我的类上定义属性时:

[Guid("4245366B-0484-4A41-A2E8-C7D9FC3A4ED7")]

编译器给了我错误

'System.Guid' 不是属性类。

知道缺少什么吗?

4

2 回答 2

5

您必须添加对 的引用System.Runtime.InteropServices,如下所示:

using System.Runtime.InteropServices;

或说出班级的全名:

[System.Runtime.InteropServices.Guid("4245366B-0484-4A41-A2E8-C7D9FC3A4ED7")]

或使用带有后缀的类名Attribute

[GuidAttribute("4245366B-0484-4A41-A2E8-C7D9FC3A4ED7")]

或使用带有后缀的完整类名Attribute

[System.Runtime.InteropServices.GuidAttribute("4245366B-0484-4A41-A2E8-C7D9FC3A4ED7")]

您可以在MSDN 文章中找到更多信息

于 2015-01-13T16:15:08.040 回答
4

您应该包含正确的命名空间或using语句。如果您不这样做,它将匹配System.Guid(而不是,为方便起见System.Runtime.InteropServices.GuidAttribute,该Attribute部分被剥离),这确实不是一个属性。这有点令人困惑,但确实...

此代码将带您到那里:

[System.Runtime.InteropServices.Guid("4245366B-0484-4A41-A2E8-C7D9FC3A4ED7")]
于 2015-01-13T16:15:24.220 回答