Getting version of assembly embedded as a resource (SOLVED)
-
I have an assembly embedded as a resource in my application. I'd like to get the System.Version of that assembly. That should be easy but I can't figure out the series of calls needed to accomplish this. I'll try to walk through my problem. I know how to get the version of the executing assembly:
Assembly assembly = Assembly.GetAssembly(GetType());
AssemblyName assemblyName = assembly.GetName();
Version version = assemblyName.Version;So if I could figure out how to get the Assembly object of my assembly embedded in my resources, it would be a trivial exercise. I also know how to get a Stream for the embedded resource:
Uri uri = new Uri("Resources/" + resourceFileName, UriKind.Relative);
StreamResourceInfo sri = Application.GetResourceStream(uri);
Stream componentStream = sri.Stream;So perhaps there's a way of getting an Assembly from a Stream. Let's see...There is An Assembly.ReflectionOnlyLoad(Byte[]) method that returns an Assembly so all I need is a Byte[] from the Stream:
Byte[] bytes = new Byte[componentStream.Length];
int size = componentStream.Read(bytes, 0, (int)componentStream.Length);Then I should be able to get the assembly like this:
Assembly assembly = Assembly.ReflectionOnlyLoad(bytes);
Putting it all together, I should be able to do this:
Uri uri = new Uri("Resources/" + resourceFileName, UriKind.Relative);
StreamResourceInfo sri = Application.GetResourceStream(uri);
Stream componentStream = sri.Stream;
Byte[] bytes = new Byte[componentStream.Length];
int size = componentStream.Read(bytes, 0, (int)componentStream.Length);
Assembly assembly = Assembly.ReflectionOnlyLoad(bytes);
AssemblyName assemblyName = assembly.GetName();
Version version = assemblyName.Version;Let me try that...Hmm. It worked. Thanks for listening to me.
-
I have an assembly embedded as a resource in my application. I'd like to get the System.Version of that assembly. That should be easy but I can't figure out the series of calls needed to accomplish this. I'll try to walk through my problem. I know how to get the version of the executing assembly:
Assembly assembly = Assembly.GetAssembly(GetType());
AssemblyName assemblyName = assembly.GetName();
Version version = assemblyName.Version;So if I could figure out how to get the Assembly object of my assembly embedded in my resources, it would be a trivial exercise. I also know how to get a Stream for the embedded resource:
Uri uri = new Uri("Resources/" + resourceFileName, UriKind.Relative);
StreamResourceInfo sri = Application.GetResourceStream(uri);
Stream componentStream = sri.Stream;So perhaps there's a way of getting an Assembly from a Stream. Let's see...There is An Assembly.ReflectionOnlyLoad(Byte[]) method that returns an Assembly so all I need is a Byte[] from the Stream:
Byte[] bytes = new Byte[componentStream.Length];
int size = componentStream.Read(bytes, 0, (int)componentStream.Length);Then I should be able to get the assembly like this:
Assembly assembly = Assembly.ReflectionOnlyLoad(bytes);
Putting it all together, I should be able to do this:
Uri uri = new Uri("Resources/" + resourceFileName, UriKind.Relative);
StreamResourceInfo sri = Application.GetResourceStream(uri);
Stream componentStream = sri.Stream;
Byte[] bytes = new Byte[componentStream.Length];
int size = componentStream.Read(bytes, 0, (int)componentStream.Length);
Assembly assembly = Assembly.ReflectionOnlyLoad(bytes);
AssemblyName assemblyName = assembly.GetName();
Version version = assemblyName.Version;Let me try that...Hmm. It worked. Thanks for listening to me.
Hey there, why not publish this as a Tip[^]. :) Daniel Vaughan Follow me on Twitter Blog: DanielVaughan.Orpius.com Open Source Projects: Calcium SDK, Clog Organization: Outcoder of PebbleAge
-
Hey there, why not publish this as a Tip[^]. :) Daniel Vaughan Follow me on Twitter Blog: DanielVaughan.Orpius.com Open Source Projects: Calcium SDK, Clog Organization: Outcoder of PebbleAge
Well, I did it, but previewing and correcting doesn't seem to be nearly as clean as working on the message boards. I made a couple of typos that I saw needed correcting but there doesn't seem to be a Preview button that leaves the edit window open. I ended up having to retype the whole blasted thing, then missed some strings I wanted to bold! Not a pleasant experience.
-
Well, I did it, but previewing and correcting doesn't seem to be nearly as clean as working on the message boards. I made a couple of typos that I saw needed correcting but there doesn't seem to be a Preview button that leaves the edit window open. I ended up having to retype the whole blasted thing, then missed some strings I wanted to bold! Not a pleasant experience.
I just gave it a 5. Daniel Vaughan Follow me on Twitter Blog: DanielVaughan.Orpius.com Open Source Projects: Calcium SDK, Clog Organization: Outcoder of PebbleAge
-
I just gave it a 5. Daniel Vaughan Follow me on Twitter Blog: DanielVaughan.Orpius.com Open Source Projects: Calcium SDK, Clog Organization: Outcoder of PebbleAge
Cool, and thanks! I found editing after the post to be much more friendly, and I was able to bold the expressions I'd missed through my second edit. Fortunately I had rights to edit it because I'd made so many posts up here previously (all questions!).