private void CreateClass(CodeNamespace cns, CodeDomObjectNode objectNode, CodeMemberMethod initComponentMethod)
        {
            CodeTypeDeclaration rootType;

            if (objectNode.XClassNode == null)
            {
                rootType = new CodeTypeDeclaration("My" + objectNode.Type.Name);
            }
            else
            {
                rootType = new CodeTypeDeclaration((string)((ValueNode)objectNode.XClassNode.ItemNodes[0]).Value);
            }
            // Keep a copy
            MainCodeClassName = rootType.Name;

            rootType.BaseTypes.Add(new CodeTypeReference(objectNode.Type.UnderlyingType));
            rootType.IsPartial  = true;
            rootType.Attributes = MemberAttributes.Public;
            cns.Types.Add(rootType);
            rootType.Members.Add(initComponentMethod);

            var cmf = new CodeMemberField(typeof(CultureInfo), CultureInfoString);

            cmf.InitExpression = new CodeObjectCreateExpression(typeof(CultureInfo), new CodeSnippetExpression("\"en-us\""), new CodePrimitiveExpression(false));

            rootType.Members.Add(cmf);

            RootObject = rootType;
        }
 private void GenerateUsings(CodeNamespace cns, CodeDomObjectNode objectNode)
 {
     cns.Imports.Add(new CodeNamespaceImport("System"));
     foreach (var namespaceName in NamespacesToUse.Keys)
     {
         cns.Imports.Add(new CodeNamespaceImport(namespaceName));
     }
 }
        private CodeMemberMethod CreateInitializeMethod(CodeNamespace cns, CodeDomObjectNode objectNode)
        {
            TypeConverterDictionary = new Dictionary <Type, string>();
            PublicObjects           = new Dictionary <string, CodeDomObjectNode>();

            CodeMemberMethod initComponentMethod = new CodeConstructor();

            initComponentMethod.Attributes = MemberAttributes.Public;

            var thisExpression = new CodeThisReferenceExpression();

            AddMembers(initComponentMethod, thisExpression, objectNode);
            return(initComponentMethod);
        }
Ejemplo n.º 4
0
        public override void WriteNamespace(NamespaceDeclaration namespaceDeclaration)
        {
            CodeDomObjectNode objectNode = null;

            if (writerStack.Count == 0)
            {
                objectNode = new CodeDomObjectNode();
                writerStack.Push(objectNode);
            }
            else
            {
                objectNode = writerStack.Peek() as CodeDomObjectNode;
                if (objectNode.Type != null)
                {
                    objectNode = new CodeDomObjectNode();
                    writerStack.Push(objectNode);
                }
            }
            objectNode.NamespaceNodes.Add(namespaceDeclaration.Prefix, namespaceDeclaration);
        }
