Area di riferimento
- Developing applications that use system types and collections
- Implement .NET Framework interfaces to cause components to comply with standard contracts. (Refer System namespace)
- IConvertible interface
- IFormattable interface
IConvertible interface
L'interfaccia IConvertible fornisce metodi per convertire un oggetto in un tipo del CLR che ha un valore in qualche modo equivalente.
[ComVisible(true), CLSCompliant(false)]
public interface IConvertible
{
// Methods
TypeCode GetTypeCode();
bool ToBoolean(IFormatProvider provider);
byte ToByte(IFormatProvider provider);
char ToChar(IFormatProvider provider);
DateTime ToDateTime(IFormatProvider provider);
decimal ToDecimal(IFormatProvider provider);
double ToDouble(IFormatProvider provider);
short ToInt16(IFormatProvider provider);
int ToInt32(IFormatProvider provider);
long ToInt64(IFormatProvider provider);
sbyte ToSByte(IFormatProvider provider);
float ToSingle(IFormatProvider provider);
string ToString(IFormatProvider provider);
object ToType(Type conversionType, IFormatProvider provider);
ushort ToUInt16(IFormatProvider provider);
uint ToUInt32(IFormatProvider provider);
ulong ToUInt64(IFormatProvider provider);
}
Di seguito riporto un esempio tratto dalla documentazione msdn:
/// Class that implements IConvertible
class Complex : IConvertible
{
double x;
double y;
public Complex(double x, double y)
{
this.x = x;
this.y = y;
}
public TypeCode GetTypeCode()
{
return TypeCode.Object;
}
bool IConvertible.ToBoolean(IFormatProvider provider)
{
if( (x != 0.0) || (y != 0.0) )
return true;
else
return false;
}
double GetDoubleValue()
{
return Math.Sqrt(x*x + y*y);
}
byte IConvertible.ToByte(IFormatProvider provider)
{
return Convert.ToByte(GetDoubleValue());
}
char IConvertible.ToChar(IFormatProvider provider)
{
return Convert.ToChar(GetDoubleValue());
}
DateTime IConvertible.ToDateTime(IFormatProvider provider)
{
return Convert.ToDateTime(GetDoubleValue());
}
decimal IConvertible.ToDecimal(IFormatProvider provider)
{
return Convert.ToDecimal(GetDoubleValue());
}
double IConvertible.ToDouble(IFormatProvider provider)
{
return GetDoubleValue();
}
short IConvertible.ToInt16(IFormatProvider provider)
{
return Convert.ToInt16(GetDoubleValue());
}
int IConvertible.ToInt32(IFormatProvider provider)
{
return Convert.ToInt32(GetDoubleValue());
}
long IConvertible.ToInt64(IFormatProvider provider)
{
return Convert.ToInt64(GetDoubleValue());
}
sbyte IConvertible.ToSByte(IFormatProvider provider)
{
return Convert.ToSByte(GetDoubleValue());
}
float IConvertible.ToSingle(IFormatProvider provider)
{
return Convert.ToSingle(GetDoubleValue());
}
string IConvertible.ToString(IFormatProvider provider)
{
return "( " + x.ToString() + " , " + y.ToString() + " )";
}
object IConvertible.ToType(Type conversionType, IFormatProvider provider)
{
return Convert.ChangeType(GetDoubleValue(),conversionType);
}
ushort IConvertible.ToUInt16(IFormatProvider provider)
{
return Convert.ToUInt16(GetDoubleValue());
}
uint IConvertible.ToUInt32(IFormatProvider provider)
{
return Convert.ToUInt32(GetDoubleValue());
}
ulong IConvertible.ToUInt64(IFormatProvider provider)
{
return Convert.ToUInt64(GetDoubleValue());
}
}
IFormattable interface
L'interfaccia IFormattable fornisce la funzionalità per formattare il valore di un oggetto in una rappresentazione di tipo stringa.
[ComVisible(true)]
public interface IFormattable
{
// Methods
string ToString(string format, IFormatProvider formatProvider);
}
Di seguito riporto un esempio di utilizzo tratto dalla documentazione msdn:
using System;
class Point : IFormattable
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public override String ToString() { return ToString(null, null); }
// Implementazione del metodo ToString definito nell'interfacci IFormattable
public String ToString(String format, IFormatProvider fp)
{
// If no format is passed, display like this: (x, y).
if (format == null) return String.Format("({0}, {1})", x, y);
// For "x" formatting, return just the x value as a string
if (format == "x") return x.ToString();
// For "y" formatting, return just the y value as a string
if (format == "y") return y.ToString();
// For any unrecognized format, throw an exception.
throw new FormatException(String.Format("Invalid format string: '{0}'.", format));
}
}
public sealed class App
{
static void Main()
{
// Create the object.
Point p = new Point(5, 98);
// Test ToString with no formatting.
Console.WriteLine("This is my point: " + p.ToString());
// Use custom formatting style "x"
Console.WriteLine("The point's x value is {0:x}", p);
// Use custom formatting style "y"
Console.WriteLine("The point's y value is {0:y}", p);
try
{
// Use an invalid format; FormatException should be thrown here.
Console.WriteLine("Invalid way to format a point: {0:XYZ}", p);
}
catch (FormatException e)
{
Console.WriteLine("The last line could not be displayed: {0}", e.Message);
}
}
}