|I'm trying to write a method that can take the path to any valid assembly and then analyze its attributes, such as title, company, and trademark, but especially custom attributes. Although I will want to get standard assembly attributes, my main purpose for this application is to get information from a custom attribute I created in another project that I add to most of my assemblies.
I have already have a method that take an assembly as a parameter and can pull the information I need. I also have a method that displays an open file dialog window where a user can select a file and then the method determines if it is a valid assembly. What I can't figure out is how to take the path to the assembly file, create an assembly object from it, and then pass it to the method that get the attributes.
He is a VERY stripped down version of the
AssemblyAttributes class just to demonstrator the constructor that take an
Assembly object. This class is not a concern; I have already tested all the code within this class by using the
GetExecutingAssembly method and all the code worked fine:
Public Class AssemblyAttributes
Private _assembly as Assembly
Public Readonly Property Copyright as string
Public Sub New(assembly As Assembly)
_assembly = assembly
And then I have my method, that gets called from a button's click event, which takes the path to an assembly (the stream writer is used later in code that is not shown to output the attribute information). This is the method where I have having trouble:
Private Sub GetAssemblyAttributes(assemblyName As String, ByRef sw As StreamWriter)
Dim currentAssembly As Assembly
Dim assemblyAttributes As New AssemblyAttributes(currentAssembly)
Dim copyright As String = assemblyAttributes.Copyright
How can I using
assemblyName to create the
currentAssembly object? I have tried:
Dim currentAssembly As Assembly = Assembly.Load(name)
And got a
FileLoadException and got the error message:
Could not load file or assembly 'C:\\[PathToAssembly]\\[AssemblyTryingToAnalyze].dll' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
Next I tried:
Dim currentAssembly As Assembly = Assembly.LoadFile(name)
And got a
NotSupportedException and got the error message:
An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous...
But this file is not from a network location; this code and the assembly I'm trying to analyze reside on the same computer.
Finally I tried:
Dim currentAssembly As Assembly = Assembly.LoadFrom(name)
And got the same exception and message as when I tried
I do understand the need to be caution loading 3rd-party libraries but I find it hard to imagine that there is no way to load an assembly simple to grab its attributes (such as the copyright notice, or custom attributes). I tried searching Google but every example I found only showed how to get attributes from the currently running assembly from within its own code. For example this works with the above code:
Dim currentAssembly As Assembly = Assembly.GetExecutingAssembly
So really in the end all I'm asking is, is there someway (and if so how) to get attributes from any assembly that a user make select? Thanks in advance for any help...this has been driving me crazy for the past few hours and my current project is kind of dead right now unless I can get this to work.
A black hole is where God tried to divide by zero.
There are 10 kinds of people in the world; those who understand binary and those who don't.