private static void FillCaseConstructor(UnionInfo unionInfo, StringBuilder result, ClassInfo c) { result.Replace("[Constructor]", Templates.CASE_CONSTRUCTOR); result.Replace("[ConstructorArguments]", GetCaseConstructorArguments(c, unionInfo.BaseClassInfo, useBaseGenericTypes: false)); result.Replace("[BaseConstructorCall]", GetBaseConstructorCall(unionInfo.BaseClassInfo)); result.Replace("[ConstructorAssignments]", string.Join(Environment.NewLine, c.PublicProperties.Select(p => p.Key + " = " + SafeLowerCase.ToLowerCase(p.Key) + ";"))); }
public static string GenerateUnion(UnionInfo unionInfo) { //TODO: pass one stringbuilder around var result = new StringBuilder(Templates.UNION_BASE_CLASS); result.Replace("[Namespace]", unionInfo.BaseClassInfo.Namespace); result.Replace("[Usings]", GetUsings(unionInfo.BaseClassInfo)); result.Replace("[MatchMethod]", Templates.MATCH_METHOD); result.Replace("[MatchFuncs]", string.Join(",", unionInfo.CaseClassInfos.Select(GetMatchFunc))); result.Replace("[MatchCases]", string.Join("", unionInfo.CaseClassInfos.Select(GetMatchCase))); result.Replace("[DoMethod]", Templates.DO_METHOD); result.Replace("[DoActions]", string.Join(",", unionInfo.CaseClassInfos.Select(GetDoAction))); result.Replace("[DoCases]", string.Join("", unionInfo.CaseClassInfos.Select(GetDoCase))); result.Replace("[StaticConstructors]", string.Join("", unionInfo.CaseClassInfos.Select(c => GetStaticConstructor(c, unionInfo.BaseClassInfo)))); result.Replace("[Equals]", GetBaseEquals(unionInfo.BaseClassInfo)); result.Replace("[GetHashCode]", GetHashCode(unionInfo.BaseClassInfo)); result.Replace("[EqualsOperators]", Templates.BASE_EQUALS_OPERATORS); FillBaseConstructor(unionInfo.BaseClassInfo, result); result.Replace("[UnionNameG]", unionInfo.BaseClassInfo.NameWithGenerics); result.Replace("[UnionName]", unionInfo.BaseClassInfo.Name); result.Replace("[UnionNameLowerCase]", unionInfo.BaseClassInfo.LowerCaseName); foreach (var c in unionInfo.CaseClassInfos) { result.Append(Templates.CASE_CLASS); result.Replace("[Namespace]", c.Namespace); result.Replace("[Usings]", GetUsings(c)); FillCaseConstructor(unionInfo, result, c); result.Replace("[Equals]", GetCaseEquals(c)); result.Replace("[GetHashCode]", GetHashCode(c)); result.Replace("[EqualsOperators]", Templates.CASE_EQUALS_OPERATORS); result.Replace("[CaseNameG]", c.NameWithGenerics); result.Replace("[CaseName]", c.Name); } return(CodeFormatter.Format(result.ToString())); }