예제 #1
0
        public void ExportCSharpData()
        {
            string code = LoadCode();

            if (code == null)
            {
                return;
            }

            ClassType ctype = getClassType();

            string _GetValueMethodName    = cmd.GetValue("method");
            string _ConstKeyClassName     = cmd.GetValue("kc");
            string _DefaultValueClassName = cmd.GetValue("dc");

            var builder = new CSharpBuilder {
                Namespace = NamespaceName
            };

            builder.AddUsing("System");
            builder.AddUsing("System.Collections.Generic");
            string cname = ClassName;

            if (ctype == ClassType.TieDataContract || ctype == ClassType.JsonDataContract)
            {
                bool isExpression = ctype == ClassType.JsonDataContract;

                string inputPath = cmd.InputPath();
                if (inputPath != null && Path.GetExtension(inputPath).ToLower() == ".json")
                {
                    isExpression = true;
                }

                ConvertJson2CS(code, builder, cname, isExpression);
                return;
            }

            var maker = new ConfigScript(code);

            if ((ctype & ClassType.HierarchicalProperty) == ClassType.HierarchicalProperty)
            {
                maker.HierarchicalMemberType = CodeMemberType.Property;
            }
            else if ((ctype & ClassType.HierarchicalMethod) == ClassType.HierarchicalMethod)
            {
                maker.HierarchicalMemberType = CodeMemberType.Method;
            }
            else
            {
                maker.HierarchicalMemberType = CodeMemberType.Field;
            }

            if (_GetValueMethodName != null)
            {
                maker.GetValueMethodName = _GetValueMethodName;
            }

            if (_ConstKeyClassName != null)
            {
                maker.ConstKeyClassName = _ConstKeyClassName;
            }

            if (_DefaultValueClassName != null)
            {
                maker.DefaultValueClassName = _DefaultValueClassName;
            }

            var clss = maker.Generate(cname);

            builder.AddClass(clss);

            if (ctype == ClassType.ConstKey)
            {
                builder = CreateClass(maker.ConstKeyFields);
            }
            else if (ctype == ClassType.DefaultValue)
            {
                builder = CreateClass(maker.DefaultValueFields);
            }
            else if (ctype == ClassType.StaticField)
            {
                builder = CreateClass(maker.StaticFields);
            }
            else if (ctype == ClassType.StaticPropery)
            {
                builder = CreateClass(maker.StaticProperties);
            }
            else if (ctype == ClassType.StaticMethod)
            {
                builder = CreateClass(maker.StaticMethods);
            }
            else if (ctype == ClassType.HierarchicalField || ctype == ClassType.HierarchicalProperty || ctype == ClassType.HierarchicalMethod)
            {
                //skip, because clss has created class already
            }
            else
            {
                if ((ctype & ClassType.HierarchicalField) != ClassType.HierarchicalField &&
                    (ctype & ClassType.HierarchicalProperty) != ClassType.HierarchicalProperty &&
                    (ctype & ClassType.HierarchicalMethod) != ClassType.HierarchicalMethod
                    )
                {
                    clss.Clear();
                }

                if ((ctype & ClassType.StaticField) == ClassType.StaticField)
                {
                    clss.AddRange(maker.StaticFields);
                }

                if ((ctype & ClassType.StaticPropery) == ClassType.StaticPropery)
                {
                    clss.AddRange(maker.StaticProperties);
                }

                if ((ctype & ClassType.StaticMethod) == ClassType.StaticMethod)
                {
                    clss.AddRange(maker.StaticMethods);
                }

                if ((ctype & ClassType.ConstKey) == ClassType.ConstKey)
                {
                    clss.AddRange(maker.ConstKeyFields);
                }

                if ((ctype & ClassType.DefaultValue) == ClassType.DefaultValue)
                {
                    clss.AddRange(maker.DefaultValueFields);
                }
            }

            builder.AddUsingRange(base.Usings);
            PrintOutput(builder, cname);
        }