Advertisement
BigETI

ReflectionTest.cs

Aug 27th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4.  
  5. namespace ReflectionTest
  6. {
  7.     public abstract class SomeBehaviour
  8.     {
  9.         internal MethodInfo startMethod = null;
  10.         internal MethodInfo updateMethod = null;
  11.     }
  12.  
  13.     public class SomeScript : SomeBehaviour
  14.     {
  15.         private void Start()
  16.         {
  17.             Console.WriteLine("SomeScript.Start");
  18.         }
  19.     }
  20.  
  21.     public class AnotherScript : SomeBehaviour
  22.     {
  23.         private void Start()
  24.         {
  25.             Console.WriteLine("AnotherScript.Start");
  26.         }
  27.  
  28.         private void Update()
  29.         {
  30.             Console.WriteLine("AnotherScript.Update");
  31.         }
  32.     }
  33.  
  34.     internal class Program
  35.     {
  36.         private static List<SomeBehaviour> objects = new List<SomeBehaviour>();
  37.  
  38.         public static SomeBehaviour CreateObject(string typeName)
  39.         {
  40.             SomeBehaviour ret = null;
  41.             try
  42.             {
  43.                 Type type = Type.GetType("ReflectionTest." + typeName);
  44.                 if (type != null)
  45.                 {
  46.                     if (type.IsSubclassOf(typeof(SomeBehaviour)))
  47.                     {
  48.                         ret = (SomeBehaviour)(Activator.CreateInstance(type));
  49.                         try
  50.                         {
  51.                             ret.startMethod = type.GetMethod("Start", BindingFlags.NonPublic | BindingFlags.Instance);
  52.                         }
  53.                         catch (Exception e)
  54.                         {
  55.                             Console.Error.WriteLine(e);
  56.                         }
  57.                         try
  58.                         {
  59.                             ret.updateMethod = type.GetMethod("Update", BindingFlags.NonPublic | BindingFlags.Instance);
  60.                         }
  61.                         catch (Exception e)
  62.                         {
  63.                             Console.Error.WriteLine(e);
  64.                         }
  65.                         objects.Add(ret);
  66.                     }
  67.                 }
  68.             }
  69.             catch (Exception e)
  70.             {
  71.                 Console.Error.WriteLine(e);
  72.             }
  73.             return ret;
  74.         }
  75.  
  76.         public static void Run()
  77.         {
  78.             foreach (SomeBehaviour obj in objects)
  79.             {
  80.                 if (obj.startMethod != null)
  81.                     obj.startMethod.Invoke(obj, new object[0]);
  82.             }
  83.             while (true)
  84.             {
  85.                 foreach (SomeBehaviour obj in objects)
  86.                 {
  87.                     if (obj.updateMethod != null)
  88.                         obj.updateMethod.Invoke(obj, new object[0]);
  89.                 }
  90.             }
  91.         }
  92.  
  93.         static void Main(string[] args)
  94.         {
  95.             CreateObject("BlahScript");
  96.             CreateObject("SomeScript");
  97.             CreateObject("AnotherScript");
  98.             Run();
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement