Microsoft 开发人员网络 > 论坛主页 > Visual C# General > get Default namespace name for Assembly.GetManifestResourceStream() method
提出问题提出问题
 

已答复get Default namespace name for Assembly.GetManifestResourceStream() method

  • 2009年11月4日 15:40Omego2K 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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?

答案

  • 2009年11月4日 17:12David M MortonMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    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

全部回复

  • 2009年11月4日 15:44David M MortonMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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
  • 2009年11月4日 16:05OmegaManMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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)
  • 2009年11月4日 17:00Omego2K 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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?
  • 2009年11月4日 17:12David M MortonMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    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