6

問題 1:

C++/CLI でコンパイル時ではなく実行時にライブラリを明示的にロードする方法はありますか。現在、コンパイル時に.NETの「参照の追加」を使用しています。管理された dll を明示的にロードしたいと思います。LoadLibrary に相当する .NET はありますか?

更新:ランドルフォに感謝

MSDNの Assembly::LoadFrom の例

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 );
}

問題 2:

Assembly::LoadFrom が LoadLibrary と同等の .NET である場合。GetProcAddress に相当するものは何ですか? メソッドへの FunctionPointers を作成するにはどうすればよいですか?

更新: MSDNの MethodBase.Invoke

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();
}
4

2 に答える 2

2

マネージDLLと言いましたか?その後、あなたがしたいですAssembly::Load

于 2010-06-23T14:40:12.950 に答える
0

Reflection の詳細については、http://www.informit.com/articles/article.aspx?p=25948を参照してください。あなたが探しているチケットかもしれません(問題の詳細を知らずに、言うのは難しいです)

アセンブリを動的にロードし、それらをプローブしてメソッドやプロパティなどを見つけることに関するセクション全体があります。

于 2010-06-23T14:44:52.463 に答える