- 最后登录
- 2019-12-2
- 注册时间
- 2012-8-25
- 阅读权限
- 90
- 积分
- 34660
- 纳金币
- 38268
- 精华
- 111
|
利用反射用字符串建立类- using UnityEngine;
- using System.Collections;
- using System.Reflection;
-
- public class Main : MonoBehaviour
- {
-
- void Start ()
- {
- //获取包含当前执行的代码的程序集。
- Assembly ass = Assembly.GetExecutingAssembly ();
-
- //使用区分大小写的搜索,从此程序集中查找指定的类型,然后使用系统激活器创建它的实例。
- Base o = ass.CreateInstance ("GameMain",true) as Base;
- o.writeString ("Start");
-
-
- o = ass.CreateInstance ("Test",true) as Base;
- o.writeString("Start");
-
-
- o = System.Activator.CreateInstance (o.GetType()) as Base;
- o.writeString ("Activator");
- }
-
- }
-
- public class Base
- {
- public virtual void writeString(string value)
- {
- Debug.Log (value);
- }
- }
-
- public class GameMain : Base
- {
- public override void writeString (string value)
- {
- base.writeString ("this is GameMain : " + value);
- }
- }
-
- public class Test : Base
- {
- public override void writeString (string value)
- {
- base.writeString ("this is Test : " + value);
- }
- }
复制代码 |
|