Automatically add namespace based on path to embedded VB.NET resources

好久不见. 提交于 2019-12-21 12:17:18

问题


I am used to embedding resources in C# and I like the way it automatically adds namespaces to the embedded resources. That allows me to do things like this:

files\version1\config.xml
files\version2\config.xml
files\version2\config.xml

Unfortunately, if you try the same in a VB.NET project you'll get compile errors, since it tries to put all embedded resources into the root namespace. To get around this I can manually edit the .vbproj file like so:

<EmbeddedResource Include="files\version1\config.xml">
  <LogicalName>$(RootNamespace).files.version1.config.xml</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="files\version2\config.xml">
  <LogicalName>$(RootNamespace).files.version2.config.xml</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="files\version3\config.xml">
  <LogicalName>$(RootNamespace).files.version3.config.xml</LogicalName>
</EmbeddedResource>

While this works, it's manual, time consuming and error prone so my question is this: Can a build task or build event be written do this automatically?


回答1:


This is a side effect of how Visual Basic does not use folder paths to create namespaces by default.

Personally, other then the specific case, your talking about I prefer it not having all the additional folder paths in the name. I hope MS adds another property to the file resources to allow a namespace to be set specifically in the future, but until then....

The solution is quite simple though.

Create a resource-only dll in C# and read your resources from there. As a VB developer I would not think twice about doing this to satisfy that specific purpose.

EDIT. OR... can use a vbs file as a prebuild event to copy files to a new resource directory in the format needed to make the simulated namespace.

dim fSys
set fsys=createobject("Scripting.FileSystemObject")
dim root : root= "c:\temp"
dim out : out="c:\temp\DynResource"

dim rFo: set rFo=fsys.getfolder(root)
dim outPath

for each sf in rFo.SubFolders
    if instr(1, sf.name, "Version")>=1 then 'valid resource folder
        for each f in sf.Files
            outpath = out & "\" & sf.name & "." & f.name
            if fsys.FileExists(output) then
                dim tf:set tf=fsys.getfile(output)
                if tf.length<>f.length or tf.DateLastModified<>f.DateLastModified then
                    f.copy outPath,true
                else
                    'same file, no update required.
                end if 
            else
                f.copy outPath,true
            end if
        next
    end if 
next 

Output foldere must already exist (and obviously cannot have "Version" in the folder name).



来源:https://stackoverflow.com/questions/12860969/automatically-add-namespace-based-on-path-to-embedded-vb-net-resources

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