public CSObject Assign(CSObject obj) { switch (ObjectType) { case ObjectType.VARIABLE: ReflectionUtil.Set(_object, _name, obj.GetAs(_type)); break; case ObjectType.STATIC: ReflectionUtil.Set(_staticType, _name, obj.GetAs(_type)); break; case ObjectType.ARRAY: IList array = (IList)_object; array[_arrayIndex] = obj.GetAs(_type); break; case ObjectType.DICTIONARY: IDictionary dictionary = (IDictionary)_object; dictionary[_name] = obj.GetAs(_type); break; case ObjectType.TEMP: case ObjectType.LOCAL: if (_type == null) { _object = obj.Value; _type = obj.Type; } else { _object = obj.GetAs(_type); } break; default: CSLog.E(_node, "cannot assign to " + _objectType.ToString()); break; } return(this); }
public override CSObject Evaluate(CSState state, CSObject curObj) { if (ChildCount != 6) { CSLog.E(this, "new operator has invalid # of children..."); return(null); } if (NewType == null || NewType._type == null) { CSLog.E(this, "new operator does not have a proper type..."); return(null); } object newInstance = null; System.Type type = NewType._type; if (IsArray) { System.Type innerType = type.GetElementType(); int count = ArrayIndex.EvaluateIndex(null, null); if (ArrayInitializer != null) { object[] elements = ArrayInitializer.EvaluateElements(state, curObj, innerType); int len = elements.Length; int allocCount = (len > count? len : count); newInstance = System.Activator.CreateInstance(type, allocCount); System.Array array = newInstance as System.Array; for (int i = 0; i < len; ++i) { array.SetValue(elements[i], i); } } else { if (count < 0) { throw new System.ArgumentException("array needs an initializer or count"); } else { newInstance = System.Activator.CreateInstance(type, count); } } } else { object[] parameters = null; if (NewParameters != null && NewParameters.ChildCount > 0) { CSNode[] pchildren = NewParameters._children; int pCount = pchildren.Length; parameters = new object[pCount]; for (int i = 0; i < pCount; ++i) { parameters[i] = pchildren[i].Evaluate(state, curObj).Value; } } if (parameters == null) { newInstance = System.Activator.CreateInstance(type); } else { newInstance = System.Activator.CreateInstance(type, parameters); } CSDictionaryInitializerNode dictInitializer = DictionaryInitializer; if (dictInitializer != null) { IDictionary dict = newInstance as IDictionary; if (dict == null) { throw new System.InvalidOperationException("you cannot use a dictionary initializer on non dictionary object"); } System.Type keyType = typeof(object); System.Type valueType = typeof(object); System.Type[] genericTypes = NewType._type.GetGenericArguments(); if (genericTypes != null) { if (genericTypes.Length > 2) { keyType = genericTypes[0]; valueType = genericTypes[1]; } } int len = dictInitializer.Count; for (int i = 0; i < len; ++i) { KeyValuePair <object, object> element = dictInitializer.Evaluate(state, curObj, i, keyType, valueType); dict.Add(element.Key, element.Value); } } CSClassInitializerNode classInitializer = ClassInitializer; if (classInitializer != null) { int len = classInitializer.Count; for (int i = 0; i < len; ++i) { KeyValuePair <string, object> element = classInitializer.Evaluate(state, curObj, i, newInstance); ReflectionUtil.Set(newInstance, element.Key, element.Value); } } CSArrayInitializerNode arrayInitializer = ArrayInitializer; IList list = newInstance as IList; if (ArrayInitializer != null) { if (list == null) { CSLog.E("array initializer cannot used for : " + type.ToString()); } else { System.Type elementType = ReflectionUtil.GetIListElementType(NewType._type); if (elementType == null) { CSLog.E("array initializer cannot used for : " + type.ToString()); } object[] elements = ArrayInitializer.EvaluateElements(state, curObj, elementType); int len = elements.Length; for (int i = 0; i < len; ++i) { list.Add(elements[i]); } } } } CSObject obj = CSObject.TempVariableObject(this, type, newInstance); return(obj); }