当指定的字符串或值等于某物时抛出错误(使用 IntelliSense)

Throw error (with IntelliSense) when a specified string or value is equal to something

当指定变量等于某个值时,是否可以在 Visual Studio 中抛出/显示错误,显示为设计时错误(有或没有 IntelliSense)?

例如,我有一个 Class 库和一个 win 窗体。如果我导入 class 库并声明 Class 库代码并写入特定的文件路径,我想在 visual studio.

中抛出错误

赢得表格代码:

Public Class Form1

    Private Sub RunTest
        ' I want to throw an error here if the string
        ' is equal to = "C:\Users\Downloads\Test.jpg"
        Dim Testing As New Test("C:\Users\Downloads\Test.jpg")
    End Sub

Class 图书馆代码:

Public Class Test

    Private Image As Bitmap

    ' If possible here is where I believe I need to throw the error.
    Public Sub New(ByVal FileName As String) 
        Image = DirectCast(Bitmap.FromFile(FileName), Bitmap)
    End Sub

End Class

这是我一直在尝试的:

    ' I wish it was possible to do in the way of:
    Public Sub New(ByVal FileName As String) 
        If FileName = "C:\Users\Downloads\Test.jpg" Then Throw New ArgumentException("Invalid")
        Image = DirectCast(Bitmap.FromFile(FileName), Bitmap)
    End Sub

我们将不胜感激任何意见或帮助!

Throw New ArgumentException 将在运行时生效,但不会在设计时生效。智能感知不适用于这种情况。

不确定您的原始要求,如果您想做的只是在设计时获得 Intellisense 可以识别的警告,您可能会从 ObsoleteAttribute 获得一些帮助。

您可以将此属性设置为测试 Class 或其中的方法。如果您的 winform 应用程序从该方法调用代码,intellisense 将显示如下警告以提醒您: