【问题标题】:How to set C# class attribute into C++/CLI class如何将 C# 类属性设置为 C++/CLI 类
【发布时间】:2018-12-11 20:28:50
【问题描述】:

我想将 testcase 属性设置到我的 c++/cli 类中。我正在为该属性添加 dll 引用。请注意,我在 C# 中用于属性设置的 dll。请找到以下代码:

using namespace Sample::Generic::TestAutomation::TestSystem;
namespace ClassLibrary1 {

[AttributeUsage(Sample::Generic::TestAutomation::TestSystem::TestCase)]
    public ref class Class1 : public Sample::CheckIt::TestPool::TestCase
    {
    private:
        static Class1^ Instance = nullptr;
        GenericLogger^ Switchmodule_log;
        TAIoPortClient^ _generalio;
        bool _testCaseInit = false;
   };

当我尝试使用此属性时,出现以下错误:

错误 C2275 'Sample::Generic::TestAutomation::TestSystem::TestCase': 非法使用这种类型作为 表达式 ClassLibrary1 c:\users\chan\documents\visual studio 2015\projects\classlibrary1\classlibrary1\ClassLibrary1.h 17

【问题讨论】:

    标签: c# attributes c++-cli


    【解决方案1】:

    AttributeUsage 用于定义新属性时。当您定义 MyFancyNewAttribute 时,您使用AttributeUsage 来声明该属性是用于类、方法还是其他任何东西。

    你想多了。要使用该属性,它的语法与 C# 中的语法相同。

    [TestCase]
    public ref class Class1
    

    【讨论】:

    • 感谢大卫,但我给出了与上面提到的相同的方法,即适用于 C# 但不适用于 C++/CLI。当我尝试使用时,我收到此错误:“无法从 ClassLibrary1 加载 TestCases!Assembly 没有定义 TestCases”
    • 我的理解是我的测试应用无法获取测试用例属性。
    • 这是一个不同的问题。也许发布一个带有测试框架标签的新问题。
    • 我修复了更新 dll 引用的问题。它对我有用。
    猜你喜欢
    • 2012-02-09
    • 1970-01-01
    • 2023-03-17
    • 2015-12-08
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    相关资源
    最近更新 更多