从VB使用StatePrinter而不是C#来实现ToString [英] Using StatePrinter from VB rather than C# to implement ToString

查看:89
本文介绍了从VB使用StatePrinter而不是C#来实现ToString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循有前途的此处发布的建议,尝试使用StatePrinter作为滚动我自己的ToString方法的快捷方式.我同意OP ,对于VS仍然无法为我生成这种方法感到遗憾.

I'm trying to follow the promising suggestion posted here to try StatePrinter as a shortcut to rolling my own ToString methods. I agree with the OP that it is a shame that VS still can't generate this method for me.

我在VS2015(社区版)中有一个相当大的项目,同时包含VB和C#代码.我使用NuGet添加了当前稳定的StatePrinter版本.

I've got a fairly large project, in VS2015 (Community Edition), with both VB and C# code. I added the current stable version of StatePrinter using NuGet.

我可以使SO回答中的示例代码在我的C#代码中工作正常,但是当我做我认为与我的VB代码等效的事情时:

I can make the example code from the SO answer work fine in my C# code but when I do what I think is the equivalent in my VB code:

Private Shared sp As StatePrinter.Stateprinter = New StatePrinter.Stateprinter
    Public Overrides Function ToString() As String
        Return sp.PrintObject(Me)
    End Function

我刚得到编译器错误

'Stateprinter' is ambiguous in the namespace 'StatePrinter' 

还有另一个构造函数StatePrinter(仅注意大小写不同),该构造函数已过时,并且在任何情况下均会生成相同的错误消息.

There IS another constructor, StatePrinter (note difference in capitalization only) which is deprecated and, in any case, generates the same error message.

我得出了不幸的结论

    VS2015中的
  1. VB似乎不区分大小写.可以吗?
  2. 没有人正在使用VB中的StatePrinter.
  1. VB in VS2015 is acting as if it is case insensitive. Can that be true?
  2. No one else is using StatePrinter from VB.

任何人都可以提供有关如何使用VB中的StatePrinter的任何建议吗?我愿意相信我在将C#示例转换为VB时犯了一些相当致命的错误.

Can anyone provide any suggestions on how to use StatePrinter from VB? I'm willing to believe I'm making some rather brain-dead mistake in converting the C# example to VB.

推荐答案

几乎不可能直接在VB中使用它并解决模棱两可的名称问题.您可以使用C#编写一个类库包装程序,该包装程序不会暴露这种不匹配(也就是说,它具有一个内部StatePrinter对象,并公开与PascalCased相同的构造函数.

It is near impossible to use this directly in VB and get around the ambiguous name issue. You could write a class library wrapper in C# that doesn't expose this mismatch (that is, it has an internal StatePrinter object and exposes constructors that are PascalCased the same.

另一种选择是在VB项目中使用反射来避免不区分大小写.

Another option would be to use reflection in the VB project to get around the case insensitivity.

您还可以创建GitHub问题.或者,为该项目做出贡献并为其创建建议的修复程序. :)

You could also create a GitHub issue. Or, be a contributor to the project and create a suggested fix for it. :)

这篇关于从VB使用StatePrinter而不是C#来实现ToString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