CLI casting RRS feed

  • Question

  • I have Three CLI projects: SharedInterface, MyLibrary and TestConsoleApp.

    There is one interface in SharedInterface and one class in MyLibrary that implements this interface.

    TestConsoleApp loads the MyLibrary.dll and try to cast Object to Interface, but can not.

    Please help.

    Here is the snippet:

    String^ path = Path::GetDirectoryName(Assembly::GetExecutingAssembly()->Location) + "\\MyLibrary.dll";
    Assembly^ assy = Assembly::LoadFrom(path);
    for each (Type^ t in assy->GetTypes())
       if (t->IsAbstract || !t->IsPublic) { continue; }
       //Getting Ex "unable to cast Class1 to IManager" here
       IManager^ mgr = safe_cast<IManager^>(Activator::CreateInstance(t));

    Friday, July 16, 2010 8:05 PM