Search code examples
c#reflection.net-assembly

How to get project namespace name using Assembly.GetExecutingAssembly().GetName().Name


I am creating class library file. In this I embedded stored procedure script file. so I need to take sp data as a string and I have to create in c#. so for this GetManifestResourceStream method need full-name of assemble and script file. so I did . But I did not figure out why my stream object getting null value.

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();
  }
}

Solution

  • It is sort of strange to get constant string value by getting assembly name... But you are missing "." in the way to construct the name:

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

    It will likely be safe and easier to simply hardcode the name:

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

    Note "How to embed and access resources" is available on Micorsoft support site and covers this topic.