using System.Text.Json.Serialization;
public static void Main()
var myArgs = new ArgClass
Console.WriteLine(myArgs.ToArguments());
var otherArgs = new OtherArgClass
Console.WriteLine(otherArgs.ToArguments());
public interface IArguments{}
public class ArgClass : IArguments
[JsonPropertyName("propertyA")]
public string PropA {get; set;}
[JsonPropertyName("propertyA")]
public int PropB {get; set;}
public class OtherArgClass : IArguments
[JsonPropertyName("propertyD")]
public string PropD {get; set;}
[JsonPropertyName("propertyE")]
public int PropE {get; set;}
public static class IArgumentsExt
public static string ToArguments(this IArguments ArgObj)
var argString = new StringBuilder();
PropertyInfo[] props = ArgObj.GetType().GetProperties()
.Where(x => x.CustomAttributes.Any(y => y.AttributeType.Name == nameof(JsonPropertyNameAttribute)))
foreach (PropertyInfo prop in props)
string propertyName = ((JsonPropertyNameAttribute)prop.GetCustomAttribute(typeof(JsonPropertyNameAttribute))).Name;
string propertyValue = JsonSerializer.Serialize(prop.GetValue(ArgObj));
argString.Append($"-argument {propertyName}={propertyValue} ");
return argString.ToString().Trim();