public static object NewInstance(VAL val, object[] args) { object instance = HostType.NewInstance(val.Class, args); Val2Host(val, instance); return(instance); }
/* * * .net object format/protocol: * * (1) new className(args,...) * 如 new System.Windows.Form.TextBox() * new Tie.VAL(20) * * * (2) new SerializedObject(string className, string value.ToString(), string SerializedValue); * 请看Encode()生成的格式 * * 例如: * 前提: * System.Windows.Form.Label的值是Type, 已经用Register函数登记在数据字典Computer.DS中. * * 求: * new System.Windows.Form.Label(); 转化为 new Label(System.Window.Form); * * */ public static VAL Decode(string className, VAL args, VAL scope, Context context) { VAL clss = new VAL(); /* * 如果是注册过的class, 那么它的namespace是定义在Computer.DS中 * 譬如 * A= new NS.CLSS(); * scope[className] --> clss; * * 如果没有定义namespace,那么直接到Computer.DS中去取值 * 譬如 * A= CLSS(); * Computer.DS[className] --> clss * * */ clss = scope[className]; if (clss.Undefined) { clss = context.GetVAL(className, true); //如果没有找到val.Class, context.GetVAL(...)返回new VAL() } //返回注册过的class名字 if (clss.Defined && clss.value is Type) { Type type = (Type)clss.value; object instance = Activator.CreateInstance(type, ConstructorArguments(args)); return(VAL.Boxing1(instance)); //返回实例 } //throw new RuntimeException(string.Format("class [{0}]has not registered yet.", scope.name + "." + val.Class)); //如果没有注册过 if (scope.name != null) { object instance = HostType.NewInstance(scope.name + "." + className, ConstructorArguments(args)); if (instance != null) { //把HostType类型注册到CPU.DS2中去 VAL hostType = VAL.NewHostType(instance.GetType()); scope[className] = hostType; return(VAL.Boxing1(instance)); //返回实例 } } if (clss.IsNull) { throw new HostTypeException("class {0} is not defined.", className); } if (clss.value != null) { Type type; if (clss.value is Type) { type = (Type)clss.value; } else { type = clss.value.GetType(); } object instance = Activator.CreateInstance(type, ConstructorArguments(args)); return(VAL.NewHostType(instance)); } return(args); }