Jeremy Hance

CF GAC, Loader, Interop, etc...

A Simple Resource Helper Class

using System;
using
System.Reflection;
using
System.IO;
using System.Runtime.CompilerServices;

/// <summary>
///
A simple helper class for extracting Resources from the
///
calling assembly
/// </summary>

public class ResourceHelper
{
   private static Stream GetStream(Assembly assembly, string
ResourceName)
   {
      if (ResourceName != null && ResourceName.Trim() != string
.Empty)
      {
         ResourceName = ResourceName.Trim();
         foreach (string name in
assembly.GetManifestResourceNames())
            if (string.Compare(ResourceName, name, true
) == 0)
               return
assembly.GetManifestResourceStream(name);

         ResourceName = ResourceName.ToUpper();
         
foreach (string name in
assembly.GetManifestResourceNames())
            if
(name.ToUpper().IndexOf(ResourceName) >= 0;
               return
assembly.GetManifestResourceStream(name);
      }

      return new MemoryStream(new byte[0], false);
   }

   /// <summary>
   /// Returns the requested resource as a Stream
   /// </summary>
   /// <param name="ResourceName">
   /// The name of the resource to retrieve
   /// </param>
   /// <returns>
   /// A stream for the requested resource on success. 
   /// An empty stream on failure
   /// </returns>
   /// <remarks>
   /// GetStream first searches for an exact, case insensitive 
   /// match to ResourceName. If the initial search fails,|
   /// GetStream will search again and return the first 
   /// resource for which ResourceName is a case insensitive 
   /// sub-string
   /// </remarks>
   [MethodImpl(MethodImplOptions.NoInlining)]
   public static Stream GetStream(string ResourceName)
   {
      
return GetStream(Assembly.GetCallingAssembly(), ResourceName);
   }

   /// <summary>
   
/// Returns the requested resource as a byte[]
   
/// </summary>
   
/// <param name="ResourceName">
   
/// The name of the resource to retrieve
   
/// </param>
   
/// <returns>
   
/// A byte[] for the requested resource on success. 
   /// An empty byte[] on failure</returns>
   /// <remarks>
   
/// GetBytes first searches for an exact, case insensitive 
   
/// match to ResourceName. If the initial search fails,
   
/// GetBytes will search again and return the first 
   
/// resource for which ResourceName is a case insensitive 
   
/// sub-string
   
/// </remarks>
   
[MethodImpl(MethodImplOptions.NoInlining)]
   
public static byte[] GetBytes(string ResourceName)
   
{
      
Stream s = GetStream(Assembly.GetCallingAssembly(), ResourceName);
      
byte[] bytes = new byte[s.Length];
      
s.Read(bytes, 0, bytes.Length);
      
s.Close();
      
return bytes;
   
}
}

--- jeh
This post is provided “As Is”, with no warranties, and confers no rights.

Published Friday, July 01, 2005 11:09 AM by jehance
Filed under:

Comments

 

Jeremy Hance said:

I knew it'd been a while since I'd updated my blog, but the realization that it'd had been nearly a year...
July 1, 2005 2:38 PM
 

Hasani said:

Nice stuff. a few questions though

1) Why did you no-inline your static methods. Can you give me advise as to when I should not inline my methods?

2) If ResourceName is null, why not throw an ArgumentNullException ?

3) Why not optimize the 1st line to
if(ResourceName != null)
ResourceName = ResourceName.Trim()
so you don't have to call Trim() a second time

4) You do a ToUpper in 1 line, but using String.Compare in another, why not just stick with String.Compare?

Minor stuff, but the code looks solid anyways... Thx for sharing.

I'm new to the CF.NET so expect more questions in the future =]
July 1, 2005 5:22 PM
 

Christopher Steen said:

Link Listing - July 1, 2005
July 2, 2005 12:15 AM
 

Christopher Steen - Learning .NET said:


A Simple Resource Helper Class [Via:
jehance ]
ASP.NET Today: Frames and Grids [Via:
DinoE ]...
July 2, 2005 12:18 AM
 

Christopher Steen - Learning .NET said:


A Simple Resource Helper Class [Via:
jehance ]
ASP.NET Today: Frames and Grids [Via:
DinoE ]...
July 2, 2005 12:19 AM
Anonymous comments are disabled

This Blog

Syndication

News

  • I reserve the right to delete, without notice, any comment, especially those I feel are inappropriate, abusive, offensive, off-topic, or spam.

© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker