这是我脑海中出现的一个问题:是否可以根据用户在构造函数中传递的参数仅显示属性?
为了使这个问题更清楚,我'将说明一个具有一些属性的简单类。
Public Class SampleClass
Dim _ForA as string
Dim _ForB as string
Public ReadOnly Property PropertyA as String
Get
return _ForA
End Get
End Property
Public ReadOnly Property PropertyB as String
Get
return _ForB
End Get
End Property
Public Sub New(SelectProp as string)
End Sub
End Class
如果用户在构造函数中传递 A
(字符串),则仅显示 PropertyA
,如果传递 B
,则显示 PropertyB
只会显示。在 .Net
中可能吗?
这是另一件事。我在互联网上的一些代码中找到了这个声明。您能解释一下声明中发生了什么吗?
Imports System.Data
#If DBType = "OLEDB" THEN
Imports System.Data.OleDB
#End IF
#If DBType = "SQLClient" THEN
Imports System.Data.SqlClient
#End IF
谢谢。
最佳答案
名称是条件编译 您可以使用条件编译来选择要编译的特定代码部分,同时排除其他部分。例如,您可能想要编写调试语句来比较同一编程任务的不同方法的速度,或者您可能想要针对多种语言本地化应用程序。条件编译语句设计为在编译时运行,而不是在运行时运行。
您可以使用 #Const 指令在代码中声明条件编译器常量,并使用 #If...Then...#Else 指令表示要进行条件编译的代码块。例如,要从同一源代码创建同一应用程序的法语和德语版本,您可以使用预定义常量 FrenchVersion 和 GermanVersion 将特定于平台的代码段嵌入到 #If...Then 语句中。下面的例子演示了如何实现
#If FrenchVersion Then
' <code specific to the French language version>.
#ElseIf GermanVersion Then
' <code specific to the German language version>.
#Else
' <code specific to other versions>.
#End If
阅读此内容 Conditional Compilation
关于c# - 仅显示基于配置的指定属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9527353/