Example #1
0
        /// <summary>
        /// Compiles enum source code to an in-memory strongly typed Type
        /// </summary>
        /// <param name="enumSourceCode"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        private static Type CompiledStrongTypeFromEnumSourceCode(string enumSourceCode, GeneratorOptions options)
        {
            var converter = new Converter();
            var stronglyTypedSourceCode = converter.Convert(enumSourceCode, options);

            Console.WriteLine(stronglyTypedSourceCode);

            var compiler = new Compiler();
            var assembly = compiler.Compile(stronglyTypedSourceCode, options.LanguageVersion);

            var type = assembly.GetTypes().SingleOrDefault(t => !t.IsAnonymous());

            return(type);
        }
Example #2
0
 protected CSharpCodeGenerator(Type enumType, GeneratorOptions options) : base(enumType)
 {
     Options = options;
 }
        public string Convert(string clrEnumDef)
        {
            var options = new GeneratorOptions();

            return(Convert(clrEnumDef, options));
        }
        public string Convert(string clrEnumDef, GeneratorOptions options)
        {
            var factory = LanguageAbstractFactory.Create(options);

            var enumAssembly = CompileCode(clrEnumDef, factory.CodeProvider());
            var enumType     = enumAssembly
                               .GetTypes()
                               .Single(t => t.IsEnum);

            var gen = factory.CodeGenerator(enumType, options);

            var result = new StringBuilder();

            result.AppendLine(gen.UsingStatement("System"));
            result.AppendLine(gen.UsingStatement("System.Collections.Generic"));
            result.AppendLine(gen.UsingStatement("System.Linq"));
            result.AppendLine(gen.UsingStatement("System.Reflection"));
            result.AppendLine();


            result.AppendLine(gen.StartClassDefinition());
            result.AppendLine(gen.PrivateConstructor());

            result.AppendLine(gen.RegionStart("Members"));
            result.AppendLine(gen.StaticMembers());
            result.AppendLine(gen.RegionEnd());

            result.AppendLine(gen.RegionStart("All"));
            result.AppendLine(gen.AllMethod());
            result.AppendLine(gen.RegionEnd());

            result.AppendLine(gen.RegionStart("To/From String"));
            result.AppendLine(gen.ToStringMethod());
            result.AppendLine(gen.FromStringMethod());
            result.AppendLine(gen.RegionEnd());

            if (options.DbValue)
            {
                result.AppendLine(gen.RegionStart("DbValue"));
                result.AppendLine(gen.ToDbValueMethod());
                result.AppendLine(gen.FromDbValueMethod());
                result.AppendLine(gen.RegionEnd());
            }

            if (options.UnderlyingValue)
            {
                result.AppendLine(gen.RegionStart("Cast to/from Underlying Type"));
                result.AppendLine(gen.CastToUnderlyingOperator());
                result.AppendLine(gen.CastFromUnderlyingOperator());
                result.AppendLine(gen.RegionEnd());
            }

            if (options.ImplementComparable)
            {
                result.AppendLine(gen.RegionStart("IComparable"));
                result.AppendLine(gen.CompareTo());
                result.AppendLine(gen.LessThan());
                result.AppendLine(gen.LessThanOrEqual());
                result.AppendLine(gen.GreaterThan());
                result.AppendLine(gen.GreaterThanOrEqual());
                result.AppendLine(gen.RegionEnd());
            }

            result.AppendLine(gen.EndClassDefinition());

            return(result.ToString());
        }
 public MemberCSharpCodeGenerator(Type enumType, GeneratorOptions options) : base(enumType, options)
 {
 }