2009年8月31日月曜日

クラスのインスタンスを動的に作成する。

Delphi2010と同じ動きをするDelphi Prismのソースです。

まずは、実行用のフォームの処理です。


namespace WindowsApplication1;

interface

uses
System.Drawing,
System.Collections,
System.Collections.Generic,
System.Linq,
System.Windows.Forms,
System.ComponentModel,
System.Reflection,
SakaTestSpace;

type
///
/// Summary description for MainForm.
///

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クラスが使える分、楽だ(短い処理で実現可能)と思う

2009年8月29日土曜日

Rttiを試して見るその2(プリズム版)

別ブログで書いたDelphi2010のメソッドコール
ソースと同等のソースをDelphi Prismで書いてみた。


namespace WindowsApplication1;

interface

uses
System.Drawing,
System.Collections,
System.Collections.Generic,
System.Linq,
System.Windows.Forms,
System.ComponentModel,
System.Reflection;


type
///
/// Summary description for MainForm.
///

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;
method SakaSum(a, b : Integer) : Integer;
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
rtm : System.Reflection.MethodInfo;
Args : Array of System.Object;
Rst : System.Object;
MyType : System.Type;
begin
MyType := System.Type.GetType('WindowsApplication1.' + Self.Name);

rtm := MyType.GetMethod('SakaSum');

if rtm <> nil Then
begin

Args := new System.Object[2];
Args[0] := Convert.ToInt32(textBox1.Text);
Args[1] := Convert.ToInt32(textBox2.Text);
Rst := rtm.Invoke(Self,Args);
label5.Text := Rst.ToString();

end;
end;

method MainForm.SakaSum(a, b : Integer) : Integer;
begin
Result := a + b;
end;

end.