|
To | Do this |
Modify the assembly-level attributes in a class. | Edit the entries in the AssemblyInfo.cpp file that is generated for all managed C++ projects in Visual Studio .NET. |
Find out about the standard attributes of a type. | Use the Attributes property on a Type object that represents the type, and use the bitwise AND operator (&) to compare the value with members of the TypeAttributes enumeration. For example: if (t->Attributes & TypeAttributes::Public) |
Create a custom attribute. | Create a class to represent an attribute, and use the AttributeUsage attribute to control where your attribute can be applied. For example: [AttributeUsageAttribute( AttributeTargets::Method)] public __gc class MyAttribute { ... }; |
Represent mandatory parameters for a custom attribute. | Add arguments to the class constructor or constructors plus read-only properties to give access to the values. |
Represent optional parameters for a custom attribute. | Add a property to represent each optional parameter. |
Find out which custom attributes are attached to a class. | Create a Type object and use its GetCustomAttributes method to retrieve an array of objects representing the attributes attached to the class. For example: Type* tt = myObject->GetType(); Object* patts[] = tt->GetCustomAttributes(true); |
Find out which custom attributes are attached to a class member. | Create a Type object and use its GetMembers method to retrieve an array of MemberInfo objects representing the class members. Then call GetCustomAttributes on each MemberInfo object. For example: Type* tt = myObject->GetType(); MemberInfo* pmi[] = tt->GetMembers(); for (int i=0; i<pmi->Count; i++) { Object* pMemberAtts[] = pmi[i]->GetCustomAttributes(true); if (pMemberAtts->Count > 0) { // Do something } { |
|