Reflection範例-1

  • 1902
  • 0

Reflection範例-1

Dotblogs 的標籤: ,

稍微練一下 Reflection,筆記:

Calcuator.cs (Class Library)

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5:  
   6: namespace ReflectionPractice_EntityClass
   7: {
   8:     public class Calcuator
   9:     {
  10:         public Calcuator() { }
  11:         private double _number;
  12:         public double Number { get { return _number; } set { _number = value; } }
  13:         public void Clear() { DoClear(); }
  14:         private void DoClear() { Console.WriteLine("Clear!"); }
  15:         public double Add(double number) { return Number += number; }
  16:         public static double Pi { get { return 3.1415926d; } }
  17:         public static double GetPi() { return Pi; }
  18:     }
  19: }
 
Program.cs (Console Project):
   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5: using System.Reflection;
   6: using RefelctionPractice_Interface;
   7: using System.Data;
   8:  
   9: namespace ReflectionPractice_CS
  10: {
  11:   class Program
  12:   {
  13:     static void Main(string[] args)
  14:     {
  15:       LoadCalcuator();
  16:       Console.ReadLine();
  17:     }
  18: public static void LoadCalcuator()
  19: {
  20:   //取得程式執行位置
  21:   Console.WriteLine(System.IO.Path.GetFullPath("./"));
  22:   //載入dll
  23:   Assembly testAssembly = Assembly.LoadFile(@System.IO.Path.GetFullPath("./") + "ReflectionPractice_EntityClass.dll");
  24:   //映射型別
  25:   Type calcType = testAssembly.GetType("ReflectionPractice_EntityClass.Calcuator");
  26:   //建立實體
  27:   object calcInstance = Activator.CreateInstance(calcType);
  28:  
  29:   //取得屬性
  30:   PropertyInfo numberPropertyInfo = calcType.GetProperty("Number");
  31:   double value = (double)numberPropertyInfo.GetValue(calcInstance, null);
  32:   Console.WriteLine(value);
  33:   //設定屬性
  34:   numberPropertyInfo.SetValue(calcInstance, 10.0, null);
  35:   Console.WriteLine((double)numberPropertyInfo.GetValue(calcInstance, null));
  36:  
  37:   //調用public方法
  38:   calcType.InvokeMember("Clear", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, calcInstance, null);
  39:   //調用private方法
  40:   calcType.InvokeMember("DoClear", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic, null, calcInstance, null);
  41:   //調用方法帶參數
  42:   double addvalue = (double)calcType.InvokeMember("Add", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
  43:       null, calcInstance, new object[] { 20.0 });
  44:   Console.WriteLine(addvalue);
  45:  
  46:   //取得靜態唯讀屬性
  47:   PropertyInfo piPropertyInfo = calcType.GetProperty("Pi");
  48:   Console.WriteLine(piPropertyInfo.GetValue(calcInstance, null));
  49:   //調用靜態方法
  50:   double piValue = (double)calcType.InvokeMember("GetPi", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, null, null, null);
  51:   Console.WriteLine(piValue);
  52:  
  53:   //取得private field
  54:   double number = (double)calcType.InvokeMember("_number", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic,
  55:       null, calcInstance, null);
  56:   Console.WriteLine(number);
  57: }
  58:  
  59: }

Output:

161320

--------
沒什麼特別的~
不過是一些筆記而已