示例#1
0
        public static ClassGenerationData ExtractClassGenerationData(string code)
        {
            var data = new ClassGenerationData();
            var root = CodeReaderUtility.GetCompilationUnitSyntaxFromCode(code);

            var @namespace = root.DescendantNodes().FirstOrDefault(x => x is NamespaceDeclarationSyntax) as NamespaceDeclarationSyntax;

            if (@namespace != null)
            {
                data.m_Namespace = @namespace.Name.ToString();
            }

            var @class = CodeReaderUtility.GetFirstOrDefaultFromRoot <ClassDeclarationSyntax>(root);

            data.m_ClassName       = @class.Identifier.ToString();
            data.m_ClassAttributes = ExtractAttributes(@class.AttributeLists.ToArray());

            // TODO: Extract constraints

            data.m_BaseClasses = @class.BaseList.Types.Select(x => x.ToString()).ToArray();

            // this takes in specifically EITHER the namespace's OR the root's usings.
            data.m_Usings = ExtractUsings(@namespace != null ? @namespace.Usings.ToArray() : root.Usings.ToArray()).ToArray();

            var fields = CodeReaderUtility.GetArrayFromRoot <FieldDeclarationSyntax>(root);

            data.m_FieldGenerationDatas = ExtractFields(fields);

            var methods = CodeReaderUtility.GetArrayFromRoot <MethodDeclarationSyntax>(root);

            data.m_MethodGenerationDatas = ExtractMethods(methods);

            return(data);
        }
示例#2
0
        private static ClassDeclarationSyntax CreateClassUsingClassGenerationData(ClassGenerationData data)
        {
            var classDeclaration = CreateClassDeclarationSyntax(data.m_ClassName, data.m_BaseClasses, data.m_ClassType, data.m_Constraints);

            classDeclaration = classDeclaration.AddAttributeLists(AttributeGenerationService.CreateAttributeListSyntaxes(data.m_ClassAttributes));
            classDeclaration = classDeclaration.AddMembers(FieldGenerationService.CreateFieldDeclarationSyntaxes(data.m_FieldGenerationDatas));

            // a more freeform type of method generation
            for (int i = 0; i < data.m_MethodGenerationDatas.Count; i++)
            {
                classDeclaration = classDeclaration.AddMembers(MethodGenerationService.CreateMethod(data.m_MethodGenerationDatas[i]));
            }

            return(classDeclaration);
        }
示例#3
0
        private static string CreateClass(ClassGenerationData classGenerationData, PreservedClassData preservedClassData)
        {
            //  Create a class: (class TestClass)
            var classDeclaration = CreateClassUsingClassGenerationData(classGenerationData);
            var @namespace       = CreateNamespaceDeclarationSyntax(classGenerationData.m_Namespace, classGenerationData.m_Usings);

            if (preservedClassData != null)
            {
                classDeclaration = AppendClassWithPreservedData(classDeclaration, preservedClassData);
            }

            // Add the class to the namespace.
            @namespace = @namespace.AddMembers(classDeclaration);

            // Normalize and get code as string.
            var code = @namespace
                       .NormalizeWhitespace()
                       .ToFullString();

            return(code);
        }
示例#4
0
        public static string CreateClass(ClassGenerationData classGenerationData, string codeWithPreservedData = null)
        {
            var preservedData = new PreservedClassData(codeWithPreservedData);

            return(CreateClass(classGenerationData, preservedData));
        }