Exemplo n.º 1
0
        /// <summary>
        /// 创建字段列表
        /// </summary>
        /// <param name="classNode"></param>
        /// <param name="components"></param>
        private static void CreateMemberFields(TypeDeclaration classNode, ComponentItem[] components)
        {
            foreach (var item in components)
            {
                var fieldName = string.Format("m_" + item.name);
                var field     = classNode.Descendants.OfType <FieldDeclaration>().Where(x => x.Variables.Where(y => y.Name == fieldName).Count() > 0).FirstOrDefault();

                if (field != null && (field.ReturnType).ToString() != item.componentType.Name)
                {
                    classNode.Members.Remove(field);
                    field = null;
                }

                if (field == null)
                {
                    field            = new FieldDeclaration();
                    field.Modifiers  = Modifiers.Private;
                    field.ReturnType = new BridgeUI.NRefactory.CSharp.PrimitiveType(item.componentType.FullName);
                    field.Variables.Add(new VariableInitializer(fieldName));
                    var att = new BridgeUI.NRefactory.CSharp.Attribute();
                    att.Type = new SimpleType("SerializeField");
                    field.Attributes.Add(new AttributeSection(att));
                    classNode.AddChild(field, Roles.TypeMemberRole);
                }
            }
        }
        /// <summary>
        /// 向viewModel添加属性
        /// </summary>
        /// <param name="type"></param>
        /// <param name="source"></param>
        /// <param name="beforeNode"></param>
        /// <param name="classNode"></param>
        /// <returns></returns>
        private static AstNode InsertPropertyToClassNode(Type type, string source, AstNode beforeNode, TypeDeclaration classNode)
        {
            var propertyNode = classNode.Descendants.OfType <PropertyDeclaration>().Where(x => x.Name == source).FirstOrDefault();

            if (propertyNode != null)
            {
                classNode.Members.Remove(propertyNode);
            }

            var typeName = TypeStringName(type);

            propertyNode = new PropertyDeclaration();
            var att = new BridgeUI.NRefactory.CSharp.Attribute();

            att.Type = new SimpleType(typeof(BridgeUI.Attributes.DefultValueAttribute).FullName);
            propertyNode.Attributes.Add(new AttributeSection(att));
            propertyNode.Modifiers  = Modifiers.Public;
            propertyNode.ReturnType = new PrimitiveType(string.Format("{0}.{1}", type.Namespace, typeName));
            propertyNode.Name       = source;

            #region Getter
            var accessor      = propertyNode.Getter = new Accessor();
            var body          = accessor.Body = new BlockStatement();
            var expression    = new IdentifierExpression("GetValue");
            var typeArguement = new MemberType(new SimpleType(type.Namespace), typeName);
            expression.TypeArguments.Add(typeArguement);
            Statement statement = new ReturnStatement(new InvocationExpression(expression, new PrimitiveExpression(source)));
            body.Add(statement);
            #endregion

            #region Setter
            accessor      = propertyNode.Setter = new Accessor();
            body          = accessor.Body = new BlockStatement();
            expression    = new IdentifierExpression("SetValue");
            typeArguement = new MemberType(new SimpleType(type.Namespace), typeName);
            expression.TypeArguments.Add(typeArguement);
            statement = new ExpressionStatement(new InvocationExpression(expression, new PrimitiveExpression(source), new IdentifierExpression("value")));
            body.Add(statement);
            #endregion

            classNode.InsertChildBefore(beforeNode, propertyNode, Roles.TypeMemberRole);
            return(propertyNode);
        }