Expression ConvertTypeIs(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) { return(null); } Expression converted = Convert(invocation.Arguments.ElementAt(0)); AstType type = ConvertTypeReference(invocation.Arguments.ElementAt(1)); if (converted != null && type != null) { return new IsExpression { Expression = converted, Type = type } } ; return(null); } #endregion #region Convert Array Expression ConvertArrayIndex(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) { return(NotSupported(invocation)); } Expression targetConverted = Convert(invocation.Arguments.First()); if (targetConverted == null) { return(null); } Expression index = invocation.Arguments.ElementAt(1); Expression indexConverted = Convert(index); if (indexConverted != null) { return(new IndexerExpression(targetConverted, indexConverted)); } IList <Expression> indexesConverted = ConvertExpressionsArray(index); if (indexesConverted != null) { return(new IndexerExpression(targetConverted, indexesConverted)); } return(null); } Expression ConvertArrayLength(InvocationExpression invocation) { if (invocation.Arguments.Count != 1) { return(NotSupported(invocation)); } Expression targetConverted = Convert(invocation.Arguments.Single()); if (targetConverted != null) { return(targetConverted.Member("Length", TextTokenKind.InstanceProperty).WithAnnotation(Create_SystemArray_get_Length())); } else { return(null); } } ModuleDef GetModule() { if (context.CurrentMethod != null && context.CurrentMethod.Module != null) { return(context.CurrentMethod.Module); } if (context.CurrentType != null && context.CurrentType.Module != null) { return(context.CurrentType.Module); } if (context.CurrentModule != null) { return(context.CurrentModule); } return(null); } IMDTokenProvider Create_SystemArray_get_Length() { if (Create_SystemArray_get_Length_result_initd) { return(Create_SystemArray_get_Length_result); } Create_SystemArray_get_Length_result_initd = true; var module = GetModule(); if (module == null) { return(null); } const string propName = "Length"; var type = module.CorLibTypes.GetTypeRef("System", "Array"); var retType = module.CorLibTypes.Int32; var mr = new MemberRefUser(module, "get_" + propName, MethodSig.CreateInstance(retType), type); Create_SystemArray_get_Length_result = mr; var md = mr.ResolveMethod(); if (md == null || md.DeclaringType == null) { return(mr); } var prop = md.DeclaringType.FindProperty(propName); if (prop == null) { return(mr); } Create_SystemArray_get_Length_result = prop; return(prop); } IMDTokenProvider Create_SystemArray_get_Length_result; bool Create_SystemArray_get_Length_result_initd; Expression ConvertNewArrayInit(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) { return(NotSupported(invocation)); } AstType elementType = ConvertTypeReference(invocation.Arguments.ElementAt(0)); IList <Expression> elements = ConvertExpressionsArray(invocation.Arguments.ElementAt(1)); if (elementType != null && elements != null) { if (ContainsAnonymousType(elementType)) { elementType = null; } return(new ArrayCreateExpression { Type = elementType, AdditionalArraySpecifiers = { new ArraySpecifier() }, Initializer = new ArrayInitializerExpression(elements) }); } return(null); } Expression ConvertNewArrayBounds(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) { return(NotSupported(invocation)); } AstType elementType = ConvertTypeReference(invocation.Arguments.ElementAt(0)); IList <Expression> arguments = ConvertExpressionsArray(invocation.Arguments.ElementAt(1)); if (elementType != null && arguments != null) { if (ContainsAnonymousType(elementType)) { elementType = null; } ArrayCreateExpression ace = new ArrayCreateExpression(); ace.Type = elementType; ace.Arguments.AddRange(arguments); return(ace); } return(null); } bool ContainsAnonymousType(AstType type) { foreach (AstType t in type.DescendantsAndSelf.OfType <AstType>()) { ITypeDefOrRef tr = t.Annotation <ITypeDefOrRef>(); if (tr != null && tr.IsAnonymousType()) { return(true); } } return(false); } #endregion }
Expression ConvertTypeIs(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) { return(null); } Expression converted = Convert(invocation.Arguments.ElementAt(0)); AstType type = ConvertTypeReference(invocation.Arguments.ElementAt(1)); if (converted != null && type != null) { return new IsExpression { Expression = converted, Type = type } } ; return(null); } #endregion #region Convert Array Expression ConvertArrayIndex(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) { return(NotSupported(invocation)); } Expression targetConverted = Convert(invocation.Arguments.First()); if (targetConverted == null) { return(null); } Expression index = invocation.Arguments.ElementAt(1); Expression indexConverted = Convert(index); if (indexConverted != null) { return(new IndexerExpression(targetConverted, indexConverted)); } IList <Expression> indexesConverted = ConvertExpressionsArray(index); if (indexesConverted != null) { return(new IndexerExpression(targetConverted, indexesConverted)); } return(null); } Expression ConvertArrayLength(InvocationExpression invocation) { if (invocation.Arguments.Count != 1) { return(NotSupported(invocation)); } Expression targetConverted = Convert(invocation.Arguments.Single()); if (targetConverted != null) { return(targetConverted.Member("Length")); } else { return(null); } } Expression ConvertNewArrayInit(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) { return(NotSupported(invocation)); } AstType elementType = ConvertTypeReference(invocation.Arguments.ElementAt(0)); IList <Expression> elements = ConvertExpressionsArray(invocation.Arguments.ElementAt(1)); if (elementType != null && elements != null) { if (ContainsAnonymousType(elementType)) { elementType = null; } return(new ArrayCreateExpression { Type = elementType, AdditionalArraySpecifiers = { new ArraySpecifier() }, Initializer = new ArrayInitializerExpression(elements) }); } return(null); } Expression ConvertNewArrayBounds(InvocationExpression invocation) { if (invocation.Arguments.Count != 2) { return(NotSupported(invocation)); } AstType elementType = ConvertTypeReference(invocation.Arguments.ElementAt(0)); IList <Expression> arguments = ConvertExpressionsArray(invocation.Arguments.ElementAt(1)); if (elementType != null && arguments != null) { if (ContainsAnonymousType(elementType)) { elementType = null; } ArrayCreateExpression ace = new ArrayCreateExpression(); ace.Type = elementType; ace.Arguments.AddRange(arguments); return(ace); } return(null); } bool ContainsAnonymousType(AstType type) { foreach (AstType t in type.DescendantsAndSelf.OfType <AstType>()) { ITypeDefOrRef tr = t.Annotation <ITypeDefOrRef>(); if (tr != null && tr.IsAnonymousType()) { return(true); } } return(false); } #endregion }