예제 #1
0
        public void TestClassPrintout()
        {
            var protocol = new QuickClassDesigner.ClassData();

            protocol.@namespace     = "TestableClasses";
            protocol.accessmodifier = AccessLevel.@public;
            protocol.name           = "TestableClass";
            var func = new QuickClassDesigner.FunctionData();

            func.accessmodifer = AccessLevel.@public;
            func.returnType    = typeof(void);
            func.name          = "makeMeAFunction";
            protocol.functions.Add(func);
            var src = protocol.generateSourceCode();

            Console.WriteLine(src);
            var func2 = new QuickClassDesigner.FunctionData();

            func2.accessmodifer = AccessLevel.protectedinternal;
            func2.returnType    = typeof(object[]);
            func2.overrides     = true;
            func2.name          = "makeMeAnotherFunction";
            protocol.functions.Add(func2);
            src = protocol.generateSourceCode();
            Console.WriteLine(src);
            var implement = InterfaceData.fromExistingInterface("System.Collections.IEnumerator");

            protocol.implements.Add(implement.name, implement);
            src = protocol.generateSourceCode();
            Console.WriteLine(src);
        }
예제 #2
0
        public void TestFunctionPrintout()
        {
            var func = new QuickClassDesigner.FunctionData();

            func.accessmodifer = AccessLevel.@public;
            func.returnType    = typeof(void);
            func.name          = "makeMeAFunction";
            var src = func.generateSourceCode();

            Console.WriteLine(src);
            var param1 = new ParameterData();

            param1.name = "s";
            param1.type = typeof(string);
            func.parameters.Add(param1);
            src = func.generateSourceCode();
            Console.WriteLine(src);
            var param2 = new ParameterData();

            param2.name = "num";
            param2.type = typeof(int);
            func.parameters.Add(param2);
            src = func.generateSourceCode();
            Console.WriteLine(src);
        }
예제 #3
0
        public static InterfaceData fromExistingInterface(string s)
        {
            var data = new InterfaceData();

            data.accessmodifier = AccessLevel.@public;
            var t = System.Reflection.TypeInfo.GetType(s);

            data.@namespace = t.Namespace;
            data.name       = MiscUtils.getClassName(t);
            var funcs = t.GetMethods();

            foreach (var func in funcs)
            {
                data.functions.Add(FunctionData.getDataForExistingFunction(func));
            }
            return(data);
        }
예제 #4
0
        public void TestInterfacePrintout()
        {
            var protocol = new QuickClassDesigner.InterfaceData();

            protocol.@namespace     = "TestableClasses";
            protocol.accessmodifier = AccessLevel.@public;
            protocol.name           = "ITestable";
            var func = new QuickClassDesigner.FunctionData();

            func.accessmodifer = AccessLevel.@public;
            func.returnType    = typeof(void);
            func.name          = "makeMeAFunction";
            protocol.functions.Add(func);
            var src = protocol.generateSourceCode();

            Console.WriteLine(src);
        }
예제 #5
0
        public string generateSourceCode(int separatorcount = 1)
        {
            //string spacing = separator;

            StringBuilder builder = new StringBuilder();

            //first, generate the using namespaces
            for (int i = 0; i < namespaces.Count; i++)
            {
                builder.AppendLine($"using {namespaces[i]};");
            }

            //next, get the namepace definition
            builder.AppendLine($"namespace {@namespace}");
            builder.AppendLine("{");

            //separatorcount = 1;

            //next, get the class definition
            //check if we can serialize
            if (canSerialize)
            {
                //string customparams = $"Name={customserialname})";
                builder.AppendLine($"{getSeparater(separatorcount)}[DataContract{(customserial ? $"(Name={customserialname})" : String.Empty)}]\n");
            }
            builder.Append($"{separator}{getAccessModifierString(accessmodifier)}{(this.isStatic ? " static" : "")}{(this.isSealed ? " sealed" : "")} class {name}");
            if (extends != null && implements.Count > 0)
            {
                var    enumerator = implements.Keys.GetEnumerator();
                string block      = ", ";

                /*
                 * string interfaces = implements[0].name;
                 * for (int i = 1; i < implements.Count; i++)
                 * {
                 *  String.Concat(interfaces, block, implements[i].name);
                 * }
                 */
                enumerator.MoveNext();
                string interfaces = enumerator.Current;
                while (enumerator.MoveNext())
                {
                    String.Concat(interfaces, block, enumerator.Current);
                }
                builder.AppendLine($":{getClassName(extends)}, {interfaces}");
            }
            else if (extends != null)
            {
                builder.AppendLine($":{getClassName(extends)}");
            }
            else if (implements.Count > 0)
            {
                var    enumerator = implements.Keys.GetEnumerator();
                string block      = ", ";
                enumerator.MoveNext();
                string interfaces = enumerator.Current;
                while (enumerator.MoveNext())
                {
                    String.Concat(interfaces, block, enumerator.Current);
                }
                builder.AppendLine($":{interfaces}");
            }
            else
            {
                builder.AppendLine();
            }
            builder.AppendLine($"{separator}{{");

            //next, get class fields
            foreach (var variable in this.variables)
            {
                variable.generateSourceCode(separatorcount + 1);
            }

            //next, let's not forget the constructor
            if (isSingleton)
            {
                builder.AppendLine($"{getSeparater(separatorcount + 1)}public static {this.name} instance;\n");
                builder.AppendLine($"{getSeparater(separatorcount + 1)}private {this.name}()\n{getSeparater(separatorcount + 1)}{{\n{getSeparater(separatorcount + 2)}\n{getSeparater(separatorcount + 1)}}}");
            }
            else
            {
                if (variables.Count > 0)
                {
                    builder.AppendLine();
                }
                builder.AppendLine($"{getSeparater(separatorcount + 1)}{getAccessModifierString(this.accessmodifier)} {this.name}()\n{getSeparater(separatorcount + 1)}{{\n{getSeparater(separatorcount + 2)}\n{getSeparater(separatorcount + 1)}}}");
            }
            builder.AppendLine();

            //next, let's try to add the functions
            //first, we must get all functions from interfaces
            foreach (var functionimplements in this.implements)
            {
                foreach (FunctionData func in functionimplements.Value.functions)
                {
                    builder.AppendLine(func.generateSourceCode(separatorcount + 1));
                    builder.AppendLine();
                }
            }

            //next, we get all virtual and abstract functions from the base class we can extend
            if (extends != null)
            {
                var funcs = extends.GetMethods();
                foreach (var func in funcs)
                {
                    if (func.IsVirtual || func.IsAbstract)
                    {
                        //I might change this later based on how I want to organizae everything with inheritance
                        builder.AppendLine(FunctionData.getDataForExistingFunction(func).generateSourceCode(separatorcount + 1));
                        builder.AppendLine();
                    }
                }
            }

            //last, we get all functions defined in this specific class
            foreach (FunctionData func in this.functions)
            {
                builder.AppendLine(func.generateSourceCode(separatorcount + 1));
                builder.AppendLine();
            }

            //next, append the closing brace for interface
            builder.AppendLine(getSeparater(2));
            builder.AppendLine($"{separator}}}");

            //last, append the closing brace for namespace
            builder.Append('}');

            return(builder.ToString());
        }