HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpMinor

Extendable format provider

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
extendableformatprovider

Problem

I needed a better string formatting options like ToUpper and ToLower but I also wanted to be able to use the default formattings so I created a custom formatter. It's main purpose is to support a template engine where the user should be able to use different formattings like upper or lower case. I still however couldn't figure out how to be make the date time upper case.

```
public class ExtendableFormatter : IFormatProvider, ICustomFormatter
{
public IDictionary FormatProviders { get; set; }

public ExtendableFormatter()
{
FormatProviders = new Dictionary
{
{ typeof(Byte), CultureInfo.InvariantCulture.NumberFormat },
{ typeof(SByte), CultureInfo.InvariantCulture.NumberFormat },
{ typeof(Int16), CultureInfo.InvariantCulture.NumberFormat },
{ typeof(Int32), CultureInfo.InvariantCulture.NumberFormat },
{ typeof(Int64), CultureInfo.InvariantCulture.NumberFormat },
{ typeof(UInt16), CultureInfo.InvariantCulture.NumberFormat },
{ typeof(UInt32), CultureInfo.InvariantCulture.NumberFormat },
{ typeof(UInt64), CultureInfo.InvariantCulture.NumberFormat },
{ typeof(Single), CultureInfo.InvariantCulture.NumberFormat },
{ typeof(Double), CultureInfo.InvariantCulture.NumberFormat },
{ typeof(Decimal), CultureInfo.InvariantCulture.NumberFormat },
{ typeof(DateTime), CultureInfo.InvariantCulture.DateTimeFormat },
{ typeof(String), new StringCaseFormatter() }
};
}

public object GetFormat(Type formatType)
{
return formatType == typeof(ICustomFormatter) ? this : null;
}

public string Format(string format, object arg, IFormatProvider formatProvider)
{
var fp = (IFormatProvider)null;
if (FormatProviders.TryGetValue(arg.GetType(), out fp))
{
formatProvider = fp;
}

format = string.IsNullOrEmpty(format) ? stri

Solution

public IDictionary FormatProviders { get; set; }


This is bad. Having a public setter for the dictionary will let a user of that code pass in null resulting in the Format() method throwing a NullReferenceException.

You don't check the passed in parameters of the Format() method if they are valid. By omiting the validation of the arguments you are exposing implementation details for the case they aren't valid. E.g if the passed object arg is null then the call to arg.GetType() will throw and exposes that you are requesting the type of that object.

You only want to show the user that the passed in arguments are invalid, but you shouldn't let the code do it for you.

Code Snippets

public IDictionary<Type, IFormatProvider> FormatProviders { get; set; }

Context

StackExchange Code Review Q#131222, answer score: 4

Revisions (0)

No revisions yet.