使用此示例代码:
public class Product
{
private Boolean _active;
public Boolean active
{
get
{
return _active;
}
set
{
_active = value;
}
}
}
Product p = new Product();
p.active = true;
如何重写
ToString()
方法以便我可以使用这个:
MessageBox.Show(p.active.ToString());
对于它的价值,您可以使用扩展方法:
public static string toString(this bool b)
{
return b ? "Si" : "No";
}
MessageBox.Show(p.active.toString());
这是因为你的错字,
toString
而不是ToString
。
Boolean.ToString
返回 "True"
或 "False"
,没有什么可以改变这一点(除了 Microsoft)。
但是当然你也可以写:
MessageBox.Show(p.active ? "Si" : "No");
我不确定您是否可以按照自己的意愿覆盖它。也许你可以只使用三元运算符来进行小的评估,例如
MessageBox.Show(p.active ? 'Yes' : 'No');
我需要一种灵活的方式来编写布尔值,因此我创建了自己的“Bool”类型,默认情况下将 True 写为“1”,将 False 写为“0”:
public readonly struct Bool
{
private readonly bool Value;
public Bool(bool digit)
{
Value = digit;
}
public static implicit operator bool(Bool d) => d.Value;
public static implicit operator Bool(bool b) => new Bool(b);
public override string ToString() => Value ? BoolWriter.TrueValue : BoolWriter.FalseValue;
public static class BoolWriter
{
public static string TrueValue = "1";
public static string FalseValue = "0";
}
}
隐式类型转换使其可以与本机 bool 类型互换:
// instatiation and assignment
var b1 = new Bool(true);
var b2 = (Bool)false;
Bool b3 = true;
b3 = b1;
bool b4 = false;
b1 = b4;
Console.WriteLine("b1: {0}, b2: {1}, b3: {2}, b4: {3}", b1, b2, b3, b4);
// b1: 0, b2: 0, b3: 1, b4: False
如果您需要更改 True 和 False 值的写入方式,请通过静态类 Bool.BoolWriter 来完成:
// globally change string representation of Bool value
Bool.BoolWriter.TrueValue = "Apple";
Bool.BoolWriter.FalseValue = "Orange";
Console.WriteLine("b1: {0}, b2: {1}, b3: {2}, b4: {3}", b1, b2, b3, b4);
// b1: Orange, b2: Orange, b3: Apple, b4: False
为什么要重写变量的 ToString 而不是对象的 ToString?你能解释一下你的场景吗?我建议您使用对象的 ToString 或使用属性来显示您的信息,而不是创建一个扩展方法,在您调用变量上的 ToString 时将所有“bool”视为“是”或“否”
class Program
{
static void Main(string[] args)
{
Product p = new Product();
Console.WriteLine(p);
Console.ReadLine();
}
}
public class Product
{
private Boolean _active;
public Boolean active
{
get
{
return _active;
}
set
{
_active = value;
}
}
public override string ToString()
{
return active ? "Yes" : "No";
}
}
如果您想使用属性,可以使用此处描述的方法创建扩展方法https://stackoverflow.com/a/672212/819153
class Program
{
static void Main(string[] args)
{
Product p = new Product();
var attValue = Helper.ToStringProperty(p, prod => prod.active);
Console.WriteLine(attValue);
Console.ReadLine();
}
}
public class Product
{
private Boolean _active;
[Display(Name = "Super Active")]
public Boolean active
{
get
{
return _active;
}
set
{
_active = value;
}
}
}
public class Helper
{
public static string ToStringProperty<TSource, TProperty>(TSource source,
Expression<Func<TSource, TProperty>> propertyLambda)
{
Type type = typeof(TSource);
MemberExpression member = propertyLambda.Body as MemberExpression;
if (member == null)
throw new ArgumentException(string.Format(
"Expression '{0}' refers to a method, not a property.",
propertyLambda.ToString()));
PropertyInfo propInfo = member.Member as PropertyInfo;
if (propInfo == null)
throw new ArgumentException(string.Format(
"Expression '{0}' refers to a field, not a property.",
propertyLambda.ToString()));
if (type != propInfo.ReflectedType &&
!type.IsSubclassOf(propInfo.ReflectedType))
throw new ArgumentException(string.Format(
"Expresion '{0}' refers to a property that is not from type {1}.",
propertyLambda.ToString(),
type));
var output = source.ToString();
var attribute = propInfo.GetCustomAttributes(typeof(DisplayAttribute), false).Cast<DisplayAttribute>().FirstOrDefault();
return attribute != null ? attribute.Name : output;
}
}