0

At runtime I need to be able to get my loaded C++ assemblies to register their version information with a top level application. I was thinking of something like:

     array<System::Reflection::Assembly^>^ assemblies = System::AppDomain::CurrentDomain->GetAssemblies();

     for each(System::Reflection::Assembly^ assembly in assemblies)
     {}

But how can you determine which are my assemblies and which are system assemblies. Is there some way of adding some static method to my classes that this code can call?

Here is the code I was actually using, I was trying to get the attributes of the Foo class so that I could then call a static method to request it to register itself.

public ref class Foo
{
public:

  Foo()
  {
  };

private:

};

public ref class InitOnLoad : System::Attribute 
{ 
public:

  InitOnLoad()
  {
     Foo ^foo = gcnew Foo();
     System::Type^ thisType = foo->GetType();

     // get a list of types which are marked with the InitOnLoad attribute
     array<System::Reflection::Assembly^>^ assemblies = System::AppDomain::CurrentDomain->GetAssemblies();

     for each(System::Reflection::Assembly^ assembly in assemblies)
     {
        try
        {
           System::Type^ type = System::Type::GetType("UtilsDotNet.Foo");

           array<Object^>^ attributes = assembly->GetCustomAttributes(type, false);

           if(attributes->Length > 0)
           {
              auto field =
                 type->GetFields(
                 System::Reflection::BindingFlags::Static | 
                 System::Reflection::BindingFlags::Public | 
                 System::Reflection::BindingFlags::NonPublic);
           }

        }
        catch (...)
        {
        }
     }
  }
};

1 Answer 1

1

I would add a custom attribute to your assemblies. You can test each assembly for the attribute before moving forward with version number registration.

My syntax may be wrong, but...

System::Type^ type = System::Type::GetType("UtilsDotNet.Foo");
array<Object^>^ attributes = assembly->GetCustomAttributes(type, false);

should be

array<Object^>^ attributes = assembly->GetCustomAttributes(__typeof(InitOnLoad), false);

I'm not sure of the C++ way of marking an assembly with an attribute, but the C# way would be to put this line in the Assembly.cs file (under the Properties folder)

[assembly:  InitOnLoad()]

Also, you shouldn't have the code for enumerating the assemblies in the constructor of the attribute. If you do that, this code would be run every time one of these attributes is found. I'd put that code in a separate method, perhaps a static method on the attribute.

Finally, keep in mind that if you are working with late-bound assemblies, you also need to listen to the AssemblyLoad event.

3
  • thanks for your reply. I tried to do something similar to what you mentioned but when I call GetCustomAttributes for some reason it has 0 attributes. Feb 27, 2012 at 16:28
  • Post your attribute code and your code looking for the attribute and I'll try to help you out. Feb 27, 2012 at 17:01
  • I have just updated the question with the code I was trying, when it loads I can't get GetCustomAttributes to return anything. I have tried __typeof when getting the type but that didn't work either. Feb 27, 2012 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.