重载类库中的VB.NET扩展方法
看来我用 VB.NET 编写的扩展方法库有问题。
我有 2 个重载的扩展方法 Crop()。
当我从 VB.NET 项目引用该库时,我看到了它们。如果从 C# 项目引用它,我看不到它们。
到底是怎么回事?
It seems that my library of extension methods that was written in VB.NET has problems.
I have 2 overloaded extension methods Crop().
When i reference the lib from a VB.NET project i see them. If reference it from a C# project i can't see them.
What the hell is going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它的工作原理如下:
out
或ref
参数的方法时,编译器要求您重复out
或调用参数上的ref
关键字。这是因为变量是否已经初始化对于语言来说确实很重要。ByRef
参数的方法时,编译器不要求您在调用参数上重复ByRef
,它只是为您整理出来。这是因为变量是否已经初始化对于语言来说并不重要。因此,VB.NET 可以使用
ByRef
轻松解析扩展方法,因为只有一种可能的调用方式。但是,C# 不知道要做什么,因为没有语法告诉它是使用out
还是ref
调用该方法。您所能做的就是:
ByVal
参数。It works like this:
out
orref
parameter, the compiler requires you to repeat theout
orref
keyword on the calling argument. This is because it does matter to the language whether the variable is already initialised or not.ByRef
parameter, the compiler does not require you to repeatByRef
on the calling argument, it just sorts it out for you. This is because it does not matter to the language whether the variable is already initialised or not.Therefore, VB.NET can easily resolve an extension method using
ByRef
, because there is only one possible way in which it can be called. However, C# does not know what to do because there is no syntax for telling it whether to call the method usingout
orref
.All you can do is:
ByVal
parameter wherever possible.如果没有看到代码,我的猜测是您在 C# .cs 文件中缺少
using
语句。但这只是在没有看到您的代码的情况下的猜测。
希望这有帮助!
Without seeing the code, my guess is that you're missing a
using
statement in your C# .cs file.But this is just a guess without seeing your code.
Hope this helps!!
我的方法使用 byref 参数。我将其更改为 byval 并且有效。
显然这很奇怪。在 VB 项目中可以,但在 C# 中不行。显然 C# 不支持使用 byref 的扩展方法,或者这是一个错误。
My methods were using byref arguments. I changed it to byval and it worked.
It's very weird apparently. In VB projects its ok but in C# not. Apparently C# does not support extension methods with byref or it is a bug.