如何使用Assembly.GetExecutingAssembly()获取项目命名空间名称.GetName()。Name

时间:2012-12-13 04:19:24

标签: c# reflection .net-assembly

我正在创建类库文件。在此我嵌入了存储过程脚本文件。所以我需要将sp数据作为字符串,我必须在c#中创建。所以对于这个GetManifestResourceStream方法需要全名的汇编和脚本文件。所以我做了 。但我没弄清楚为什么我的流对象获得空值。

string strNameSpace = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

using (Stream stream = Assembly.GetExecutingAssembly()
   .GetManifestResourceStream(strNameSpace + "GP_SOP_AdjustTax.sql")) 
{
  // Here stream  is null.

  using (StreamReader reader = new StreamReader(stream))
  {
    string result = reader.ReadToEnd();
  }
}

1 个答案:

答案 0 :(得分:2)

通过获取程序集名称获取常量字符串值有点奇怪......但是你缺少“。”在构建名称的方式:

 Assembly.GetExecutingAssembly()
   .GetManifestResourceStream(strNameSpace + ".GP_SOP_AdjustTax.sql"))

简单地对名称进行硬编码可能更安全,更容易:

 Assembly.GetExecutingAssembly()
   .GetManifestResourceStream("WhateverYoourNamespaceIs.GP_SOP_AdjustTax.sql"))

注意Micorsoft support site上提供了“如何嵌入和访问资源”,并涵盖了此主题。

相关问题