Example #1
0
        private ILGenerator CreateMethod(Type type)
        {
            ILGenerator methodBuilder = null;

            var initCtor   = builderStore[type].First();
            var parameters = initCtor.Value;

            if (ReferenceEquals(parameters, null))
            {
                methodBuilder = DynamicMethodBuilder.Init((ConstructorInfo)initCtor.Key, type);

                if (builderStore[type].Count > 1)
                {
                    foreach (var dependence in builderStore[type].Skip(1))
                    {
                        var info = dependence.Key;

                        methodBuilder = methodBuilder.Dup();

                        methodBuilder = InitDependence(methodBuilder, registeredTypes.ElementAt(dependence.Value[0]));

                        if (info is PropertyInfo)
                        {
                            methodBuilder = methodBuilder.AddProperty((PropertyInfo)info);
                        }

                        if (info is FieldInfo)
                        {
                            methodBuilder = methodBuilder.AddField((FieldInfo)info);
                        }
                    }
                }
            }
            else
            {
                foreach (var dependence in parameters)
                {
                    methodBuilder = InitDependence(methodBuilder, registeredTypes.ElementAt(dependence), type);
                }

                methodBuilder = methodBuilder.AddCtor((ConstructorInfo)initCtor.Key);
            }

            return(methodBuilder);
        }
Example #2
0
        private ILGenerator InitDependence(ILGenerator methodBuilder, Type type, Type initType = null)
        {
            var instructions = builderStore[type];

            var ctor = instructions.First();

            if (ReferenceEquals(ctor.Value, null))
            {
                methodBuilder = methodBuilder.AddCtor((ConstructorInfo)ctor.Key, initType);

                if (builderStore[type].Count > 1)
                {
                    foreach (var dependence in builderStore[type].Skip(1))
                    {
                        var info = dependence.Key;

                        methodBuilder = methodBuilder.Dup();

                        methodBuilder = InitDependence(methodBuilder, registeredTypes.ElementAt(dependence.Value[0]), initType);

                        if (info is PropertyInfo)
                        {
                            methodBuilder = methodBuilder.AddProperty((PropertyInfo)info);
                        }

                        if (info is FieldInfo)
                        {
                            methodBuilder = methodBuilder.AddField((FieldInfo)info);
                        }
                    }
                }
            }
            else
            {
                foreach (var dependence in ctor.Value)
                {
                    methodBuilder = InitDependence(methodBuilder, registeredTypes.ElementAt(dependence));
                }

                methodBuilder = methodBuilder.AddCtor((ConstructorInfo)ctor.Key);
            }

            return(methodBuilder);
        }