-11

これを参照してください...

http://docs.oracle.com/javase/tutorial/reflect/index.htmlまたは リフレクションとは何ですか?なぜそれが役立つのですか?

.Net プラットフォームで利用できるこのようなものはありますか?

4

1 に答える 1

2

リフレクションは、アセンブリ、モジュール、および型をカプセル化する (Type 型の) オブジェクトを提供します。リフレクションを使用して、型のインスタンスを動的に作成したり、型を既存のオブジェクトにバインドしたり、既存のオブジェクトから型を取得してそのメソッドを呼び出したり、そのフィールドやプロパティにアクセスしたりできます

リフレクションを使用してアセンブリを動的にロードし、リフレクションを使用してそのフォームを表示しました。

Step 1: Loading an assembly form the specified path.

string path = Directory.GetCurrentDirectory() + @"\dynamicdll.dll";
try
{
    asm = Assembly.LoadFrom(path);
}
catch (Exception)
{
}

Step 2: Getting all forms of an assembly dynamically & adding them to the list type.

List<Type> FormsToCall = new List<Type>();
Type[] types = asm.GetExportedTypes();
foreach (Type t in types)
{
    if (t.BaseType.Name == "Form")
        FormsToCall.Add(t);
}

Step 3:

int FormCnt = 0;
Type ToCall;
while (FormCnt < FormsToCall.Count)
{
     ToCall = FormsToCall[FormCnt];
     //Creates an instance of the specified type using the constructor that best matches the specified parameters.
     object ibaseObject = Activator.CreateInstance(ToCall);
     Form ToRun = ibaseObject as Form;
     try
     {
          dr = ToRun.ShowDialog();
          if (dr == DialogResult.Cancel)
          {
             cancelPressed = true;
             break;
          }
          else if (dr == DialogResult.Retry)
          {
             FormCnt--;
             continue;
          }
     }
     catch (Exception)
     {
     }
     FormCnt++;
}
于 2012-10-05T13:32:24.707 に答える