Search code examples
c#attributesc++-cli

How to set C# class attribute into C++/CLI class


I want to set testcase attribute into my c++/cli class. i am adding the dll reference for that attribute. Note here that the dll which i am using for attribute setup which is in C#. please find below code:

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;
   };

When i am trying to use this attribute, i am getting below error:

Error C2275 'Sample::Generic::TestAutomation::TestSystem::TestCase': illegal use of this type as an expression ClassLibrary1 c:\users\chan\documents\visual studio 2015\projects\classlibrary1\classlibrary1\ClassLibrary1.h 17


Solution

  • AttributeUsage is for when you define a new attribute. When you define MyFancyNewAttribute, you use AttributeUsage to declare whether the attribute is used on classes, or methods, or whatever.

    You're overthinking this. To use the attribute, it's the same syntax as in C#.

    [TestCase]
    public ref class Class1