重写 List的 ToString()
我有一个 MyClass 类,我想重写 List 实例的 ToString() 方法:
class MyClass
{
public string Property1 { get; set; }
public int Property2 { get; set; }
/* ... */
public override string ToString()
{
return Property1.ToString() + "-" + Property2.ToString();
}
}
我想要以下内容:
var list = new List<MyClass>
{
new MyClass { Property1 = "A", Property2 = 1 },
new MyClass { Property1 = "Z", Property2 = 2 },
};
Console.WriteLine(list.ToString()); /* prints: A-1,Z-2 */
可以这样做吗?或者我必须将 List
谢谢!
I have a class MyClass, and I would like to override the method ToString() of instances of List:
class MyClass
{
public string Property1 { get; set; }
public int Property2 { get; set; }
/* ... */
public override string ToString()
{
return Property1.ToString() + "-" + Property2.ToString();
}
}
I would like to have the following:
var list = new List<MyClass>
{
new MyClass { Property1 = "A", Property2 = 1 },
new MyClass { Property1 = "Z", Property2 = 2 },
};
Console.WriteLine(list.ToString()); /* prints: A-1,Z-2 */
Is it possible to do so? Or I would have to subclass List<MyClass> to override the method ToString() in my subclass? Can I solve this problem using extension methods (ie, is it possible to override a method with an extension method)?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
根据您想要覆盖
List.ToString()
以返回特定内容的确切原因,查看自定义TypeConverter
实现。如果您只是希望特定
T
的List
在使用 TypeConverters 的位置以某种方式显示为string
,例如在调试器或string.Format("List: {0}", listVariable)
类型的情况下,这可能就足够了。您可能只是在某处看到了 ToString() 的结果,并想要更改它,而不知道
TypeConverter
的存在及其使用位置。我相信 .NET Framework 中的许多/大多数/全部(不确定是哪一个?)默认 TypeConverters 在将其定义的任何类型转换为string
时仅使用 ToString()。Depending on the exact reason you have for wanting to override
List<T>.ToString()
to return something specific it might be handy to have a look at customTypeConverter
implementations.If you simply want a
List<T>
of specificT
to show itself a certain way as astring
in locations where TypeConverters are used, like in the debugger or instring.Format("List: {0}", listVariable)
type situations, this might be enough.You might just have seen the result of ToString() being shown somewhere and wanted to change that, without knowing about the existence of
TypeConverter
and locations where they are used. I believe many/most/all (not sure which?) of the default TypeConverters in the .NET Framework simply use ToString() when converting any type for which they are defined for to astring
.也许有点偏离主题,但我使用了适用于任何
IEnumerable
的ToDelimitedString
扩展方法。您可以(可选)指定要使用的分隔符和委托来为每个元素执行自定义字符串转换:Perhaps a bit off-topic, but I use a
ToDelimitedString
extension method which works for anyIEnumerable<T>
. You can (optionally) specify the delimiter to use and a delegate to perform a custom string conversion for each element:您需要子类化才能覆盖任何方法。泛型的要点是,无论 T 的类型如何,您都想要相同的行为。如果您希望特定类型的 T 具有不同的行为,那么您就违反了该约定,并且需要编写自己的类:
编辑添加:
不,您不能通过创建扩展来覆盖方法,但您可以创建一个具有特定于此列表类型的不同签名的新方法:
与 一起使用
我仍然认为您最好不要进行子类化...
You'll need to subclass to override any method. The point of generics is to say that you want the same behaviour regardless of the type of T. If you want different behaviour for a specific type of T then you are breaking that contract and will need to write your own class:
Edited to add:
No, you can't override a method by creating an extension, but you could create a new method with a different signature that is specific to this list type:
Used with
I still think you're better off subclassing though...
如果您的方法必须命名为
ToString
,则必须从List
派生一个类。您可以将其设为通用:在这种情况下,如果您希望进行自定义转换,则必须在整个应用程序中使用
MyList
而不是List
。但是,如果您可以为方法选择不同的名称,则可以使用扩展方法并实现相同的效果,而几乎无需修改代码:
您可以使用扩展方法使其更通用:
您可以在任何实例上使用它
IEnumerable
就像普通方法一样:If you method must be named
ToString
you will have to derive a class fromList
. You can make it a generic:In this case, you would have to use
MyList
instead ofList
throughout your application if you wish to have your custom conversion.However, if you can choose a different name for your method, you can use extension methods and achieve the same effect, with almost no modifications to your code:
You can use extension methods to make this more generic:
You can use it on any instance of
IEnumerable<T>
just as if it were an ordinary method:实际上,您可以使用 unicode 技巧来直接针对通用列表定义备用 ToString 方法。
如果您在 Visual Studio 中启用十六进制字符输入,那么您可以通过按住 Alt 键,然后按数字键盘上的以下内容 + FFF 9 (现在释放 Alt)来创建不可见字符,
因此我们可以创建以下带有不可见字符的函数它的名称旁边...(是的,我知道它的 VB 代码,但这个概念仍然适用于 C#)
现在在 Visual Studio 中,当您根据列表访问智能感知时,您将能够在标准 ToString 或您的自定义功能。
要在 Visual Studio 中启用十六进制字符输入,您可能需要编辑注册表,
打开 HKEY_CURRENT_USER\Control Panel\Input Method
并创建一个名为 EnableHexNumpad 的 REG_SZ 将其设置为 1
您还需要禁用 &文件、编辑、调试、数据菜单的快捷方式,
在 Visual Studio 中,打开工具菜单,选择自定义,然后打开命令选项卡,并通过删除 & 来使用任何使用 ABCDEF 字符作为快捷方式的任何菜单项的修改选择按钮。
否则,您最终将打开弹出菜单,而不是键入十六进制字符。
You can actually use a unicode trick to allow you to define an alternate ToString method directly against your generic list.
If you enable hex character input into visual studio then you can create invisible characters by holding down the Alt key, then pressing the following on your numeric keypad + F F F 9 (now release Alt)
So we can create the following function with an invisible character placed next to its name... (yes i know its VB code, but the concept will still work work for C#)
Now in visual studio, when you access intellisense against your list, you will be able to choose between either the standard ToString or your custom function.
To enable hex character input into visual studio you may need to edit your registry
open HKEY_CURRENT_USER\Control Panel\Input Method
and create a REG_SZ called EnableHexNumpad set this to 1
You will also need to disable the & shortcuts for the File, Edit, Debug, Data menus,
In visual studio, open the tools menu, select customize, then open the commands tab, and using the modify selection button for any menu item that uses either of the ABCDEF charactes for its short cut, by removing the &
Otherwise you will end up opening popup menus, instead of typing hex characters.
您必须创建自己的继承自 Collection 的自定义类,然后专门覆盖该类的 ToString() 方法。
You would have to create your own custom class that inherits from Collection and then overwride the ToString() method of that class specifically.
不,这是不可能的。 TList 的 ToString 将为您提供列表对象的字符串表示形式。
您的选择是:
希望有帮助!
No its not possible. ToString of TList will give you the string representation of the list object.
Your options are:
Hope that helps!