1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; using System.Reflection; namespace test { class Program { static void Main(string[] args) { string className = "test.FireBird"; Type type = Type.GetType(className); // インスタンスの作成 object fireBirdObj = Activator.CreateInstance(type); // 関数呼び出し(引数なし) MethodInfo methodInfo = type.GetMethod("Fly"); methodInfo.Invoke(fireBirdObj, null); // 関数呼び出し(引数あり) methodInfo = type.GetMethod("Fire"); object[] msg = { "Wow!" }; methodInfo.Invoke(fireBirdObj, msg); Console.ReadKey(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; namespace test { class FireBird { public string Name { get; set; } = "hino-tori"; public void Fly() { Console.WriteLine($"{Name} is flying."); } public void Fire(string msg) { Console.WriteLine($"{Name} is firing. {msg}"); } } } |