PM> Install-Package Restsharp.Get.AddObjectParameter
- non public properties as parameter
- Custom value formatting
- Custom parameter names
- Excluding public properties from parameter list
- Default values for parameter
public class ObjectParameterWithPublicPropertiesOnly
{
// Parameter { Name : "SimplePublicProperty" , Value : "Value33" }
public string SimplePublicProperty { get; } = "Value33";
// Parameter { Name : "ParaMeter" , Value : "Value11" }
[ParameterName("ParaMeter")]
public string CustomNamedParameter { get; } = "Value11";
// Parameter { Name : "ExplicitlyIncludedPrivateProperty" , Value : "2.343" }
[IncludeParameter]
private double ExplicitlyIncludedPrivateProperty { get; } = 2.343;
// Property will be excluded from parameter list
[ExcludeParameter]
public bool ExplicitlyExcludedPrivateProperty { get; } = true;
// Parameter { Name : "FormattedPublicProperty" , Value : "52.03" }
[ParameterFormatString("F")]
public double FormattedPublicProperty { get; } = 52.02947819434;
// Parameter { Name : "PropertywithDefaultValue" , Value : "Dracula" }
[ParameterDefaultValue("Dracula")]
public string PropertywithDefaultValue { get; } = null;
// Parameter { Name : "CustomFormattableProperty" , Value : "Formatted as SomeFormatString" }
[ParameterFormatString("SomeFormatString")]
public FormattableObject CustomFormattableProperty { get; } = new FormattableObject();
public class FormattableObject
{
public string ToString(string format) => $"Formatted as {format}";
}
// Parameter { Name : "FormateddObject" , Value : "Y" }
[ParameterFormatter(typeof(BooleanParameterFormatter))]
public bool FormateddObject { get; } = true;
public class BooleanParameterFormatter : IParameterFormatter
{
public string Format(object parameterValue)
{
var parameterBooleanValue = (bool)parameterValue;
return parameterBooleanValue ? "Y" : "N";
}
}
}
var request = new RestRequest("resource/{id}", Method.POST);
request.AddObjectParameter(new ObjectParameterWithPublicPropertiesOnly());