public override string ToDbValueMethod() { var code = new CSharpBuilder(Options.LanguageVersion); code.Indent(1).AppendLine("private readonly string _dbValue;"); code.Indent(1).Append("public string ToDbValue()") .ExpressionBody("_dbValue"); return(code.ToString()); }
public override string CastToUnderlyingOperator() { var code = new CSharpBuilder(Options.LanguageVersion); code.Indent(1).AppendLine($"private readonly {UnderlyingTypeName} _value;"); code.Indent(1).Append($"public static explicit operator {UnderlyingTypeName}({TypeName} value)") .ExpressionBody("value._value"); return(code.ToString()); }
protected string Indent(int count) { var code = new CSharpBuilder(Options.LanguageVersion); code.Indent(count); return(code.ToString()); }
public override string PrivateConstructor() { var code = new CSharpBuilder(Options.LanguageVersion); code.Indent(1).AppendLine($"private {TypeName}() {{ }}"); return(code.ToString()); }
public override string RegionStart(string regionName) { var code = new CSharpBuilder(Options.LanguageVersion); code.Indent(1).AppendLine($"#region {regionName}"); return(code.ToString()); }
public override string RegionEnd() { var code = new CSharpBuilder(Options.LanguageVersion); code.Indent(1).AppendLine("#endregion"); code.AppendLine(); return(code.ToString()); }
public override string GreaterThanOrEqual() { var code = new CSharpBuilder(Options.LanguageVersion); code.Indent(1).Append($"public static bool operator >=({TypeName} lhs, {TypeName} rhs)") .ExpressionBody("lhs.CompareTo(rhs) >= 0"); return(code.ToString()); }
public override string AllMethod() { var code = new CSharpBuilder(Options.LanguageVersion); code.Indent(1).Append($"public static IEnumerable<{TypeName}> All()") .ExpressionBody($"All<{TypeName}>()"); code.AppendLine(); code.Indent(1).AppendLine("private static IEnumerable<T> All<T>()"); code.Indent(1).AppendLine("{"); code.Indent(2).AppendLine("var type = typeof(T);"); code.Indent(2).AppendLine("return type.GetFields(BindingFlags.Public | BindingFlags.Static)"); code.Indent(3).AppendLine(".Where(field => field.IsInitOnly)"); code.Indent(3).AppendLine(".Where(field => field.FieldType == type)"); code.Indent(3).AppendLine(".Select(field => field.GetValue(null))"); code.Indent(3).AppendLine(".Cast<T>();"); code.Indent(1).AppendLine("}"); return(code.ToString()); }
public override string CastToUnderlyingOperator() { var code = new CSharpBuilder(Options.LanguageVersion); code.Indent(1).Append($"private static readonly Dictionary<{TypeName}, {UnderlyingTypeName}> UnderlyingMap") .AppendLine($" = new Dictionary<{TypeName}, {UnderlyingTypeName}>"); code.Indent(1).AppendLine("{"); var castIntMappings = Members .Select(member => $"{{{member.Name}, {Convert.ChangeType(member.GetValue(null), UnderlyingType)}}}") .ToArray(); code.Indent(2); code.AppendLine(string.Join($",\r\n{Indent(2)}", castIntMappings)); code.Indent(1).AppendLine("};"); code.AppendLine(); code.Indent(1).Append($"public static explicit operator {UnderlyingTypeName}({TypeName} value)") .ExpressionBody("UnderlyingMap[value]"); return(code.ToString()); }
public override string ToStringMethod() { var code = new CSharpBuilder(Options.LanguageVersion); code.Indent(1) .AppendLine($"private static readonly Dictionary<{TypeName}, string> ToStringMap = new Dictionary<{TypeName}, string>"); code.Indent(1).AppendLine("{"); var toStringMappings = MemberNames .Select(memberName => $"{{{memberName}, {NameOf(memberName)}}}") .ToArray(); code.Indent(2); code.AppendLine(string.Join($",\r\n{Indent(2)}", toStringMappings)); code.Indent(1).AppendLine("};"); code.AppendLine(); code.Indent(1).Append("public override string ToString()") .ExpressionBody("ToStringMap[this]"); return(code.ToString()); }
public override string StaticMembers() { var code = new CSharpBuilder(Options.LanguageVersion); foreach (var memberName in MemberNames) { code.Indent(1).Append($"public static readonly {TypeName} {memberName}"); code.AppendLine($" = new {TypeName}();"); } return(code.ToString()); }
public override string PrivateConstructor() { var code = new CSharpBuilder(Options.LanguageVersion); code.Indent(1).Append($"private {TypeName}(string name"); if (Options.DbValue) { code.Append(", string dbValue"); } if (Options.UnderlyingValue) { code.Append($", {UnderlyingTypeName} value"); } code.AppendLine(")"); code.Indent(1).AppendLine("{"); code.Indent(2).AppendLine("_name = name;"); if (Options.DbValue) { code.Indent(2).AppendLine("_dbValue = dbValue;"); } if (Options.UnderlyingValue) { code.Indent(2).AppendLine("_value = value;"); } code.Indent(1).AppendLine("}"); return(code.ToString()); }
public override string FromStringMethod() { var code = new CSharpBuilder(Options.LanguageVersion); code.Indent(1).AppendLine($"public static {TypeName} FromString(string value)"); code.Indent(1).AppendLine("{"); code.Indent(2).AppendLine($"if (value == null) throw new ArgumentNullException({NameOf("value")});"); code.AppendLine(); code.Indent(2).AppendLine("var result = All().FirstOrDefault(x => x.ToString() == value);"); code.Indent(2).AppendLine("if (result != null) return result;"); code.AppendLine(); code.Indent(2).AppendLine($"throw new ArgumentOutOfRangeException({NameOf("value")}, value, \"Invalid {TypeName}\");"); code.Indent(1).AppendLine("}"); return(code.ToString()); }
public override string CastFromUnderlyingOperator() { var code = new CSharpBuilder(Options.LanguageVersion); code.Indent(1).AppendLine($"public static explicit operator {TypeName}({UnderlyingTypeName} value)"); code.Indent(1).AppendLine("{"); code.Indent(2).AppendLine($"var result = All().FirstOrDefault(x => ({UnderlyingTypeName}) x == value);"); code.Indent(2).AppendLine("if (result != null) return result;"); code.AppendLine(); code.Indent(2).AppendLine($"throw new InvalidCastException(\"The value \" + value + \" is not a valid {TypeName}\");"); code.Indent(1).AppendLine("}"); return(code.ToString()); }
public override string StaticMembers() { var code = new CSharpBuilder(Options.LanguageVersion); foreach (var member in Members) { var memberValue = Convert.ChangeType(member.GetValue(null), UnderlyingType); code.Indent(1).Append($"public static readonly {TypeName} {member.Name}"); code.Append($" = new {TypeName}("); code.Append($"{NameOf(member.Name)}"); if (Options.DbValue) { code.Append($", \"{DbValue(member.Name)}\""); } if (Options.UnderlyingValue) { code.Append($", {memberValue}"); } code.AppendLine(");"); } return(code.ToString()); }
public override string CompareTo() { var code = new CSharpBuilder(Options.LanguageVersion); code.Indent(1).AppendLine($"public int CompareTo({TypeName} other)"); code.Indent(1).AppendLine("{"); code.Indent(2).AppendLine("var results = new[]"); code.Indent(2).AppendLine("{"); if (Options.UnderlyingValue) { code.Indent(3).AppendLine($"(({UnderlyingTypeName}) this).CompareTo(({UnderlyingTypeName}) other)"); } else { code.Indent(3).AppendLine("ToString().CompareTo(other.ToString())"); } code.Indent(2).AppendLine("};"); code.Indent(2).AppendLine("return results"); code.Indent(3).AppendLine(".SkipWhile(diff => diff == 0)"); code.Indent(3).AppendLine(".FirstOrDefault();"); code.Indent(1).AppendLine("}"); return(code.ToString()); }