Ejemplo n.º 1
0
        private Expression FetchModelMethod(string name, IList <Tuple <Expression, Location> > args, Location funcPos)
        {
            bool       variable;
            MethodInfo func;
            var        signatures = ContextType.GetMethods()
                                    .Where(mi => name.Equals(mi.Name) && mi.GetParameters().Length == args.Count).ToList();

            if (signatures.Count == 0)
            {
                func = ContextType.GetMethods().FirstOrDefault(mi => name.Equals(mi.Name));
                if (func == null)
                {
                    return(null);
                }
                variable = IsVariableNumOfArgsAccepted(func);
                if (!variable)
                {
                    return(null);
                }

                signatures = new List <MethodInfo> {
                    func
                };
            }
            AssertMethodNotAmbiguous(signatures.Count, name, args.Count, funcPos);

            func     = signatures.Single();
            variable = IsVariableNumOfArgsAccepted(func);
            return(CreateMethodCallExpression(ContextExpression, args, func, variable));
        }
Ejemplo n.º 2
0
 private void AssertMethodExists(string name, Location pos)
 {
     if (!Functions.ContainsKey(name) && !ContextType.GetMethods().Any(mi => name.Equals(mi.Name)))
     {
         throw new ParseErrorException(
                   $"Function '{name}' not known.", ExprString, pos);
     }
 }
Ejemplo n.º 3
0
 private void AssertMethodNameExistence(string name, Location funcPos)
 {
     if (!Functions.ContainsKey(name) && !ContextType.GetMethods().Any(mi => name.Equals(mi.Name)))
     {
         throw new ParseErrorException(
                   string.Format("Function '{0}' not known.", name),
                   funcPos);
     }
 }
Ejemplo n.º 4
0
        private Expression FetchModelMethod(string name, IList <Tuple <Expression, Location> > args, Location funcPos)
        {
            var signatures = ContextType.GetMethods()
                             .Where(mi => name.Equals(mi.Name) && mi.GetParameters().Length == args.Count)
                             .ToList();

            if (signatures.Count == 0)
            {
                return(null);
            }
            AssertNonAmbiguity(signatures.Count, name, args.Count, funcPos);

            return(CreateMethodCallExpression(ContextExpression, args, signatures.Single()));
        }
Ejemplo n.º 5
0
        private Expression FetchModelMethod(string name, IList <Expression> args)
        {
            var signatures = ContextType.GetMethods()
                             .Where(mi => name.Equals(mi.Name) && mi.GetParameters().Length == args.Count)
                             .ToList();

            if (signatures.Count == 0)
            {
                return(null);
            }
            if (signatures.Count > 1)
            {
                throw new InvalidOperationException(string.Format("Function {0} accepting {1} arguments is ambiguous.", name, args.Count));
            }

            return(CreateMethodCallExpression(ContextExpression, signatures.Single(), args));
        }
Ejemplo n.º 6
0
        public IContext LoadContext()
        {
            var propertyDictionary = PropertyLoader.Load(ContextType);
            // inject property into context
            var contextProperties = from PropertyInfo propertyInfo
                                    in ContextType.GetProperties()
                                    where propertyInfo.PropertyType == typeof(string) &&
                                    AttributeHelper.IsPropertyMarked <PropertyValue>(propertyInfo)
                                    select(propertyInfo, AttributeHelper.getAttribute <PropertyValue>(propertyInfo));

            foreach (var property in contextProperties)
            {
                property.Item1.SetValue(Context, propertyDictionary[property.Item2.Name]);
            }

            var contextBuilder = new InjectorContext.InjectorContextBuilder();
            // initialize models
            var priorityQueue    = new SimplePriorityQueue <int, InstantiateModel>();
            var methodsInContext = from MethodInfo methodInfo
                                   in ContextType.GetMethods(BindingFlags.Instance | BindingFlags.Public)
                                   where AttributeHelper.IsMethodMarked <Instantiate>(methodInfo)
                                   select methodInfo;

            foreach (var methodInfo in methodsInContext)
            {
                var attr  = methodInfo.GetCustomAttribute <Instantiate>();
                var model = string.IsNullOrEmpty(attr.Name) ? AddModel(methodInfo) : AddModel(methodInfo, attr.Name);

                if (model != null)
                {
                    priorityQueue.Enqueue(model.Weight, model);
                }
            }

            var namespaceToScan = AttributeHelper.GetNamespaceToScan(ContextType);
            var assembly        = Assembly.GetCallingAssembly();

            var scanTypes = from Type type
                            in assembly.GetTypes()
                            where AttributeHelper.IsTypeMarked <Instantiate>(type) && namespaceToScan.Contains(type.Namespace)
                            select type;

            // create models to instantiate
            foreach (var scanType in scanTypes)
            {
                var attr  = scanType.GetCustomAttribute <Instantiate>();
                var model = string.IsNullOrEmpty(attr.Name) ? AddModel(scanType) : AddModel(scanType, attr.Name);

                if (model != null)
                {
                    priorityQueue.Enqueue(model.Weight, model);
                }
            }

            // Create Objects
            var previousCount = priorityQueue.Count;

            while (!priorityQueue.IsEmpty)
            {
                var model = priorityQueue.Dequeue();
                if (CanInstantiate(model))
                {
                    var instance = Instantiate(model);
                    // property injection
                    if (model.IsConstructorType)
                    {
                        var properties = from PropertyInfo propertyInfo in model.Type.GetProperties()
                                         where AttributeHelper.IsPropertyMarked <PropertyValue>(propertyInfo)
                                         select(propertyInfo, AttributeHelper.getAttribute <PropertyValue>(propertyInfo));

                        foreach (var property in properties)
                        {
                            var name = property.Item2.Name;
                            property.Item1.SetValue(instance, propertyDictionary[name]);
                        }
                    }
                    ObjectStorage[model.Name] = instance;
                }
                else
                {
                    UpdateWeight(model);
                    priorityQueue.Enqueue(model.Weight, model);
                }

                if (previousCount <= priorityQueue.Count)
                {
                    throw new DependencyNotFoundException(model);
                }

                previousCount = priorityQueue.Count;
            }

            // Property Injection
            foreach (var instance in ObjectStorage.Values)
            {
                var instanceType = instance.GetType();
                var properties   = from PropertyInfo propertyInfo
                                   in instanceType.GetProperties()
                                   where AttributeHelper.IsPropertyMarked <Autowired>(propertyInfo)
                                   select propertyInfo;
                foreach (var property in properties)
                {
                    var attr = property.GetCustomAttribute <Autowired>();
                    property.SetValue(instance,
                                      !string.IsNullOrEmpty(attr?.Name)
                            ? ObjectStorage[attr.Name]
                            : ObjectStorage[NamingHelper.ConvertToCamelCase(property.Name)]);
                }
            }

            // invoke post constructor
            foreach (var name in ObjectStorage.Keys)
            {
                var model = ModelStorage[name];
                if (!model.IsConstructorType)
                {
                    continue;
                }
                var type             = model.Type;
                var instance         = ObjectStorage[name];
                var postConstructors =
                    from MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
                    where AttributeHelper.IsMethodMarked <PostConsturctor>(method)
                    select method;
                foreach (var postConstructor in postConstructors)
                {
                    postConstructor.Invoke(instance, null);
                }
            }
            // Build
            foreach (var pair in ObjectStorage)
            {
                contextBuilder.Add(pair.Key, pair.Value);
            }

            return(contextBuilder.Build());
        }