how to use class attributes

Rudolf Meier 271 Reputation points
2021-12-13T20:03:31.607+00:00

in .net languages a class can have "attributes"... I have one, that has this attribute

[System::Runtime::InteropServices::GuidAttribute("7F7CD0DB-91EF-49dc-9FA9-02D128515DD4")]

... I need this value in my code. How can I access it in the C++/CLI language? MyClass[GuidAttribute]? MyClass:GuidAttribute? [GuidAttribute]MyClass? how is the syntax? ... I've no idea...

Rudolf

.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
322 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,523 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 112K Reputation points
    2021-12-13T21:31:35.127+00:00

    Check an idea:

    [System::Runtime::InteropServices::GuidAttribute("7F7CD0DB-91EF-49dc-9FA9-02D128515DD4")]
    ref class MyClass1
    {
    
    };
    
    . . .
    
    using namespace System::Runtime::InteropServices;
    
    auto type = MyClass1::typeid; // or 'obj->GetType( )' if you have an object
    auto attribututes = type->GetCustomAttributes( GuidAttribute::typeid, false);
    
    if( attribututes->Length > 0)
    {
        auto attribute = (GuidAttribute^)attribututes[0];
    
        String^ guid = attribute->Value;
    
        // the Guid attribute was found
    
    }
    
    0 comments No comments