Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t查找嵌入在项目DLL中的.XSL文件_C#_Xslt_Embedded Resource - Fatal编程技术网

C# Can';t查找嵌入在项目DLL中的.XSL文件

C# Can';t查找嵌入在项目DLL中的.XSL文件,c#,xslt,embedded-resource,C#,Xslt,Embedded Resource,这个问题毁了我的早晨,快把我逼疯了 我有一个小项目,其中包括一个DLL从另一个项目。DLL中嵌入了一个XSL文件,我想提取该文件并将其应用于webbrowser控件 我在提取/访问主EXE文件中的嵌入式资源方面没有问题,但在DLL中找不到访问它的方法 我试过: “SolutionName.DllName.Resource.xsl” “ExeName.DllName.Resource.xsl” “ProjectNamespace.DllNamespace.Resource.xsl” …和几乎所

这个问题毁了我的早晨,快把我逼疯了

我有一个小项目,其中包括一个DLL从另一个项目。DLL中嵌入了一个XSL文件,我想提取该文件并将其应用于webbrowser控件

我在提取/访问主EXE文件中的嵌入式资源方面没有问题,但在DLL中找不到访问它的方法

我试过:

  • “SolutionName.DllName.Resource.xsl”
  • “ExeName.DllName.Resource.xsl”
  • “ProjectNamespace.DllNamespace.Resource.xsl”
…和几乎所有的排列,但它永远找不到

我没有在C#中对它的点表示法引用来与
nameof()
一起使用,并且我找不到任何明显的引用/访问:

System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
那么,检索此文件的正确命名(或其他)方法是什么

如果这些有任何帮助,这里有一些额外的细节:

项目名称:DataBuilder
项目名称空间:DataBuilder
DLL名称:cobcomon
DLL名称空间:cobcomon,cobcomon.Classes,cobcomon.Classes.Data,cobcomon.Winforms,cobcomon.Winforms.Controls
XSL资源名称:XmlFormating.XSL

指定的资源文件操作是“嵌入式资源”,它位于DLL项目的“根”区域

访问
global::
使我可以在可用的选项中选择COBComon和DataBuilder,但COBComon既没有
.Properties
也没有
.Resources
选项,而DataBuilder有
.Properties.Resources
则将“文化性”作为唯一的参考

XSL文件列在DLL项目的“属性|资源|文件”选项卡上


我缺少什么?

使用
getExecutionGassembly()
可能总是引用您的程序集。相反,创建一个在外部DLL中声明的无害(希望)简单对象的实例,然后使用该实例对象的

<object-from-DLL>.GetType().Assembly.GetManifestResourceStream("what.youre.looking.for") 
.GetType().Assembly.GetManifestResourceStream(“what.your.looking.for”)

获取嵌入对象的流句柄。

使用
getExecutionGassembly()
可能总是引用程序集。相反,创建一个在外部DLL中声明的无害(希望)简单对象的实例,然后使用该实例对象的

<object-from-DLL>.GetType().Assembly.GetManifestResourceStream("what.youre.looking.for") 
.GetType().Assembly.GetManifestResourceStream(“what.your.looking.for”)

获取嵌入对象的流句柄。

因此,这就是我最终设法组合一个通用函数的方式,以从任何项目程序集中检索任何文本编码的嵌入资源(这在我的项目中按预期工作):

首先,我扩展了
Assembly
类,以便仅获取
Assembly.FullName的相关前导部分,我们需要使用它来搜索请求的资源:

/// <summary>Adds a function to dereference just the base name portion of an Assembly from the FullName.</summary>
/// <returns>The extracted base name if able, otherwise just the FullName.</returns>
public static string ExtractName(this Assembly root)
{
    string pattern = @"^(?<assy>(?:[a-zA-Z\d]+[.]?)+)(?>,).*$", work = root.FullName;
    MatchCollection matches = Regex.Matches(work, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
    if (matches.Count > 0) return matches[0].Groups["assy"].Value;

    if (work.IndexOf(",") > 3) return work.Substring(0, work.IndexOf(','));

    return root.FullName;
}
///添加一个函数,仅从完整名称中取消引用程序集的基本名称部分。
///提取的基名称(如果可以),否则仅为全名。
公共静态字符串ExtractName(此程序集根)
{
字符串模式=@“^(?([a-zA-Z\d]+[.]?)+)(?>,).$”,work=root.FullName;
MatchCollection matches=Regex.matches(工作、模式、RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
如果(matches.Count>0)返回匹配项[0]。组[“assy”]。值;
if(work.IndexOf(“,”)>3)返回work.Substring(0,work.IndexOf(“,”);
返回root.FullName;
}
然后我编写了这个函数来搜索指定的assembly+嵌入式资源文件,并将其内容作为字符串返回(如果找到):

/// <summary>Searches all loaded assemblies in a project for the specified embedded resource text file.</summary>
/// <returns>If the specified resource is found, its contents as a string, otherwise throws a DllNotFoundException.</returns>
/// <exception cref="DllNotFoundException"></exception>
public static string FetchAssemblyResourceFile(string name)
{
    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

    int i = -1; while ((++i < assemblies.Length) && !Regex.IsMatch(name, "^(" + assemblies[i].ExtractName() + ")", RegexOptions.IgnoreCase)) ;
    if (i < assemblies.Length)
    {
        try {
            using (System.IO.Stream stream = assemblies[i].GetManifestResourceStream(name))
            using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
                return reader.ReadToEnd();
        }
        catch (Exception innerExc)
        {
            Exception result = new Exception("The requested resource (\"" + name + "\") was not found.");
            result.InnerException = innerExc;
            throw result;
        }
    }
    throw new DllNotFoundException("The requested assembly resource (\"" + name + "\") could not be found.");
}
///在项目中搜索所有加载的程序集以查找指定的嵌入式资源文本文件。
///如果找到指定的资源,则将其内容作为字符串,否则将抛出DllNotFoundException。
/// 
公共静态字符串FetchAssemblyResourceFile(字符串名称)
{
Assembly[]assemblies=AppDomain.CurrentDomain.GetAssemblies();
int i=-1;while((++i
因此,这就是我最终设法组合一个通用函数的方式,以从任何项目程序集中检索任何文本编码的嵌入式资源(这在我的项目中按预期工作):

首先,我扩展了
Assembly
类,以便仅获取
Assembly.FullName的相关前导部分,我们需要使用它来搜索请求的资源:

/// <summary>Adds a function to dereference just the base name portion of an Assembly from the FullName.</summary>
/// <returns>The extracted base name if able, otherwise just the FullName.</returns>
public static string ExtractName(this Assembly root)
{
    string pattern = @"^(?<assy>(?:[a-zA-Z\d]+[.]?)+)(?>,).*$", work = root.FullName;
    MatchCollection matches = Regex.Matches(work, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
    if (matches.Count > 0) return matches[0].Groups["assy"].Value;

    if (work.IndexOf(",") > 3) return work.Substring(0, work.IndexOf(','));

    return root.FullName;
}
///添加一个函数,仅从完整名称中取消引用程序集的基本名称部分。
///提取的基名称(如果可以),否则仅为全名。
公共静态字符串ExtractName(此程序集根)
{
字符串模式=@“^(?([a-zA-Z\d]+[.]?)+)(?>,).$”,work=root.FullName;
MatchCollection matches=Regex.matches(工作、模式、RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
如果(matches.Count>0)返回匹配项[0]。组[“assy”]。值;
if(work.IndexOf(“,”)>3)返回work.Substring(0,work.IndexOf(“,”);
返回root.FullName;
}
然后我编写了这个函数来搜索指定的assembly+embedde