4

Is there a way to get the assembly information (name, description, version) programmatically (in C#) without loading the classes into the AppDomain? I just need the information from the manifest and nothing else. Is Assembly.ReflectionOnlyLoad(..) what I need? Or does it load the classes, too?

For Example: I have a dictionary with Files and I want to list the assembly names, descriptions and versions. I don't want to use these Assemblies at this point.

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
mick
  • 127
  • 2
  • 11
  • Note that even if you load with `Assembly.ReflectionOnlyLoad`, you still can't unload it unless you have done it in another `AppDomain` (see http://stackoverflow.com/q/1258165/613130) – xanatos May 18 '15 at 15:08

1 Answers1

8

You're looking for AssemblyName.GetAssemblyName method which returns an AssemblyName instance. It has Name, Version, PublicKey and some basic information about the assembly.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • add the importan remark from that page: *This will only work if the file contains an assembly manifest. This method causes the file to be opened and closed, but the assembly is not added to this domain.* – xanatos May 18 '15 at 15:10
  • 1
    @xanatos Manifest is usually embedded in the assembly isn't it? – Sriram Sakthivel May 18 '15 at 15:11
  • My point was the second sentence, not the first. The `AssemblyName` is correctly programmed and doesn't leave the assembly loaded. – xanatos May 18 '15 at 15:13
  • @xanatos I'm sorry. I can't see what's wrong in opening and closing the file. Without opening and closing the file, how can you read its content? This method will not load the assembly to the appdomain. That's what the OP's requirement also. So, I'm not sure what's your point here. – Sriram Sakthivel May 18 '15 at 15:15
  • @SriramSakthivel He wasn't saying you were wrong, just trying to add some important information to the answer that clarifies **exactly what it does.** – Der Kommissar May 18 '15 at 15:44
  • @EBrown No, he edited his comment later. Initially he was claiming I was wrong. And there was a downvote in my answer at that time(I assume it was him). – Sriram Sakthivel May 18 '15 at 15:45
  • @xanatos Oh, Sorry. My mistake. My assumption was wrong :\ – Sriram Sakthivel May 18 '15 at 17:28
  • @SriramSakthivel creating an `AppDomain`, loading with `Assembly.ReflectionOnlyLoad`, extracting the info, unloading the created `AppDomain`, all together are a pain, and not-easy to write. `Assembly.GetAssemblyName` sidesteps everything and does it "correctly" (I hope :-) ). It is the "not reinvent the wheel" solution. – xanatos May 18 '15 at 17:30
  • thanks for this information - GetAssemlbyName seems to be what i need but unfortunately there is no field for the description - is there something i can do about this? are there any possiblilities to add additional information? thanks for helping me – mick May 20 '15 at 08:03
  • @mick Description isn't part of assembly manifest. So, `GetAssemlbyName ` doesn't gives you that. You need to load the assembly and get the `AssemblyDescriptionAttribute` from the assembly. I'm not sure you can do it without loading the assembly. If nothing works out, you can load the assembly in another app domain, then get the information and tear down the newly created domain. – Sriram Sakthivel May 20 '15 at 08:12
  • @SriramSakthivel Thanks for the answer. That is the way i am doing it right now. but i thought there could be a better way – mick May 20 '15 at 12:23