从 net471 到 .NET Standard noob 问题 [英] from net471 to .NET Standard noob questions

查看:19
本文介绍了从 net471 到 .NET Standard noob 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常旧的 VB 项目,我希望对其进行现代化改造,我创建了一个新解决方案(稍后将代码转换为 C#),正在将库和 Web 项目重新添加到新解决方案中以消除旧项目拥有旧的 .publishproj、对 mscorlib 2.0 的引用(尽管尽最大努力通过重新添加引用来解决)以及其他几个可能会消失的问题.

在此过程中,我想尝试使用 .NET Standard 以获得标准化的 PCL,以便将来与 Xamarin、Mono 等一起使用.我并不完全精通 .NET Standard,因此需要一些输入(尝试 2.0基于从我读到的 2.0 的缩放效果)

我遇到的问题是:

1) 我有几个来自 .NET Framework 的基本 CORE 函数在 .NET Standard 中无法识别:

IsNumeric、IsNothing、IsDBNull、IIF

有什么关于为什么会这样的建议吗?

(重新编辑以删除保留)感谢 jmcilhinney 的回答:-)

解决方案

IsNumericIsNothingIsDBNull 四个IIf 是特定于 VB 的.如果在不引用 Microsoft.VisualBasic 程序集的情况下其他语言无法访问它们,它们就不能成为 .NET Standard 的一部分.无论如何,您之前真的不应该使用它们中的任何一个,因为它们是 VB6 的遗留物.

IsNumeric 的情况下,它无论如何在内部使用 Double.TryParse.事实上,Double.TryParse 是专门为提高IsNumeric 的性能而编写的.如果您想知道 String 是否可以转换为该类型,您应该自己使用适当数字类型的 TryParse 方法.

IsNothing 的情况下,您应该简单地将您的引用与 Nothing 进行比较,例如而不是:

If IsNothing(myThing) Then

你应该使用:

如果 myThing Is Nothing 那么

IsDBNull 的情况下,你应该像上面那样做,例如而不是:

如果 IsDBNull(myThing) 那么

你应该使用:

如果 myThing 是 DBNull.Value 那么

也就是说,DataRow 和数据读取器都有自己的专用方法来告诉您它们的字段之一是否为 NULL.

IIf 的情况下,它总是有问题,因为在许多情况下人们试图把它当作一个操作符来对待.我认为实际上是 VB 2008 引入了一个 If 运算符,它的工作方式与 C# 三元运算符非常相似,所以从那时起你应该一直使用它,例如而不是:

myVar = IIf(someBooleanExpression, someValue, someOtherValue)

你应该一直在使用:

myVar = If(someBooleanExpression, someValue, someOtherValue)

IIfIf 之间有一些细微的区别,但我会让你自己阅读 If 是如何工作的.>

I have a very old VB project I am looking to modernize to which I created a new solution (will convert code to C# later on), am re-adding the libraries and web projects to the new solution to eliminate the old project having old .publishproj, references to mscorlib 2.0 (despite best efforts to resolve through re-adding references) and several other issues that will likely go away.

In the process, I figured to try to go to .NET Standard for the standardized PCL that will allow for future use with Xamarin, Mono, etc. I am not fully versed in .NET Standard so need some input (attempting 2.0 based on the scaling effect of 2.0 down from what I read)

The problems I am running into are:

1) I have several basic CORE functions from the .NET Framework that are not recognized in .NET Standard:

IsNumeric, IsNothing, IsDBNull, IIF

Any suggestions as to why this is?

(re-edit to remove Hold) Thank you to jmcilhinney for answering :-)

解决方案

All four of IsNumeric, IsNothing, IsDBNull and IIf are VB-specific. They can't be part of .NET Standard if they've never been accessible to other languages without referencing the Microsoft.VisualBasic assembly. You really shouldn't have been using any of them previously anyway as they are holdovers from VB6.

In the case of IsNumeric, it uses Double.TryParse internally anyway. In fact, Double.TryParse was written specifically to improve the performance of IsNumeric. You should be using the TryParse method of the appropriate numeric type yourself if you want to know whether a String can be converted to that type.

In the case of IsNothing, you should simply be comparing your reference to Nothing, e.g. instead of:

If IsNothing(myThing) Then

you should be using:

If myThing Is Nothing then

In the case of IsDBNull, you should be doing much as above, e.g. instead of:

If IsDBNull(myThing) Then 

you should be using:

If myThing Is DBNull.Value Then

That said, both a DataRow and a data reader have their own dedicated methods to tell you whether one of their fields is NULL.

In the case of IIf, it always had it's issues because it is a method that people tried to treat like an operator in many cases. I think it was VB 2008 that actually did introduce an If operator that works much like the C# ternary operator, so you should have been using that since then anyway, e.g. instead of:

myVar = IIf(someBooleanExpression, someValue, someOtherValue)

you should have been using:

myVar = If(someBooleanExpression, someValue, someOtherValue)

There are some subtle differences between IIf and If but I'll leave you to read about how If works for yourself.

这篇关于从 net471 到 .NET Standard noob 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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