まずは、実行用のフォームの処理です。
- namespace WindowsApplication1;
- interface
- uses
- System.Drawing,
- System.Collections,
- System.Collections.Generic,
- System.Linq,
- System.Windows.Forms,
- System.ComponentModel,
- System.Reflection,
- SakaTestSpace;
- type
- /// <summary>
- /// Summary description for MainForm.
- /// </summary>
- MainForm = partial class(System.Windows.Forms.Form)
- private
- method button1_Click(sender: System.Object; e: System.EventArgs);
- protected
- method Dispose(disposing: Boolean); override;
- public
- constructor;
- end;
- implementation
- {$REGION Construction and Disposition}
- constructor MainForm;
- begin
- //
- // Required for Windows Form Designer support
- //
- InitializeComponent();
- //
- // TODO: Add any constructor code after InitializeComponent call
- //
- end;
- method MainForm.Dispose(disposing: Boolean);
- begin
- if disposing then begin
- if assigned(components) then
- components.Dispose();
- //
- // TODO: Add custom disposition code here
- //
- end;
- inherited Dispose(disposing);
- end;
- {$ENDREGION}
- method MainForm.button1_Click(sender: System.Object; e: System.EventArgs);
- var
- SakaTestIntf : SakaTestSpace.ISakaTest;
- CreateClassName : String;
- t : System.Type;
- obj : System.Object;
- Assem : System.Reflection.Assembly;
- begin
- //使用するアセンブリをロードする
- Assem := System.Reflection.Assembly.Load('ClassLibrary1');
- t := Assem.GetType('SakaTestSpace.' + textBox1.Text);
- obj := Activator.CreateInstance(t);
- if obj <> nil then
- begin
- SakaTestIntf := (obj As SakaTestSpace.ISakaTest);
- if SakaTestIntf <> nil then
- begin
- label3.Text := SakaTestIntf.SayMessage();
- end;
- end;
- end;
- end.
次に検証に使ったクラスです。
- namespace SakaTestSpace;
- interface
- uses
- System.Collections.Generic,
- System.Linq,
- System.Text;
- type
- ISakaTest = public interface
- method SayMessage : String;
- end;
- type
- TSakaTest = public class(ISakaTest)
- private
- protected
- public
- method SayMessage : String ; virtual;
- end;
- TSakaTest1 = public class(TSakaTest)
- private
- protected
- public
- method SayMessage : String; override;
- end;
- TSakaTest2 = public class(TSakaTest)
- private
- protected
- public
- method SayMessage : String; override;
- end;
- implementation
- method TSakaTest.SayMessage : String;
- begin
- Result := "";
- end;
- method TSakaTest1.SayMessage : String;
- begin
- Result := 'Delphi Prism First Message';
- end;
- method TSakaTest2.SayMessage : String;
- begin
- Result := 'Delphi Prism Second Message';
- end;
- end.
PrismのほうがActivatorクラスが使える分、楽だ(短い処理で実現可能)と思う
0 件のコメント:
コメントを投稿