Managing resources via compilation flags
Any idea how i can do that ?
Now days it is done in Resources.Designer.vb
we have there following lines:
Friend ReadOnly Property ResourceManager() As Global.S开发者_StackOverflow中文版ystem.Resources.ResourceManager
Get
If Object.ReferenceEquals(resourceMan, Nothing) Then
#If WizardVersion Then
Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("Wizard.Resources", GetType(Resources).Assembly)
#ElseIf CalculatorVersion Then
Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("Calculator.Resources", GetType(Resources).Assembly)
#ElseIf ViewerVersion Then
Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("Viewer.Resources", GetType(Resources).Assembly)
#End If
resourceMan = temp
End If
Return resourceMan
End Get
End Property
That file is regenerated each time via the compiler and so each time i have to add there those lines by hand.
Can you suggest some another way to do so ?
Thanks.
You could use reflection to overwrite the ResourceManager created by the Resources class :
Sub InitResources()
#If WizardVersion Then
Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("Wizard.Resources", GetType(Resources).Assembly)
#ElseIf CalculatorVersion Then
Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("Calculator.Resources", GetType(Resources).Assembly)
#ElseIf ViewerVersion Then
Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("Viewer.Resources", GetType(Resources).Assembly)
#End If
Dim resManField As System.Reflection.FieldInfo = GetType(My.Resources.Resources).GetField("resourceMan", Reflection.BindingFlags.Static Or Reflection.BindingFlags.NonPublic)
resManField.SetValue(Nothing, temp)
End Sub
That's not very elegant, but it should work...
You could run a batch file that copies (and overrides the old file) the files into the proper place on a pre-build action. You could take a flag in as a parameter of the batch.
精彩评论