public string GetTextFromType(Type type)
        {
            string outputText = String.Empty;

            if (type.IsClass || type.IsInterface)
            {
                var generator = new ClassGenerator();
                return(generator.GetTextFromType(type));
            }
            else if (type.IsEnum)
            {
                var generator = new EnumGenerator();
                return(generator.GetTextFromType(type));
            }
            else if (type.IsValueType)
            {
                var generator = new StructGenerator();
                return(generator.GetTextFromType(type));
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("Type not supported " + type.FullName);
            }
            return(string.Empty);
        }
Esempio n. 2
0
        public static void Run(string path, string assemblyName)
        {
            string code = ClassGenerator.GetClass();

            var assembly = CSharpCompilation
                           .Create(assemblyName).WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
                           .AddReferences(MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location))
                           .AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(code));

            var compilationResult = assembly.Emit(path, assemblyName);

            if (!compilationResult.Success)
            {
                compilationResult.Diagnostics.ToList().ForEach(issue =>
                                                               Console.WriteLine(
                                                                   $"ID: {issue.Id}, Message: {issue.GetMessage()}, Location: { issue.Location.GetLineSpan()}, Severity: { issue.Severity}")
                                                               );
            }
        }