1、在Person类中,重写系统定义的ToString()方法:
public class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
public Person()
{
}
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
public override string ToString()
{
string rel = string.Format("姓名:{0} 年龄:{1}", this.name, this.age);
return rel;
}
}
这是当我再调用p.ToString()方法时,就会输出:姓名:张三 年龄:10
2、自定义Person类与String类之间的转换,这时候的Person类应为:
public class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
public Person()
{
}
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
public Person(string name)
{
this.name = name;
}
/*
* 以下方法定义了Person类与String类之间的转换规则
*/
///
/// 定义Person转换
///
///
///
public static explicit operator Person(string name)
{
Person p=new Person(name);
return p;
}
public static explicit operator string(Person p)
{
string rel = string.Format("姓名:{0} 年龄:{1}",p.name,p.age);
return rel;
}
}
这时当我再程序中进行Person类与String类之间的转换时,则可得到想要的结果(如果没有重新定义而这之间的转换关系,程序编译时会报错),自如的完成Person类与String类之间的转换
Person p=new Person("李四",11);
string mes =(string)p;
MessageBox.Show(mes);
string name = "王五";
Person p1 =(Person)name;
MessageBox.Show(p1.Name);
3、自定义类,该类继承自IConvertible类,依次根据要求实现IConvertible类中的方法,依次实现IConvertible的各个方法(这里只实现了ToInt32()与ToString()方法),这时Person类为:
public class Person:IConvertible
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
public Person()
{
}
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
public Person(string name)
{
this.name = name;
}
public TypeCode GetTypeCode()
{
{
throw new InvalidCastException("此种方法不支持ToBoolean");
}
}
public bool ToBoolean(IFormatProvider provider)
{
throw new InvalidCastException("此种方法不支持ToBoolean");
}
public byte ToByte(IFormatProvider provider)
{
throw new InvalidCastException("此种方法不支持ToBoolean");
}
public char ToChar(IFormatProvider provider)
{
throw new InvalidCastException("此种方法不支持ToBoolean");
}
public DateTime ToDateTime(IFormatProvider provider)
{
throw new InvalidCastException("此种方法不支持ToBoolean");
}
public decimal ToDecimal(IFormatProvider provider)
{
throw new InvalidCastException("此种方法不支持ToBoolean");
}
public double ToDouble(IFormatProvider provider)
{
throw new InvalidCastException("此种方法不支持ToBoolean");
}
public short ToInt16(IFormatProvider provider)
{
throw new InvalidCastException("此种方法不支持ToBoolean");
}
public int ToInt32(IFormatProvider provider)
{
return age;
}
public long ToInt64(IFormatProvider provider)
{
throw new InvalidCastException("此种方法不支持ToBoolean");
}
public sbyte ToSByte(IFormatProvider provider)
{
throw new InvalidCastException("此种方法不支持ToBoolean");
}
public float ToSingle(IFormatProvider provider)
{
throw new InvalidCastException("此种方法不支持ToBoolean");
}
public string ToString(IFormatProvider provider)
{
string rel = string.Format("姓名:{0} 年龄:{1}", name, age);
return rel;
}
public object ToType(Type conversionType, IFormatProvider provider)
{
throw new InvalidCastException("此种方法不支持ToBoolean");
}
public ushort ToUInt16(IFormatProvider provider)
{
throw new InvalidCastException("此种方法不支持ToBoolean");
}
public uint ToUInt32(IFormatProvider provider)
{
throw new InvalidCastException("此种方法不支持ToBoolean");
}
public ulong ToUInt64(IFormatProvider provider)
{
throw new InvalidCastException("此种方法不支持ToBoolean");
}
}
这时我再调用 Convert.ToString()与 Convert.ToInt32()方法时,就会得到想要的结果:
Person p = new Person("李六", 22);
string mes = Convert.ToString(p);
int age = Convert.ToInt32(p);
MessageBox.Show(mes);
MessageBox.Show(age.ToString());
以上三种方法就是我了解到的自定义类型与C#基础类型之间的转换方法,在第二种方法中,用到了explicit与operator,这两个关键字的作用可以查看MSDN
http://msdn.microsoft.com/zh-cn/library/z5z9kes2.aspx
http://msdn.microsoft.com/zh-cn/library/s53ehcz3.aspx