Exemple #1
0
        public static Type ResolveType(TypeLookup typeLookup, IReadOnlyList <string> namespaces = null, Type scopeType = null)
        {
            if (typeLookup.resolvedType != null)
            {
                return(typeLookup.resolvedType);
            }

            FilterAssemblies();

            // base type will valid or an exception will be thrown
            Type baseType = ResolveBaseTypePath(typeLookup, namespaces);

            if (typeLookup.generics != null && typeLookup.generics.Count != 0)
            {
                if (!baseType.IsGenericTypeDefinition)
                {
                    throw new TypeResolutionException($"{baseType} is not a generic type definition but we are trying to resolve a generic type with it because generic arguments were provided");
                }

                baseType = ReflectionUtil.CreateGenericType(baseType, ResolveGenericTypes(typeLookup, namespaces, scopeType));
            }

            if (typeLookup.isArray)
            {
                baseType = baseType.MakeArrayType();
            }

            return(baseType);
        }
        public Expression Resolve(LinqCompiler _compiler)
        {
            UIForiaLinqCompiler compiler = (UIForiaLinqCompiler)_compiler;

            switch (variableType)
            {
            case AliasResolverType.ContextVariable: {
                ParameterExpression el     = compiler.GetElement();
                Expression          access = Expression.MakeMemberAccess(el, TemplateCompiler.s_UIElement_BindingNode);
                Expression          call   = ExpressionFactory.CallInstanceUnchecked(access, TemplateCompiler.s_LinqBindingNode_GetContextVariable, Expression.Constant(id));
                Type varType = ReflectionUtil.CreateGenericType(typeof(ContextVariable <>), type);

                UnaryExpression     convert  = Expression.Convert(call, varType);
                ParameterExpression variable = compiler.AddVariable(type, "ctxVar_resolve_" + GetName());

                compiler.Assign(variable, Expression.MakeMemberAccess(convert, varType.GetField("value")));
                return(variable);
            }

            case AliasResolverType.RepeatItem: {
                compiler.Comment(name);
                //var repeat_item_name = element.bindingNode.GetRepeatItem<T>(id).value;
                ParameterExpression el     = compiler.GetElement();
                Expression          access = Expression.MakeMemberAccess(el, TemplateCompiler.s_UIElement_BindingNode);

                ReflectionUtil.TypeArray1[0] = type;
                MethodInfo getItem = TemplateCompiler.s_LinqBindingNode_GetRepeatItem.MakeGenericMethod(ReflectionUtil.TypeArray1);
                Expression call    = ExpressionFactory.CallInstanceUnchecked(access, getItem, Expression.Constant(id));
                Type       varType = ReflectionUtil.CreateGenericType(typeof(ContextVariable <>), type);

                ParameterExpression variable = compiler.AddVariable(type, "repeat_item_" + GetName());

                compiler.Assign(variable, Expression.MakeMemberAccess(call, varType.GetField(nameof(ContextVariable <int> .value))));
                return(variable);
            }

            case AliasResolverType.RepeatIndex: {
                ParameterExpression el     = compiler.GetElement();
                Expression          access = Expression.MakeMemberAccess(el, TemplateCompiler.s_UIElement_BindingNode);
                Expression          call   = ExpressionFactory.CallInstanceUnchecked(access, TemplateCompiler.s_LinqBindingNode_GetContextVariable, Expression.Constant(id));

                UnaryExpression     convert  = Expression.Convert(call, typeof(ContextVariable <int>));
                ParameterExpression variable = compiler.AddVariable(type, "repeat_index_" + GetName());

                compiler.Assign(variable, Expression.MakeMemberAccess(convert, typeof(ContextVariable <int>).GetField(nameof(ContextVariable <int> .value))));
                return(variable);
            }

            default:
                throw new ArgumentOutOfRangeException();
            }
        }