Пример #1
0
        public static Object Construct(Type type, CelestialBody body)
        {
            // Get all methods of the object
            ConstructorInfo[] methods =
                type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            Object value = null;

            for (Int32 i = 0; i < methods.Length; i++)
            {
                // Is the method a KittopiaDestructor?
                if (!HasAttribute <KittopiaConstructor>(methods[i]))
                {
                    continue;
                }
                KittopiaConstructor attr = GetAttributes <KittopiaConstructor>(methods[i])[0];
                switch (attr.Parameter)
                {
                case KittopiaConstructor.ParameterType.Empty:
                    value = methods[i].Invoke(null);
                    break;

                case KittopiaConstructor.ParameterType.CelestialBody:
                    value = methods[i].Invoke(new Object[] { body });
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            if (value == null)
            {
                value = Activator.CreateInstance(type);
            }

            // Check if the object implements other constructors
            if (typeof(ICreatable <CelestialBody>).IsAssignableFrom(type))
            {
                ICreatable <CelestialBody> creatable = (ICreatable <CelestialBody>)value;
                creatable.Create(body);
            }
            else if (typeof(ICreatable).IsAssignableFrom(type))
            {
                ICreatable creatable = (ICreatable)value;
                creatable.Create();
            }

            return(value);
        }
Пример #2
0
            /// <summary>
            /// Calls the methods declared as KittopiaDestructor
            /// </summary>
            public static Object Construct(Type type, CelestialBody body)
            {
                // Get all methods of the object
                ConstructorInfo[] methods =
                    type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

                Object value = null;

                for (Int32 i = 0; i < methods.Length; i++)
                {
                    // Is the method a KittopiaDestructor?
                    if (HasAttribute <KittopiaConstructor>(methods[i]))
                    {
                        KittopiaConstructor attr = GetAttributes <KittopiaConstructor>(methods[i])[0];
                        if (attr.parameter == KittopiaConstructor.Parameter.Empty)
                        {
                            value = methods[i].Invoke(null);
                        }

                        if (attr.parameter == KittopiaConstructor.Parameter.CelestialBody)
                        {
                            value = methods[i].Invoke(new Object[] { body });
                        }
                    }
                }

                if (value == null)
                {
                    value = Activator.CreateInstance(type);
                }

                // Check if the object implements other constructors
                if (typeof(ICreatable <CelestialBody>).IsAssignableFrom(type))
                {
                    ICreatable <CelestialBody> creatable = (ICreatable <CelestialBody>)value;
                    creatable.Create(body);
                }
                else if (typeof(ICreatable).IsAssignableFrom(type))
                {
                    ICreatable creatable = (ICreatable)value;
                    creatable.Create();
                }

                return(value);
            }