「メタプログラミング」の編集履歴(バックアップ)一覧はこちら
「メタプログラミング」(2008/06/21 (土) 09:58:29) の最新版変更点
追加された行は緑色になります。
削除された行は赤色になります。
&bold(){※下記のサンプルデータを対象にしたものです}
&bold(){private フィールドに値を入れる}
/// <summary>
/// private フィールドに値を突っ込むサンプル
/// </summary>
private void SetPrivateValue()
{
Generater gene = new Generater();
Type type = typeof(Generater);
FieldInfo info = type.GetField("pValue", BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Instance);
info.SetValue(gene, "できた!!!");
Console.WriteLine(gene.GetValue());
}
&bold(){戻り値無しのprivate メソッドの実行サンプル}
/// <summary>
/// 戻り値無しのprivate メソッドの実行サンプル
/// </summary>
private void ExecPrivateMethod()
{
Generater gene = new Generater();
Type type = typeof(Generater);
MethodInfo method = type.GetMethod("GetData", BindingFlags.NonPublic | BindingFlags.Instance, null
, new Type[] { typeof(string), typeof(DateTime), typeof(MethodBase) }, null);
MethodBase methodBase = null;
method.Invoke(gene, new object[] { "できるかな?", DateTime.Now, methodBase });
}
&bold(){戻り値有りのprivate メソッドの実行サンプル}
/// <summary>
/// 戻り値有りのprivate メソッドの実行サンプル
/// </summary>
private void ExexPrivateMethodReturn()
{
Generater gene = new Generater();
Type type = typeof(Generater);
MethodInfo method = type.GetMethod("GetData", BindingFlags.NonPublic | BindingFlags.Instance, null
, new Type[] { typeof(string), typeof(object) }, null);
object obj = method.Invoke(gene, new object[] { "戻り値希望!", null });
if (obj == null) Console.WriteLine("失敗");
Console.WriteLine(((EventMessage)obj).time.ToString());
}
&bold(){サンプルデータクラス}
public class EventMessage
{
public string val = "data";
public string Daata = "data";
public DateTime time = DateTime.Now;
}
public class Generater
{
private int Count;
private string pValue;
Generater s_Instance;
public Generater()
{
s_Instance = this;
}
public string GetValue()
{
return pValue;
}
public void GetData(string val)
{
Console.WriteLine(val);
}
public void GetData(string val, DateTime date)
{
Console.WriteLine(val);
}
private EventMessage GetData(string val, object obj)
{
EventMessage e = new EventMessage();
return e;
}
private void GetData(string val, DateTime date, MethodBase method)
{
Console.WriteLine(val);
}
}
&bold(){※下記のサンプルデータを対象にしたものです}
&bold(){※using System.Reflection;}
&bold(){private フィールドに値を入れる}
/// <summary>
/// private フィールドに値を突っ込むサンプル
/// </summary>
private void SetPrivateValue()
{
Generater gene = new Generater();
Type type = typeof(Generater);
FieldInfo info = type.GetField("pValue", BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Instance);
info.SetValue(gene, "できた!!!");
Console.WriteLine(gene.GetValue());
}
&bold(){戻り値無しのprivate メソッドの実行サンプル}
/// <summary>
/// 戻り値無しのprivate メソッドの実行サンプル
/// </summary>
private void ExecPrivateMethod()
{
Generater gene = new Generater();
Type type = typeof(Generater);
MethodInfo method = type.GetMethod("GetData", BindingFlags.NonPublic | BindingFlags.Instance, null
, new Type[] { typeof(string), typeof(DateTime), typeof(MethodBase) }, null);
MethodBase methodBase = null;
method.Invoke(gene, new object[] { "できるかな?", DateTime.Now, methodBase });
}
&bold(){戻り値有りのprivate メソッドの実行サンプル}
/// <summary>
/// 戻り値有りのprivate メソッドの実行サンプル
/// </summary>
private void ExexPrivateMethodReturn()
{
Generater gene = new Generater();
Type type = typeof(Generater);
MethodInfo method = type.GetMethod("GetData", BindingFlags.NonPublic | BindingFlags.Instance, null
, new Type[] { typeof(string), typeof(object) }, null);
object obj = method.Invoke(gene, new object[] { "戻り値希望!", null });
if (obj == null) Console.WriteLine("失敗");
Console.WriteLine(((EventMessage)obj).time.ToString());
}
&bold(){サンプルデータクラス}
public class EventMessage
{
public string val = "data";
public string Daata = "data";
public DateTime time = DateTime.Now;
}
public class Generater
{
private int Count;
private string pValue;
Generater s_Instance;
public Generater()
{
s_Instance = this;
}
public string GetValue()
{
return pValue;
}
public void GetData(string val)
{
Console.WriteLine(val);
}
public void GetData(string val, DateTime date)
{
Console.WriteLine(val);
}
private EventMessage GetData(string val, object obj)
{
EventMessage e = new EventMessage();
return e;
}
private void GetData(string val, DateTime date, MethodBase method)
{
Console.WriteLine(val);
}
}
表示オプション
横に並べて表示:
変化行の前後のみ表示: