get Default namespace name for Assembly.GetManifestResourceStream() method
-
Wednesday, November 04, 2009 3:40 PMHi, Visual Studio has a way for you to set the assembly name and default namespace name through a project's properties. Assembly.GetManifestResourceStream() method needs the default namespace to get an embedded resources. i.e. DefaultNamespace.EmbeddedResourceFileName
My question is how would one attain this default namespace to get the resource. I know the method has an overload to accept a type and use that types namespace, but the type could always be altered not to use the Default Namespace. Is there a way of using this method without specifically using a type that is in accordance with Visual Studio's default namespace or explicitly putting it in? Or is this method Visual Studio dependant?
All Replies
-
Wednesday, November 04, 2009 3:44 PMModeratorYou can't get the default namespace of a compiled assembly... it's not stored there. The default namespace only exists for Visual Studio to determine the default namespace to use when new classes and other items are created during development.
You'll have to use something like Assembly.GetTypes to find the type with the name "Resources", and then check to see if it has a GeneratedCodeAttribute as a custom attribute on the class. If it does, there's a high likelihood you got the right class. Beyond that, there's no way to perfectly get this data.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser -
Wednesday, November 04, 2009 4:05 PMModerator
My question is how would one attain this default namespace to get the resource. I know the method has an overload to accept a type and use that types namespace, but the type could always be altered not to use the Default Namespace. Is there a way of using this method without specifically using a type that is in accordance with Visual Studio's default namespace or explicitly putting it in? Or is this method Visual Studio dependant?
If this is really a reflection issue, one can browse the classes regardless of namespace. If I am not misunderstanding you I provide such an example on my blog entitled: Reflect Interface from Unknown Assembly in C# . HTH
William Wegerson (www.OmegaCoder.Com) -
Wednesday, November 04, 2009 5:00 PM
You can't get the default namespace of a compiled assembly... it's not stored there. The default namespace only exists for Visual Studio to determine the default namespace to use when new classes and other items are created during development.
That is no guarantee though, I can change the namespace of that as well, so this method is truly visual studio dependent? Is there anyway to assign a namespace the the embedded resource?
You'll have to use something like Assembly.GetTypes to find the type with the name "Resources", and then check to see if it has a GeneratedCodeAttribute as a custom attribute on the class. If it does, there's a high likelihood you got the right class. Beyond that, there's no way to perfectly get this data.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser -
Wednesday, November 04, 2009 5:12 PMModerator
You could use a custom attribute, defined in a separate common assembly, and applied to each of your assemblies at the assembly level.
For example...
1. Create a console application.
2. Create a class library.
3. Reference the class library from the console application.
4. Add the following code to the class library:
namespace System
{
public class DefaultNamespaceAttribute : Attribute
{
private string _defaultNamespace;public DefaultNamespaceAttribute(string defaultNamespace)
{
_defaultNamespace = defaultNamespace;
}public string DefaultNamespace { get { return _defaultNamespace; } }
}
}
5. Add the following code somewhere in your console application:
[assembly:DefaultNamespace("SomeValue")]
6. To fetch this value, once you have the assembly reference, use the following code (assuming the reference to the assembly is called "assm")
DefaultNamespaceAttribute attr = (DefaultNamespaceAttribute) assm
.GetCustomAttributes(
typeof (DefaultNamespaceAttribute), false)[0];
Console.WriteLine(attr.DefaultNamespace);
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marked As Answer by Roahn LuoModerator Friday, November 06, 2009 8:30 AM

