VB.NET Is there a way to create a pre-processor constant that behaves as a simple text substitution?

与世无争的帅哥 提交于 2019-12-11 02:18:37

问题


VB.NET 2010, .NET 4

Hello,

I would like to do (something like) the following:

\#Const T = "Byte()"

Public Class MyClass
  Inherits SomeGenericClass(Of T)

  .. other code ..
End Class

And have it act the same as if I'd typed

Public Class MyClass
  Inherits SomeGenericClass(Of Byte())

  .. other code ..
End Class

It's not so much that i have to do it this way, I'm just curious if such a thing is possible.

Thanks in advance!

Brian


回答1:


No, this is not possible. The Visual Basic and C# designers decided not to allow a C-like preprocessor, because they felt it led to a lot of errors and confusion. It is very easy to write C macros that behave in unintended ways, and the VB and C# designers felt that safety for a broad range of developers took priority. Therefore defines in VB and C# are only 'defined' or 'undefined' rather than having values.

Eric Gunnerson discusses this from a C# perspective here, and I think the design reasoning for VB.NET was essentially the same.




回答2:


You only have: #Const #If #Else #Endif But, you can use another program to generate the code.



来源:https://stackoverflow.com/questions/4952413/vb-net-is-there-a-way-to-create-a-pre-processor-constant-that-behaves-as-a-simpl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!