Microsoft Developer Network > Página principal de foros > Visual C# General > get Default namespace name for Assembly.GetManifestResourceStream() method
Formular una preguntaFormular una pregunta
 

Respondidaget Default namespace name for Assembly.GetManifestResourceStream() method

  • miércoles, 04 de noviembre de 2009 15:40Omego2K Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Hi, 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?

Respuestas

  • miércoles, 04 de noviembre de 2009 17:12David M MortonMVP, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida

    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 WikiLinkedInForumsBrowser

Todas las respuestas

  • miércoles, 04 de noviembre de 2009 15:44David M MortonMVP, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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. 

    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 WikiLinkedInForumsBrowser
  • miércoles, 04 de noviembre de 2009 16:05OmegaManMVP, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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)
  • miércoles, 04 de noviembre de 2009 17:00Omego2K Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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. 

    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 WikiLinkedInForumsBrowser
    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?
  • miércoles, 04 de noviembre de 2009 17:12David M MortonMVP, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida

    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 WikiLinkedInForumsBrowser