Load a resource from any namespace in project?

  • Thread starter Thread starter Ed Sutton
  • Start date Start date
E

Ed Sutton

How can I load a resource from any namespace in my project?

I have all my icons located under the following namespace:

..\resources\images\

For example, I can not figure out how to load them from the following
namespace:

..\ui\views\navcontroller\

Thanks in advance for any tips or suggestions,

-Ed
 
Hi Ed,

It depends what kind of resource you want to load. For example if you load
bitmap it constructor has an ovreload that accepts Type object. This type
used only to get the name of the namespace. You can privide any type it
doesn't have to be *this.GetType()*. If you have a type decalred in the same
namespace as the resource or the namespace of the type forms some sufix of
the namspace of the resources you can use it.

For example let say we want to load a bitmap that is located in
FooNamespace.Images.pic.jpg
and you have a class in the FooNamspace you can load the bitmap as

new Bitmap(typeof(Foo), "Images.pic.jpg");
the bitmap constructor will take 'FooNamspace' from the Foo's type object.

If for example you have any kind of resource or there is no class in this
namespace
you can get reference to the Assembly that has that resource and get the
resource as a stream

Stream s =
assem.GetManifestResourceGetManifestResourceStream(FooNamespace.Images.pic.jpg);

And then load the resource from the stream.
 
Back
Top