C++/CLI Explizit Laden Verwaltete DLL-Datei zur Laufzeit (das äquivalent von LoadLibrary für Standalone)

Problem 1:

Gibt es eine Möglichkeit, explizit laden einer Bibliothek zur Laufzeit statt bei der Kompilierung in C++/CLI. Momentan benutze ich die .NET "Referenz Hinzufügen" zur compile-Zeit.
Ich möchte ausdrücklich laden Sie eine verwaltete dll. Gibt es die .NET-äquivalent von LoadLibrary?

Update: Dank Randolpho

Montage::LoadFrom Beispiel aus MSDN

Assembly^ SampleAssembly;
SampleAssembly = Assembly::LoadFrom( "c:\\Sample.Assembly.dll" );
//Obtain a reference to a method known to exist in assembly.
MethodInfo^ Method = SampleAssembly->GetTypes()[ 0 ]->GetMethod( "Method1" );
//Obtain a reference to the parameters collection of the MethodInfo instance.
array<ParameterInfo^>^ Params = Method->GetParameters();
//Display information about method parameters.
//Param = sParam1
//  Type = System::String
//  Position = 0
//  Optional=False
for each ( ParameterInfo^ Param in Params )
{
   Console::WriteLine( "Param= {0}", Param->Name );
   Console::WriteLine( "  Type= {0}", Param->ParameterType );
   Console::WriteLine( "  Position= {0}", Param->Position );
   Console::WriteLine( "  Optional= {0}", Param->IsOptional );
}

Problem 2:

Wenn Montage::LoadFrom ist .NET-äquivalent von LoadLibrary. Was ist das äquivalent von GetProcAddress? Wie erstelle ich FunctionPointers zu den Methoden?

Update: MethodBase.Aufrufen von MSDN

using namespace System;
using namespace System::Reflection;

public ref class MagicClass
{
private:
    int magicBaseValue;

public:
    MagicClass()
    {
        magicBaseValue = 9;
    }

    int ItsMagic(int preMagic)
    {
        return preMagic * magicBaseValue;
    }
};

public ref class TestMethodInfo
{
public:
    static void Main()
    {
        //Get the constructor and create an instance of MagicClass

        Type^ magicType = Type::GetType("MagicClass");
        ConstructorInfo^ magicConstructor = magicType->GetConstructor(Type::EmptyTypes);
        Object^ magicClassObject = magicConstructor->Invoke(gcnew array<Object^>(0));

        //Get the ItsMagic method and invoke with a parameter value of 100

        MethodInfo^ magicMethod = magicType->GetMethod("ItsMagic");
        Object^ magicValue = magicMethod->Invoke(magicClassObject, gcnew array<Object^>(1){100});

        Console::WriteLine("MethodInfo.Invoke() Example\n");
        Console::WriteLine("MagicClass.ItsMagic() returned: {0}", magicValue);
    }
};

int main()
{
    TestMethodInfo::Main();
}
InformationsquelleAutor | 2010-06-23
Schreibe einen Kommentar