public bool CanCast(System.Type type) { return(ReflectionUtil.CanCast(type, Value)); }
public bool CanCast <T> () { return(ReflectionUtil.CanCast(typeof(T), Value)); }
CSNode VisitSelectors(CSScriptParser.SelectorContext[] selectors, int line, int col) { int selectorLen = selectors.Length; string firstName = selectors[0].NAME().GetText(); if (_state.HasVariable(firstName)) { CSLocalVariableNode node = new CSLocalVariableNode(line, col); node._declaration = false; node._variableName = firstName; if (selectorLen == 1) { return(node); } else { CSSelectorNode selectorNode = new CSSelectorNode(line, col); selectorNode._selectors = GetSelectorStrings(selectors, 1); CSOPDotNode dotNode = new CSOPDotNode(line, col); dotNode._children = new CSNode[2]; dotNode._children[0] = node; dotNode._children[1] = selectorNode; return(dotNode); } } string currentTypeString = null; System.Type currentType = null; int typeStart = -1; int typeEnd = 0; for (int i = 0; i < selectorLen; ++i) { CSScriptParser.SelectorContext next = selectors[i]; string name = next.NAME().GetText(); currentTypeString = GetTypeString(selectors, i + 1, typeStart); System.Type nextType = ReflectionUtil.GetType(currentTypeString); if (typeStart == -1 && nextType != null) { typeStart = i; } if (currentType != null && nextType == null) { typeEnd = i; break; } currentType = nextType; } if (currentType != null) { if (typeEnd == 0) { CSTypeNode node = new CSTypeNode(line, col); node._typeString = currentTypeString; node._type = currentType; node._assemblyName = currentType.Assembly.GetCleanName(); return(node); } else { CSStaticVariableNode node = new CSStaticVariableNode(line, col); string varName = selectors[typeEnd].NAME().GetText(); node._variableName = varName; node._staticType = currentType; node._type = ReflectionUtil.GetFieldType(currentType, varName); if (node._type == null) { CSLog.E(node, "type: " + currentType.FullName + " doesn't have: " + varName); } if (selectorLen == typeEnd + 1) { return(node); } else { CSSelectorNode selectorNode = new CSSelectorNode(line, col); selectorNode._selectors = GetSelectorStrings(selectors, typeEnd + 1); CSOPDotNode dotNode = new CSOPDotNode(line, col); dotNode._children = new CSNode[2]; dotNode._children[0] = node; dotNode._children[1] = selectorNode; return(dotNode); } } } else { CSSelectorNode node = new CSSelectorNode(line, col); node._selectors = GetSelectorStrings(selectors, 0); return(node); } }
string _GetTypeString( object[] vartypes, int varCount, int typeStart, System.Func <object, string> getName, System.Func <object, CSScriptParser.Generic_parametersContext> getGenericParams) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); System.Text.StringBuilder sbTemplate = new System.Text.StringBuilder(); sbTemplate.Append('['); bool isThereTemplate = false; for (int i = 0; i < varCount; ++i) { object next = vartypes[i]; if (i != 0) { if (typeStart >= 0 && typeStart < i) { sb.Append('+'); } else { sb.Append('.'); } } string name = getName(next); sb.Append(ReflectionUtil.GetCleanNameIfPrimitive(name)); CSScriptParser.Generic_parametersContext genericParameters = getGenericParams(next); if (genericParameters != null) { CSScriptParser.TypeContext[] genericTypes = genericParameters.type(); int tempVarCount = genericTypes.Length; if (tempVarCount > 0) { if (isThereTemplate) { sbTemplate.Append(','); } else { isThereTemplate = true; } sb.Append('`'); sb.Append(tempVarCount.ToString()); sbTemplate.Append('['); for (int j = 0; j < tempVarCount; ++j) { if (j != 0) { sbTemplate.Append("], ["); } CSTypeNode child = VisitType(genericTypes[j]) as CSTypeNode; sbTemplate.Append(child._typeString); } sbTemplate.Append(']'); } } } sbTemplate.Append(']'); if (isThereTemplate) { sb.Append(sbTemplate.ToString()); } string typeString = sb.ToString(); return(typeString); }
public override CSNode VisitNewExp(CSScriptParser.NewExpContext context) { CSOPNewNode node = new CSOPNewNode(context.Start.Line, context.Start.Column); node._children = new CSNode[6]; CSScriptParser.ParametersContext parameters = context.parameters(); CSScriptParser.Array_indexContext arrayIndex = context.array_index(); System.Type currentType; string currentTypeString = VisitTypeElements(context.type_elements(), out currentType); if (arrayIndex != null) { currentTypeString = currentTypeString + "[]"; currentType = ReflectionUtil.GetType(currentTypeString); } if (currentType == null) { CSLog.E(node, "unknown type: " + currentTypeString); return(null); } CSTypeNode typeNode = new CSTypeNode(context.Start.Line, context.Start.Column); typeNode._type = currentType; typeNode._typeString = currentTypeString; typeNode._assemblyName = currentType.Assembly.GetCleanName(); node._children[0] = typeNode; if (parameters != null) { node._children[1] = Visit(parameters); } if (arrayIndex != null) { node._children[2] = Visit(arrayIndex); } CSScriptParser.InitializerContext initializer = context.initializer(); if (initializer != null) { CSScriptParser.Array_initializerContext arrayInitializer = initializer.array_initializer(); if (arrayInitializer != null) { node._children[3] = Visit(arrayInitializer); } CSScriptParser.Dictionary_initializerContext dictionaryInitializer = initializer.dictionary_initializer(); if (dictionaryInitializer != null) { node._children[4] = Visit(dictionaryInitializer); } CSScriptParser.Class_initializerContext classInitializer = initializer.class_initializer(); if (classInitializer != null) { node._children[5] = Visit(classInitializer); } } return(node); }
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); }