分享

Retrieving Resources with the ResourceManager Class

 amine 2010-12-07

Retrieving Resources with the ResourceManager Class

http://msdn.microsoft.com/en-us/library/aa984408%28VS.71%29.aspx

Visual Studio .NET 2003

You can use the ResourceManager Class to retrieve embedded resources (resources that have been compiled into an application or class library) at run time. Each instance of the ResourceManager class is associated with an assembly and manages retrieval of the resources embedded in that assembly.

To retrieve a resource

  1. Create a reference to the assembly that contains the resources you want to access. If the resource-containing assembly has not been loaded, it must be loaded at this time.
  2. Create an instance of ResourceManager class to retrieve the resources.
  3. Specify the base name of the embedded file and provide a reference to the assembly that contains the resources.
    Note   The base name of an embedded resource file is the name of the namespace that the resource file is embedded and the name of the file without any extensions. For example, the base name of a file named Resource1.resX in the myApplication namespace would be myApplication.Resource1.
  4. Call the ResourceManager.GetString Method or ResourceManager.GetObject Method to retrieve your resources.
    • To retrieve String resources, call the GetString method.
    • To retrieve resources of other types, call the GetObject method. Resources retrieved by means of the GetObject method must be explicitly converted to the appropriate type.

// C#
// Gets a reference to the same assembly that
// contains the type that is creating the ResourceManager.
System.Reflection.Assembly myAssembly;
MyAssembly = this.GetType().Assembly;

// Gets a reference to a different assembly.
System.Reflection.Assembly myOtherAssembly;
myOtherAssembly = System.Reflection.Assembly.Load("ResourceAssembly");

// Creates the ResourceManager.
System.Resources.ResourceManager myManager = new
   System.Resources.ResourceManager("ResourceNamespace.myResources",
   myAssembly);

// Retrieves String and Image resources.
System.String myString;
System.Drawing.Image myImage;
myString = myManager.GetString("StringResource");
myImage = (System.Drawing.Image)myManager.GetObject("ImageResource");

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多