【发布时间】:2011-03-28 02:51:09
【问题描述】:
查看了很多关于使用 Iif() 的帖子/文章。所有这一切都始于我尝试像 C/C#/C++ 等类似条件运算符一样使用 IIf()。
我试图做的正是以下帖子中的内容:
Using VB.NET IIF I get NullReferenceException
但是,我实施的解决方案是从以下 msdn 站点借用的:
http://msdn.microsoft.com/en-us/library/27ydhh0d%28v=vs.80%29.aspx
后来才知道vb.net中的If()方法。
所以最后我写了一个函数,它返回适当的值(使用反射)来达到目的。但是那个方法(我写的)没有抛出任何异常。事实上,我能够检查函数内部类型的可空性。
Function ReturnValue(ByVal MyType As SomeType, ByVal PropertyName as String) As Object
If MyType Is Nothing Then Return String.Empty
Dim arrPropInfo As PropertyInfo() = MyType.GetType().GetProperties()
Return arrPropInfo.Where(Function(x) x.Name = PropertyName).Item(0).GetValue(MyType, Nothing)
End Function
我的问题是,在 Iif() 中是否写了一些东西来抛出这样的错误? - NullReferenceException
If MyType Is Nothing Then Throw New NullReferenceException()
或者这里有什么更大的工作?所以假设如果我想编写像 iif 这样的函数,如果参数列表中的某些内容为 null,它会抛出错误,这是上述方法的唯一方法吗?
【问题讨论】:
标签: vb.net nullreferenceexception iif-function