简体   繁体   English

C#使自动ToString转换无效

[英]C# Invalidate automatic ToString conversion

In C#, I have a class: 在C#中,我有一个类:

public class Person
{
    public string name;
    public int id;
}

Currently, when I do: 目前,当我这样做时:

Person person = new Person
{
    name = "John",
    id = 3
}
// Will be converted Console.Writeline("Person = " + person.ToString()); by the compiler
Console.Writeline("Person = " + person); 

What can I do in the class to invalidate the automatic conversion from person to person.ToString() and make the compiler give an error to indicate Person object cannot be converted to string implicitly? 我可以在该类中做什么以使从person到person.ToString()的自动转换无效并使编译器给出错误消息,以指示不能将Person对象隐式转换为字符串?

What can I do in the class to invalidate the automatic conversion from person to person.ToString() and make the compiler give an error to indicate Person object cannot be converted to string implicitly? 我可以在该类中做什么以使从person到person.ToString()的自动转换无效并使编译器给出错误消息,以指示不能将Person对象隐式转换为字符串?

You can't, at least not in an easy way. 您不能,至少不能以简单的方式。 The overload of string + object will be the one emitting the ToString on object , which you have no way of controlling. string + object的重载将是在object上发出ToString的重载,您无法对其进行控制。 This is specified in Addition Operator (7.7.4) part of the specification (emphasis mine): 这在规范的“ 加法运算符(7.7.4)”部分(强调我的)中指定:

The binary + operator performs string concatenation when one or both operands are of type string. 当一个或两个操作数的类型为string时,binary +运算符将执行字符串连接。 If an operand of string concatenation is null, an empty string is substituted. 如果字符串连接的操作数为null,则替换为空字符串。 Otherwise, any non-string argument is converted to its string representation by invoking the virtual ToString method inherited from type object. 否则,通过调用从类型对象继承的虚拟ToString方法,将任何非字符串参数转换为其字符串表示形式。 If ToString returns null, an empty string is substituted. 如果ToString返回null,则替换为空字符串。

What you can do, which isn't a compile-time warning, is throw an exception from an overloaded ToString . 您可以执行的操作(不是编译时警告)是从重载的ToString引发异常。 I would definitely recommend not doing that . 我绝对建议不要这样做 You could possibly write a Roslyn analyzer which looks for any object passed to Console.WriteLine without overriding the ToString method and emit a warning for that. 您可以编写一个Roslyn分析器来查找传递给Console.WriteLine任何对象,而不会覆盖ToString方法并为此发出警告。

You'd have to write your programming language that doesn't have an overload of the + operator accepting a string and an object as its two operands. 您必须编写不带有+运算符的重载的编程语言,该运算符接受一个字符串和一个对象作为其两个操作数。 As it is, if you're using C# which states that such an operator overload is required as a part of the language specs , the code that you've shown must compile in C#. 原样 ,如果您使用的C#声明这样的运算符重载是语言规范的一部分 ,那么您显示的代码必须在C#中编译。 In order to make that operation not compile would require using a language that doesn't meet the C# language specs. 为了使该操作无法编译,将需要使用不符合C#语言规范的语言。

Using the new keyword when overriding the ToString method and marking it as obsolete looks to do the trick. 在覆盖ToString方法并将其标记为过时时使用new关键字可以解决问题。

public class Person
{
    public string name;
    public int id;

    [Obsolete("this should not be used", true)]
    public new string ToString()
    {
        return null;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM