Esempio n. 1
0
        /// <summary>
        /// Initializes the core generator description from a component instance.
        /// </summary>
        /// <param name="desc">description to initialize</param>
        /// <param name="component">component instance</param>
        public static void FromComponent(this CoreGenDescription desc, Component component)
        {
            Type type           = component.GetType();
            var  componentProps = type.GetProperties(BindingFlags.Instance |
                                                     BindingFlags.Public |
                                                     BindingFlags.NonPublic);
            var cgProps = componentProps.Where(p => Attribute.IsDefined(p, typeof(CoreGenProp)));
            List <CoreGenCommand> cmds = new List <CoreGenCommand>();

            foreach (PropertyInfo pi in cgProps)
            {
                var  cgProp    = (CoreGenProp)Attribute.GetCustomAttribute(pi, typeof(CoreGenProp));
                var  attrs     = pi.GetCustomAttributes(typeof(PresentOn), true);
                var  conds     = attrs.Select(o => ((PresentOn)o).PresenceCondition);
                bool fulfilled = !conds.Any() ||
                                 conds.Any(c => cgProps.Any(p => object.Equals(c, p.GetValue(component, new object[0]))));
                if (!fulfilled)
                {
                    continue;
                }
                object propValue    = pi.GetValue(component, new object[0]);
                string propValueStr = PropEnum.ToString(propValue, EPropAssoc.CoreGen);
                switch (cgProp.Usage)
                {
                case ECoreGenUsage.Select:
                    desc.Select(propValueStr, propValue.GetType());
                    break;

                case ECoreGenUsage.CSet:
                {
                    string propIDStr = PropEnum.ToString(pi, EPropAssoc.CoreGen);
                    desc.CSet(propIDStr, propValueStr, propValue.GetType());
                }
                break;

                default:
                    throw new NotImplementedException();
                }
            }
            desc.Generate();
        }