我引用了一些C++/CLI代码中的第三方.NET库。我的C++/CLI调用代码引用了一个在C#库中使用Obsolete属性标记为Obsolete的属性:
// C# External library code
using System;
namespace ExternalLibrary
{
public class Dependency
{
[Obsolete("Please use the new version.")]
public static bool IsInitialized
{
get { return true; }
}
}
}
但是,当我编译使用该属性的C++/CLI调用代码时,即使我将"警告级别"设置为/Wall EnableAllWarnings
,它也不会生成任何过时/不推荐使用的警告(例如C4947)。
如果我从C#代码引用相同的API,我会收到预期的CS0618警告,告诉我该属性已过时,但当我编译C++/CLI代码时,我不会收到任何过时或弃用警告。
// C# Calling code (in another assembly)
namespace CalledFromCSharp
{
public class CSharpCode
{
public static void CallingCode()
{
// Generates warning CS0618:
// 'ExternalLibrary.Dependency.IsInitialized' is obsolete:
// 'Please use the new version.'
bool initialized = ExternalLibrary.Dependency.IsInitialized;
}
}
}
// C++/CLI Calling code (also in another assembly)
#pragma once
using namespace System;
namespace CppCode
{
public ref class CalledFromCpp
{
static void CallingCode()
{
// NO OBSOLETE WARNING GENERATED
bool isInitialized = ExternalLibrary::Dependency::IsInitialized;
};
};
}
静态和非静态属性调用似乎都会发生这种情况。我是否需要在我的C++/CLI项目(使用Visual Studio 2013)中设置一些额外的内容来显示适当的警告?或者目前的行为有原因吗?
Hmya,不想成为坏消息的传播者,但C++/CLI的设计人员对属性的处理方式不同。从他们的语法中可以明显看出,他们在许多选择中都倾向于"类似C++"的方法。最好通过修改代码片段来演示:
public ref class CalledFromCpp {
public:
static property bool Foo {
[Obsolete("This works")]
bool get() { return false; }
}
[Obsolete("This doesn't work")]
static property bool Bar {
bool get() { return false; }
}
static void CallingCode() {
bool test1 = CalledFromCpp::Foo; // Yes
bool test2 = CalledFromCpp::Bar; // Nope
bool test3 = ExternalLibrary::Dependency::IsInitialized; // Nope
}
};
你不能在C#属性上做同样的事情,它不允许你在getter上应用[Obsolete]属性。对此没有解决方法。