Roe*_*den 9 .net c# resources naming-conventions
我在获取嵌入式资源的流时遇到问题.大多数在线示例显示可以通过将路径的斜线更改为源的点(MyFolder/MyFile.ext变为MyNamespace.MyFolder.MyFile.ext)来直接转换的路径.但是,如果文件夹中的名称中包含点,并且使用了特殊字符,则手动获取资源名称不起作用.我正在尝试找到一个可以将路径转换为资源名称的函数,因为Visual Studio在编译时重命名它们.
这些名称来自解决方案......
......在资源中被改成这些名字......
斜线转换为点.但是,当在文件夹名称中使用点时,第一个点显然被视为扩展名,其余的点被更改为以下划线为前缀.但是,这个逻辑不适用于jQuery.js文件,可能是因为'extension'是一个数字?这是一个能够翻译我迄今为止所遇到的问题的函数,但是在jQuery.js路径上不起作用.
protected String _GetResourceName( String[] zSegments )
{
String zResource = String.Empty;
for ( int i = 0; i < zSegments.Length; i++ )
{
if ( i != ( zSegments.Length - 1 ))
{
int iPos = zSegments[i].IndexOf( '.' );
if ( iPos != -1 )
{
zSegments[i] = zSegments[i].Substring( 0, iPos + 1 )
+ zSegments[i].Substring( iPos + 1 ).Replace( ".", "._" );
}
}
zResource += zSegments[i].Replace( '/', '.' ).Replace( '-', '_' );
}
return String.Concat( _zAssemblyName, zResource );
}
Run Code Online (Sandbox Code Playgroud)
是否有可以为我更改名称的功能?它是什么?或者我在哪里可以找到所有规则,以便我可以编写自己的功能?感谢您提供的任何帮助.
这是一个很晚的答案...但是,由于这是google上的首个热门产品,因此我将发布我发现的内容!
您可以简单地强制编译器根据需要命名嵌入式资源。从一开始就可以解决这个问题...您只需要编辑csproj文件,通常就需要在其中添加通配符!这是我所做的:
<EmbeddedResource Include="$(SolutionDir)\somefolder\**">
<Link>somefolder\%(RecursiveDir)%(Filename)%(Extension)</Link>
<LogicalName>somefolder:\%(RecursiveDir)%(Filename)%(Extension)</LogicalName>
</EmbeddedResource>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我告诉Visual Studio,我希望将“某些文件夹”中的所有文件导入为嵌入式资源。我也希望它们显示在VS解决方案资源管理器中的“某些文件夹”下(这是链接标记)。最后,在编译它们时,我希望它们的名称和地址与磁盘上的名称和地址完全相同,并且仅以“ somefolder:\”为前缀。最后一部分是做魔术。
这就是我想出的解决问题的方法。我仍然对更好的方法持开放态度,因为这有点小技巧(但对于当前的规范似乎是准确的)。该函数需要处理来自 Uri 的段(处理 Web 请求时为 LocalPath)。示例调用如下..
protected String _GetResourceName( String[] zSegments )
{
// Initialize the resource string to return.
String zResource = String.Empty;
// Initialize the variables for the dot- and find position.
int iDotPos, iFindPos;
// Loop through the segments of the provided Uri.
for ( int i = 0; i < zSegments.Length; i++ )
{
// Find the first occurrence of the dot character.
iDotPos = zSegments[i].IndexOf( '.' );
// Check if this segment is a folder segment.
if ( i < zSegments.Length - 1 )
{
// A dash in a folder segment will cause each following dot occurrence to be appended with an underscore.
if (( iFindPos = zSegments[i].IndexOf( '-' )) != -1 && iDotPos != -1 )
{
zSegments[i] = zSegments[i].Substring( 0, iFindPos + 1 ) + zSegments[i].Substring( iFindPos + 1 ).Replace( ".", "._" );
}
// A dash is replaced with an underscore when no underscores are in the name or a dot occurrence is before it.
//if (( iFindPos = zSegments[i].IndexOf( '_' )) == -1 || ( iDotPos >= 0 && iDotPos < iFindPos ))
{
zSegments[i] = zSegments[i].Replace( '-', '_' );
}
}
// Each slash is replaced by a dot.
zResource += zSegments[i].Replace( '/', '.' );
}
// Return the assembly name with the resource name.
return String.Concat( _zAssemblyName, zResource );
}
Run Code Online (Sandbox Code Playgroud)
示例调用..
var testResourceName = _GetResourceName( new String[] {
"/",
"Scripts/",
"jQuery.UI-1.8.12/",
"jQuery-_.UI.js"
});
Run Code Online (Sandbox Code Playgroud)