| |
 |
Monday, November 18, 2002 |
While trying to install some software from my new Cannon PowerShot S40 camera today I came across a new tab on the file properties dialog of Windows 2003 .NET Server. The tab is a "Compatibility" tab that I don't recall seeing previously (although it is probably on XP and I just don't remember it.) Anyway, I thought I just wanted to make a note of it. The purpose of the tab is to allow you to configure the application so that a different OS is emulated when it runs.
10:30:37 PM
|
|
I made one more modification to my Hex class. This time I added support for conversion using cast operators and the IConvertable interface. Implementing a Type Converter is one other method of conversion that I didn't implment as this seemed to be relatively trivial.
Using Implicit and Explicit Cast Operators:
public override string ToString() { return ToString(null); }
public static implicit operator string(Hex hex) { return hex.ToString(); }
public static explicit operator Hex( string hexString) { Hex hex = new Hex(); hex.Value = long.Parse(hexString, NumberStyles.HexNumber, null); return hex; }
Using IConvertible Interface:
The code below pertains to the Hex class described earlier today. The implementation of this class is to store the hex value in a long field called m_Value and then expose it as a public property called Value. In the conversion, everything works exactly as it would for a long except when converting to a string. Therefore, the IConvertable implementations are custom when strings are involved.
#region Implementation of IConvertible System.TypeCode IConvertible.GetTypeCode() { return Convert.GetTypeCode(Value); }
public string ToString(System.IFormatProvider provider) { return ToString("X", provider); }
ulong IConvertible.ToUInt64(System.IFormatProvider provider) { return Convert.ToUInt64(Value, provider); }
... // Repeat same pattern as above function using each data type // as the all simply delegate to the Convert class except for // when using a string. ... #endregion
For a full listing of the code see here.
12:58:31 AM
|
|
At the beginning of the month I described a way to convert integers to hex in .NET. As part of the discovery for this I created a Hex data value. In recent days I have expanded this Hex data value to include formatting using the .NET formatting design pattern. Here are some notes I learned along the way.
When you control the data type definition:
If you have control over the data type then you should change it to support the IFormattable interface in order to provide custom formatting. This involves adding one method
string ToString(string format, IFormatProvider formatProvider)
Using my example of a Hex data type, the implementation was simply to set the format parameter to "X" whenever it was null (or "G" for general formatting) and then pass on the implementation to Int64.ToString(string format, IFormatProvider formatProvider). The implementation is essentially as follows:
#region IFormattable Members string IFormattable.ToString(string format, IFormatProvider formatProvider) { if ( (format == null) || (format == "G")) format = "X"; return Value.ToString(format, formatProvider); } #endregion IFormattable Members
Because the "x" and "X" formatting characters are already understood by the long data type to format the number as hex, I can rely on its implmentation to do the work correctly. In this case Value is a property of my Hex class that stores the value of the hex as a long.
When you want to use custom formatting on a data type you don't control the source code to:
You can control how an existing base type is formatted, and provide additional codes for formatting, by creating a format provider class that implements ICustomFormatter and IFormatProvider. It is important to note that there is no need to do this if you control the code of the data type as shown above. Below is a sample implementation, however, note that I didn't actually do any custom formatting but left it as a TODO:
public class MyCustomFormatInfo : IFormatProvider, ICustomFormatter {
#region Implementation of IFormatProvider public object GetFormat(System.Type formatType) { // Note that the formatters provided in the // FCL use the equivalent of "this.GetType()" in // place of typeof(ICustomFormatter), however, // formatType never appears to be HexFormatInfo so // ICustomFormatter is used instead. This is also // the sample code shown in MSDN title // Customizing Format Strings if (formatType != typeof(ICustomFormatter)) return null; return this; } #endregion
#region Implementation of ICustomFormatter public string Format(string format, object arg, System.IFormatProvider formatProvider) { string formattedString;
// **************************** // TODO: Add custom formatting of the argument here // ****************************
if (arg is IFormattable) { formattedString = ((IFormattable)arg).ToString( format, formatProvider); } else if (arg != null) formattedString = arg.ToString(); else { throw new ArgumentNullException(); } return formattedString; } #endregion }
Sample code can be downloaded here.
12:12:08 AM
|
|
© Copyright 2004 Mark Michaelis.
|
|