public ArraySequence(ArrayDefinition arrayDef) : base(arrayDef) { if (arrayDef == null) throw new ArgumentNullException("arrayDef"); this.arrayDef = arrayDef; }
private static Boolean CreateArrayItem(ReflectorResult result, ArrayDefinition arrayDefinition) { Type resultType = result.Value.GetType(); Type containedType = null; if (resultType.IsArray) { containedType = resultType.GetElementType(); } else { containedType = resultType.GetGenericArguments()[0]; } object newInstance = Activator.CreateInstance(containedType); if (!resultType.IsArray) { MethodInfo[] methods = GetMethods(result.Value, "Insert"); foreach (MethodInfo methodInfo in methods) { object[] temp = new object[arrayDefinition.Parameters.Length + 1]; arrayDefinition.Parameters.CopyTo(temp, 0); temp[arrayDefinition.Parameters.Length] = newInstance; object[] parameters = GetParameters(methodInfo, temp); if (parameters != null) { methodInfo.Invoke(result.Value, parameters); return(true); } } } return(false); }
private static void ProcessArray(ReflectorResult result, String codeFragment, Boolean createIfNotExists) { Int32 failCount = 0; ArrayDefinition arrayDefinition = GetArrayDefinition(result.Value, codeFragment); if (arrayDefinition != null) { // If this is anything but System.Array we need to call a Property PropertyInfo propertyInfo = arrayDefinition.RetrieveMemberInfo as PropertyInfo; if (propertyInfo != null) { SetPropertyInfoValue: try { object value = propertyInfo.GetValue(result.Value, arrayDefinition.Parameters); result.SetResult(value, propertyInfo, arrayDefinition.Parameters); } catch (TargetInvocationException ex) { failCount++; if (ex.InnerException is ArgumentOutOfRangeException && failCount == 1 && createIfNotExists) { if (CreateArrayItem(result, arrayDefinition)) { goto SetPropertyInfoValue; } } // Tried to fix it but failed. Blow up result.Clear(); throw new InvalidCodeFragmentException(codeFragment); } } else { // System.Array has a Method to call MethodInfo methodInfo = arrayDefinition.RetrieveMemberInfo as MethodInfo; if (methodInfo != null) { try { // We can't support dynamically creating array items object value = methodInfo.Invoke(result.Value, arrayDefinition.Parameters); result.SetResult(value, methodInfo, arrayDefinition.Parameters); } catch (TargetInvocationException) { result.Clear(); throw new InvalidCodeFragmentException(codeFragment); } } } } else { result.Clear(); throw new InvalidCodeFragmentException(codeFragment); } }
private static ArrayDefinition GetArrayDefinition(object value, String codeToExecute) { // All IList classes have an Item property except for System.Array. List <MemberInfo> retrieveMemberInfos = new List <MemberInfo>(); foreach (PropertyInfo propertyInfo in value.GetType().GetProperties(DefaultBindings)) { if (propertyInfo.Name == "Item") { retrieveMemberInfos.Add(propertyInfo); } } if (retrieveMemberInfos.Count == 0) { // We didn't find any Item properties so this is probably an Array. Use the GetValue method foreach (MethodInfo methodInfo in value.GetType().GetMethods(DefaultBindings)) { if (methodInfo.Name == "GetValue") { retrieveMemberInfos.Add(methodInfo); } } } // Some members have overloaded this[] methods. Find the correct method. foreach (MemberInfo memberInfo in retrieveMemberInfos) { object[] parameters = GetParameters(codeToExecute, memberInfo); if (parameters != null) { ArrayDefinition arrayDefinition = new ArrayDefinition(); arrayDefinition.Parameters = parameters; arrayDefinition.RetrieveMemberInfo = memberInfo; return(arrayDefinition); } } return(null); }
private VarDefinition ParseVarDefinition() { var id = ParseIdentifier(); var node = new VarDefinition(); node.AddChild(id); var t = NextToken(); if (t.IsOperator(Operator.Assign)) { var expr = ParseExpression(); node.AddChild(expr); } else if (t.IsDelimiter(Delimiter.BraceOpen)) { node = new ArrayDefinition(); node.AddChild(id); var expr = ParseExpression(); node.AddChild(expr); t = NextToken(); AssertDelimiter(Delimiter.BraceClose, t); node.PropagatePosition(t); } else { _stream.Previous(); } return(node); }
internal protected virtual T Visit(ArrayDefinition node) { return(Visit(node as Expression)); }
public ArrayParameter(Int16 id, IParameterManager manager, ArrayDefinition <T> typeDefinition) : base(id, manager, typeDefinition) { }
protected override EP_VP1 Visit(ArrayDefinition node) { return(Visit(node as Expression)); }
/// <summary> /// Generates an array type /// </summary> /// <param name="emitter">The emitter to write to</param> /// <param name="typeName">The name of the type</param> /// <param name="def">The array definition</param> /// <param name="root">True if this is the root type, false otherwise</param> private void _generateArrayType(CSharpEmitter emitter, string typeName, string fieldName, ArrayDefinition def, bool root) { if (root) { typeName = _getDefinitionName(typeName, fieldName, def); var elementTypeName = _getDefinitionName(null, "element", def); using (var wrapper = _generateWrapperType(emitter, typeName, elementTypeName)) { if (root) { _generateDefinition(wrapper, null, "element", def.ElementType); } } } if (!root) { _generateDefinition(emitter, null, fieldName, def.ElementType); } }