Ejemplo n.º 5
0
        private void WriteObject(XamlType xamlType, bool isGetObject)
        {
            CodeDomObjectNode objectNode = null;
            var propertyNode             = writerStack.Peek() as MemberNode;

            if (writerStack.Count > 0)
            {
                objectNode = writerStack.Peek() as CodeDomObjectNode;
                if (!(objectNode != null && objectNode.NamespaceNodes.Count > 0))
                {
                    objectNode = new CodeDomObjectNode();
                    writerStack.Push(objectNode);
                }
                else
                {
                    //root node
                    objectNode.SchemaContext = SchemaContext;
                }
            }
            else
            {
                //root node
                objectNode = new CodeDomObjectNode();
                objectNode.SchemaContext = SchemaContext;
                writerStack.Push(objectNode);
            }
            objectNode.Type        = xamlType;
            objectNode.IsGetObject = isGetObject;

            if (RootNode != null)
            {
                propertyNode.ItemNodes.Add(objectNode);
            }
            else
            {
                RootNode = objectNode;
            }
        }
        private void DumpNodeTree(CodeDomObjectNode rootNode)
        {
            if (rootNode.Type != null)
            {
                Debug.WriteLine(rootNode.Type.Name);
            }

            var members = rootNode.MemberNodes;

            foreach (var member in members)
            {
                Debug.WriteLine("Member={0}, Type={1}", member.Member.Name, member.Member.Type.Name);

                foreach (var itemNode in member.ItemNodes)
                {
                    var valueNode = itemNode as ValueNode;

                    if (valueNode != null)
                    {
                        var value = valueNode.Value as string;
                        Debug.WriteLine("Underlying Type={0}, Value={1}", member.Member.Type.UnderlyingType, value);
                    }
                    else
                    {
                        var objectNode = (CodeDomObjectNode)itemNode;
                        var xamlType   = objectNode.Type;

                        if (xamlType == XamlLanguage.Static)
                        {
                            var    valueNode2        = objectNode.XPosParamsNode.ItemNodes[0] as ValueNode;
                            var    xamlTypeReference = valueNode2.Value as string;
                            string memberName        = null;
                            string typeName          = null;
                            var    period            = xamlTypeReference.IndexOf('.');
                            if (period > -1)
                            {
                                memberName = xamlTypeReference.Substring(period + 1);
                                typeName   = xamlTypeReference.Substring(0, period);
                            }
                            var resolvedType = objectNode.Resolve(typeName);
                            //TODO: don't forget to make sure a using happens for the referencedXamlType
                            var typeName2 = resolvedType != null ? resolvedType.Name : xamlTypeReference;

                            Debug.WriteLine("TypeName={0}, MemberName={1}", typeName2, memberName);
                        }
                        else if (xamlType == XamlLanguage.Null)
                        {
                            Debug.WriteLine("NULL Expression");
                        }
                        else if (xamlType == XamlLanguage.Type)
                        {
                            var valueNode2        = objectNode.XPosParamsNode.ItemNodes[0] as ValueNode;
                            var xamlTypeReference = valueNode2.Value as string;
                            var resolvedType      = objectNode.Resolve(xamlTypeReference);
                            //TODO: don't forget to make sure a using happens for the referencedXamlType
                            var typeName = resolvedType != null ? resolvedType.Name : xamlTypeReference;

                            Debug.WriteLine("Type Ref={0}", new object[] { typeName });
                        }
                        else if (xamlType == XamlLanguage.Reference)
                        {
                        }
                        else
                        {
                            if (objectNode.Type != null)
                            {
                                DumpNodeTree(objectNode);
                            }
                            else
                            {
                                DumpNodeTree(objectNode);
                            }
                        }
                    }
                }
            }
        }
        private void GenerateMemberAssignment(CodeMemberMethod initComponentMethod, MemberNode member, CodeExpression targetExpression, CodeExpression valueExpression,
                                              CodeDomObjectNode targetObjectNode)
        {
            CodeStatement cs = null;

            //if (member.Member.IsUnknown)
            //{
            //    throw new Exception("Unknown member " + member.Member.Name);
            //}
            if (member.Member == XamlLanguage.Items)
            {
                var      parentObjectNode = member.ParentObjectNode;
                XamlType parentType       = null;
                if (parentObjectNode.IsGetObject)
                {
                    parentType = parentObjectNode.ParentMemberNode.Member.Type;
                }
                else
                {
                    parentType = parentObjectNode.Type;
                }
                if (parentType.IsDictionary)
                {
                    if (!typeof(IDictionary).IsAssignableFrom(parentType.UnderlyingType))
                    {
                        throw new NotImplementedException("Support non-IDictionary adds");
                    }
                    CodeExpression keyExpression;
                    if (targetObjectNode.XKeyNode != null)
                    {
                        keyExpression = new CodeSnippetExpression("\"" + ((ValueNode)targetObjectNode.XKeyNode.ItemNodes[0]).Value + "\"");
                    }
                    else
                    {
                        if (targetObjectNode.DictionaryKeyProperty == null)
                        {
                            throw new NotSupportedException("No key on dictionary entry");
                        }
                        throw new NotImplementedException();
                    }
                    cs = new CodeExpressionStatement(
                        new CodeMethodInvokeExpression(new CodeCastExpression(typeof(IDictionary), targetExpression), "Add", keyExpression, valueExpression));
                }
                else
                {
                    if (!typeof(IList).IsAssignableFrom(parentType.UnderlyingType))
                    {
                        throw new NotImplementedException("Support non-IList adds");
                    }
                    //TODO: calling Add directly is how I'll leave it for now...
                    //cs = new CodeExpressionStatement(new CodeMethodInvokeExpression(new CodeCastExpression(typeof(IList), targetExpression), "Add", valueExpression));
                    cs = new CodeExpressionStatement(new CodeMethodInvokeExpression(targetExpression, "Add", valueExpression));
                }
            }
            else if (member.Member.IsEvent)
            {
                throw new NotImplementedException();
            }
            else
            {
                if (member.Member.IsAttachable)
                {
                    cs = new CodeExpressionStatement(new CodeMethodInvokeExpression(
                                                         new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(member.Member.DeclaringType.UnderlyingType.Name),
                                                                                           "Set" + member.Member.Name), targetExpression, valueExpression));
                }
                else //normal property
                {
                    cs = new CodeAssignStatement(
                        new CodePropertyReferenceExpression(targetExpression, member.Member.Name), valueExpression);
                }
            }
            initComponentMethod.Statements.Add(cs);
        }
        private void GenerateMemberAssignmentWithProvideValueIfNecessary(CodeMemberMethod initComponentMethod, CodeExpression parentExpression, CodeDomObjectNode targetObjectNode,
                                                                         MemberNode parentMember, CodeVariableReferenceExpression cvre)
        {
            //if (parentMember.Member.IsUnknown)
            //{
            //    throw new Exception("Unknown member " + parentMember.Member.Name);
            //}
            var xamlType = targetObjectNode.Type;

            if (xamlType.IsMarkupExtension)
            {
                GenerateMemberAssignment(initComponentMethod, parentMember, parentExpression,
                                         new CodeMethodInvokeExpression(cvre, "ProvideValue", new CodeVariableReferenceExpression("context"))
                                         , targetObjectNode);
            }
            else
            {
                GenerateMemberAssignment(initComponentMethod, parentMember, parentExpression, cvre, targetObjectNode);
            }
        }
        private void GenerateObject(CodeMemberMethod initComponentMethod, CodeExpression parentExpression, CodeDomObjectNode targetObjectNode, MemberNode parentMember)
        {
            string targetName;
            var    isPublicObject = false;

            if (targetObjectNode.XNameNode == null)
            {
                targetName = GenerateUniqueName(targetObjectNode.Type);
            }
            else
            {
                var valueNode = targetObjectNode.XNameNode.ItemNodes[0] as ValueNode;
                targetName     = (string)valueNode.Value;
                isPublicObject = true;
            }
            CodeExpression ctor = null;

            if (targetObjectNode.XInitNode != null)
            {
                var tcName = GenerateTypeConverter(initComponentMethod, targetObjectNode.Type); // TextSyntax

                ctor = GetTypeConverteredValue(targetObjectNode.Type, ((ValueNode)targetObjectNode.XInitNode.ItemNodes[0]).Value, targetObjectNode.Type.UnderlyingType, tcName);
            }
            else if (targetObjectNode.XFactoryMethodNode != null)
            {
                throw new NotImplementedException();
            }
            else if (targetObjectNode.XPosParamsNode != null)
            {
                var types       = targetObjectNode.Type.GetPositionalParameters(targetObjectNode.XPosParamsNode.ItemNodes.Count);
                var constructor = new CodeObjectCreateExpression(targetObjectNode.Type.UnderlyingType.Name);
                for (var i = 0; i < types.Count; i++)
                {
                    var tcName = GenerateTypeConverter(initComponentMethod, types[i]);

                    constructor.Parameters.Add(GetTypeConverteredValue(types[i], ((ValueNode)targetObjectNode.XPosParamsNode.ItemNodes[i]).Value, types[i].UnderlyingType, tcName));
                }
                ctor = constructor;
            }
            else if (targetObjectNode.XArgumentsNode != null)
            {
                throw new NotImplementedException();
            }
            else
            {
                if (targetObjectNode.Type.IsUnknown)
                {
                    throw new Exception("Unknown type " + targetObjectNode.Type.Name + " found.");
                }
                ctor = new CodeObjectCreateExpression(targetObjectNode.Type.UnderlyingType.Name);
            }

            if (!isPublicObject)
            {
                var cvds = new CodeVariableDeclarationStatement(targetObjectNode.Type.UnderlyingType.Name, targetName, ctor);
                initComponentMethod.Statements.Add(cvds);
            }
            else
            {
                var cas = new CodeAssignStatement(new CodeVariableReferenceExpression(targetName), ctor);
                initComponentMethod.Statements.Add(cas);

                // Needs to be public
                PublicObjects.Add(targetName, targetObjectNode);
            }

            var cvre = new CodeVariableReferenceExpression(targetName);

            var isUsuable = GetIsUsableDuringInitialization(targetObjectNode.Type);

            if (isUsuable)
            {
                GenerateMemberAssignmentWithProvideValueIfNecessary(initComponentMethod, parentExpression, targetObjectNode, parentMember, cvre);
            }

            AddMembers(initComponentMethod, cvre, targetObjectNode);

            if (!isUsuable)
            {
                GenerateMemberAssignmentWithProvideValueIfNecessary(initComponentMethod, parentExpression, targetObjectNode, parentMember, cvre);
            }
        }
        private void GenerateGetObject(CodeMemberMethod initComponentMethod, CodeExpression parentExpression, CodeDomObjectNode targetObjectNode, MemberNode parentMember)
        {
            if (parentMember.Member.IsAttachable)
            {
                throw new NotImplementedException();
            }
            var cpfe       = new CodePropertyReferenceExpression(parentExpression, parentMember.Member.Name);
            var type       = parentMember.Member.Type.UnderlyingType;
            var targetName = GenerateUniqueName(type);
            var cvds       = new CodeVariableDeclarationStatement(type.Name, targetName, cpfe);

            initComponentMethod.Statements.Add(cvds);

            var cvre = new CodeVariableReferenceExpression(targetName);

            AddMembers(initComponentMethod, cvre, targetObjectNode);
        }
        //  MyData myDataObject = new MyData(DateTime.Now);
        //  Binding myBinding = new Binding("MyDataProperty");
        //  myBinding.Source = myDataObject;
        //  myText.SetBinding(TextBlock.TextProperty, myBinding);

        private void GenerateBindingObject(CodeMemberMethod initComponentMethod, CodeExpression parentExpression, CodeDomObjectNode targetObjectNode, MemberNode parentMember)
        {
            // Generate new binding name
            var targetName  = GenerateUniqueName(targetObjectNode.Type);
            var elementName = string.Empty;
            var path        = string.Empty;

            // Grab all the attributes of the binding
            var members = targetObjectNode.MemberNodes;

            foreach (var member in members)
            {
                switch (member.Member.Name)
                {
                case "ElementName":
                    elementName = ExtractItemMemberValue(member.ItemNodes[0]);
                    break;

                case "Path":
                    path = ExtractItemMemberValue(member.ItemNodes[0]);
                    break;
                }
            }

            // Create a constructor for the binding adding the Path, if it exists
            CodeExpression ctor = new CodeObjectCreateExpression(targetObjectNode.Type.UnderlyingType.Name, new CodePrimitiveExpression(path));
            var            cvds = new CodeVariableDeclarationStatement(targetObjectNode.Type.UnderlyingType.Name, targetName, ctor);

            initComponentMethod.Statements.Add(cvds);

            // Set the source property on the binding
            CodeStatement cs = new CodeAssignStatement(new CodePropertyReferenceExpression(new CodeVariableReferenceExpression(targetName), "Source"),
                                                       new CodeVariableReferenceExpression(elementName));

            initComponentMethod.Statements.Add(cs);

            // Set the binding on the target object
            var cfre = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(parentMember.ParentObjectNode.Type.UnderlyingType), parentMember.Member.Name + "Property");

            var cmie = new CodeMethodInvokeExpression(parentExpression, "SetBinding", cfre, new CodeVariableReferenceExpression(targetName));

            initComponentMethod.Statements.Add(cmie);
        }
        private void AddMembers(CodeMemberMethod initComponentMethod, CodeExpression targetExpression, CodeDomObjectNode objectNode)
        {
            var members = objectNode.MemberNodes;

            foreach (var member in members)
            {
                if (member.Member.Name == "Implementation")
                {
                    GenerateImplementation(initComponentMethod, member, targetExpression);
                }
                else
                {
                    GenerateMemberValue(initComponentMethod, member, targetExpression);
                }
            }
        }