public void MakeExtensionMethod(CsClass target) { if (EnumOptions is null) { return; } var paramValue = target.GetTypeName(TypeName); var writer = CsCodeWriter.Create <ShellEnumOptionsGenerator>(); writer.Open("switch (value)"); foreach (var i in EnumOptions.Items) { var line = $"case {paramValue}.{i.CsValue}: return {i.LinuxValue.CsEncode()};"; writer.WriteLine(line); } var notSupportedException = target.GetTypeName <NotSupportedException>(); writer.WriteLine($"default: throw new {notSupportedException}();"); writer.Close(); var parameter = new CsMethodParameter("value", paramValue) { UseThis = target.IsStatic }; target.AddMethod(extensionMethodName, "string") .WithBody(writer) .WithStatic() .WithParameter(parameter); }
public string GetTypeName(CsClass str, NamespaceAndName valueEnumTypeName) { if (FixedType != null) { return(str.GetTypeName(FixedType)); } return(str.GetTypeName(valueEnumTypeName)); }
private static void AddConstructor(CsClass csStruct, KeysGeneratorDef def) { var propName = def.ValuePropertyName; var argName = propName.FirstLower(); var cs = new CsCodeWriter(); if (def.WrappedType == WrappedTypes.String) { var args = new CsArguments($"nameof({argName})"); var exception = args.Create(csStruct.GetTypeName <NullReferenceException>()); cs.SingleLineIf($"{argName} is null", $"throw {exception};"); exception = args.Create(csStruct.GetTypeName <ArgumentException>()); cs.SingleLineIf($"{argName}.Length == 0", $"throw {exception};"); cs.WriteAssign(propName, $"{argName}.Trim()"); } else { cs.WriteAssign(propName, argName); } csStruct.AddConstructor() .WithParameter(new CsMethodParameter(argName, def.CsWrappedType)) .WithBody(cs); for (var i = 0; i < 2; i++) { var eq = i == 0; var m = csStruct.AddMethod(eq ? "==" : "!=", "bool", eq ? "Equality operator" : "Inequality operator") .WithBody($"return {(eq ? "" : "!")}left.Equals(right);"); m.AddParam("left", csStruct.Name, "first value to compare"); m.AddParam("right", csStruct.Name, "second value to compare"); } }