Access Compiler Constants in code
Is it possible to use a constant defined by the compiler in code like below?
#If DEALER_DEBUG = "ID12345" Then
If(Dealer.ID = DEALER_DEBUG) Then
'Do something
End If
#End If
I'm running batch processes and I'm ex开发者_如何学Pythonperiencing problems with one of my customer's data. I want to add special code for only that customer, but I want to keep the code there so I can easily switch the customer ID in the future should i need to debug a different customer.
The source-code of the compiled dll will then look like this:
If(Dealer.ID = "ID12345") Then
'Do something
End If
No. Compiler directives are just that, directives to the compiler. They are not included in the generated IL code, and so cannot be accessed at runtime.
You can use it in the compile time #If, but you can't use it in the runtime If
You can define your custom compiler constants in the project properties under Compile->Advanced Compile Options->Custom Constants, or alternately by using a #Const directive.
There are many better ways to do this. I don't know much about what exactly you are attempting to accomplish, but you might want to consider some sort of factory pattern + plugins + config that allows you to provide a plugin assembly to that client which can allow for extra functionality.
It is arguably a lot more work to create an extensible app, but if you find yourself needing to do these kinds of things, its much better to write it to be extensible from the start than have to go back and refactor later.
精彩评论