Ejemplo n.º 1
0
        public ClassBuilder(Type type)
        {
            _classType = type;
            Parameter[] defParameters = GetParameters(type);

            List <Parameter> listParameters = new List <Parameter>();

            foreach (Parameter prm in defParameters)
            {
                Parameter newParam = this.OnParameterAdded(prm);
                if (newParam != null)
                {
                    listParameters.Add(newParam);
                }
            }
            Parameter[] parameters = listParameters.ToArray();
            _parameters = parameters;

            ConstructorInfo[]       constructors = type.GetConstructors();
            List <ClassConstructor> list         = new List <ClassConstructor>();

            foreach (ConstructorInfo cnst in constructors)
            {
                if (!cnst.IsPublic)
                {
                    continue;
                }
                ClassConstructor classConstructor = new ClassConstructor(cnst, parameters);
                if ((int)classConstructor.Error == -1)
                {
                    list.Add(classConstructor);
                }
            }
            _constructors = list.ToArray();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Создание экземпляра класса
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public Object CreateInstance(params Parameter[] parameters)
        {
            ParameterCollection pcs = new ParameterCollection();

            pcs.AddRange(parameters);

            List <Parameter> inputs            = new List <Parameter>();
            ClassConstructor actualConstructor = null;

            foreach (ClassConstructor constructor in Constructors)
            {
                inputs.Clear();
                bool actual = true;
                foreach (Parameter input in constructor.Parameters)
                {
                    Parameter newParam = pcs[input.Name];
                    if (newParam == null)
                    {
                        actual = false;
                        break;
                    }
                    inputs.Add(newParam);
                }
                if (actual)
                {
                    actualConstructor = constructor;
                }
            }

            if (actualConstructor == null)
            {
                throw (new Exception("Actual constructor not found."));
            }

            List <object> prmObjects = new List <object>();

            foreach (Parameter param in inputs)
            {
                //if (param.Value is Type) {
                //  ClassBuilder cb = new ClassBuilder(param.Value as Type);

                //  param.Value = cb.CreateInstance(parameters);
                //}
                if (param.Value == null)
                {
                    if (param.ClassBuilder == null)
                    {
                        if (param.DefaultType != null)
                        {
                            param.ClassBuilder = new ClassBuilder(param.DefaultType);
                        }
                        else
                        {
                            param.ClassBuilder = new ClassBuilder(param.ParameterType);
                        }
                    }
                    param.Value = param.ClassBuilder.CreateInstance(parameters);
                }
                prmObjects.Add(param.Value);
            }

            //BindingFlags bf = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

            //object obj = typeof(Indicator).GetType().
            //  Assembly.CreateInstance(_classType.FullName, false,
            //  bf, null, prmObjects.ToArray(),
            //  CultureInfo.CurrentCulture, null);
            //return obj;

            return(Activator.CreateInstance(_classType, prmObjects.ToArray()));
        }