opa*_*era 5 embedded-resource .net-assembly
我构建了一个包含一个js文件的程序集.我将该文件标记为嵌入式资源,并将其添加到AssemblyInfo文件中.
我无法从网站上提及大会.它位于bin文件夹中,但我没有看到它的引用.
好像没有在程序集中至少有一个类我无法引用它.
我会将js文件从程序集中包含到我的页面中.
我该怎么做?
谢谢
我在我的一个项目中做了完全相同的事情。我有一个中央 ScriptManager 类,它实际上在拉取脚本时缓存脚本,但从嵌入资源中提取脚本文件的核心如下所示:
internal static class ScriptManager
{
private static Dictionary<string, string> m_scriptCache =
new Dictionary<string, string>();
public static string GetScript(string scriptName)
{
return GetScript(scriptName, true);
}
public static string GetScript(string scriptName, bool encloseInCdata)
{
StringBuilder script = new StringBuilder("\r\n");
if (encloseInCdata)
{
script.Append("//<![CDATA[\r\n");
}
if (!m_scriptCache.ContainsKey(scriptName))
{
var asm = Assembly.GetExecutingAssembly();
var stream = asm.GetManifestResourceStream(scriptName);
if (stream == null)
{
var names = asm.GetManifestResourceNames();
// NOTE: you passed in an invalid name.
// Use the above line to determine what tyhe name should be
// most common is not setting the script file to be an embedded resource
if (Debugger.IsAttached) Debugger.Break();
return string.Empty;
}
using (var reader = new StreamReader(stream))
{
var text = reader.ReadToEnd();
m_scriptCache.Add(scriptName, text);
}
}
script.Append(m_scriptCache[scriptName]);
if (encloseInCdata)
{
script.Append("//]]>\r\n");
}
return script.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
为了更加清晰,我发布了我的 ScriptManager 类。要提取脚本文件,我只需这样调用它:
var script = ScriptManager.GetScript("Fully.Qualified.Script.js");
Run Code Online (Sandbox Code Playgroud)
您在其中传递的名称是完整的、区分大小写的资源名称(异常处理程序通过调用获取它们的列表GetManifestResourceNames())。
这为您提供了字符串形式的脚本 - 然后您可以将其放入文件中,将其注入页面(这就是我正在做的)或您喜欢的任何内容。
| 归档时间: |
|
| 查看次数: |
5990 次 |
| 最近记录: |