[C#] クラスの動的生成あれこれ(その4)

今日で Activator.CreateInstance 編は終了。
この関数は9つオーバーロードされているが、もう今までの説明ででてきたやつで、おおかた終わり。残りは一応の紹介程度にとどめておくことにする。

  • Activator.CreateInstance (Type, BindingFlags, Binder, Object, CultureInfo, Object) のサンプル


object[] activationAttributes = {new System.Runtime.Remoting.Activation.UrlAttribute("tcp://localhost:8085/HelloService")};

RemotingSamples.HelloServer hs =
System.Activator.CreateInstance
(
typeof(RemotingSamples.HelloServer),
BindingFlags.CreateInstance,
null,
null,
new System.Globalization.CultureInfo("ja-JP"),
activationAttributes
) as RemotingSamples.HelloServer;

hs.HelloMethod("foo");

  • Activator.CreateInstance (string, string, bool, BindingFlags, Binder, object, CultureInfo, object, Evidence) のサンプル


Assembly assem = Assembly.GetAssembly(typeof(HelloServer));
object[] activationAttributes = {new System.Runtime.Remoting.Activation.UrlAttribute("tcp://localhost:8085/HelloService")};

System.Runtime.Remoting.ObjectHandle oh =
System.Activator.CreateInstance
(
assem.FullName,
"RemotingSamples.HelloServer",
false, // true にすると、HelloServer -> helloserver でもOKになる
BindingFlags.CreateInstance,
null,
null,
new System.Globalization.CultureInfo("ja-JP"),
activationAttributes,
null // 証拠オブジェクト、、、よくわからん(^^;
);

HelloServer hs = oh.Unwrap() as HelloServer;
hs.HelloMethod("fuga");