Assembly
Comment le runtime cherche les assemblies
- Assemblies already loaded
- GAC
- codeBase
- Application's root directory
- No culture information
- [assembly name].dll
- [assembly name] / [assembly name].dll
- Culture information is specified for the referenced assembly
- [culture] / [assembly name].dll
- [culture] / [assembly name] / [assembly name].dll
- privatePath (sous dossiers)
- No culture information
- [privatePath] / [assembly name].dll
- [privatePath] / [assembly name] / [assembly name].dll
- Culture information is specified for the referenced assembly
- [privatePath] / [culture] / [assembly name].dll
- [privatePath] / [culture] / [assembly name] / [assembly name].dll
- No culture information
- No culture information
Obtenir la version
GetType().Assembly.GetName().Version.ToString(); Assembly.GetEntryAssembly().GetName().Version.ToString(); var simplifiedVersion = Regex.Replace(version, "(\\.0)+$", string.Empty); |
[Reflection.AssemblyName]::GetAssemblyName('MonAssembly.dll').Version |
Assembly vs File vs Product version
- If Assembly version is not explictly specified, it takes the value of 0.0.0.0.
- If File version is not explicitly specified, it takes the value of Assembly version.
- If Product version is not explicitly specified, it takes the value of File version.
Assembly
// L'exécutable Assembly entryAssembly = Assembly.GetEntryAssembly(); // L'assemblage en cours d'exécution Assembly executingAssembly = Assembly.GetExecutingAssembly(); // L'assemblage ayant appelé la méthode en cours d'exécution Assembly callingAssembly = Assembly.GetCallingAssembly(); // Charger une assembly depuis son nom Assembly assembly = Assembly.Load("Namespace.Class, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"); Assembly assembly = Assembly.LoadWithPartialName("Namespace.Class"); // Charger une assembly depuis un de ses types Assembly assembly = Assembly.GetAssembly(typeof(Console)); // chemin vers le fichier *.exe string path = Assembly.GetEntryAssembly().Location; // chemin vers le dossier contenant le fichier *.exe string folder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); // Version Version assemblyVersion = Assembly.GetEntryAssembly().GetName().Version; string stringAssemblyVersion = assemblyVersion.ToString(); // "1.0.0.0" string fileVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion; string productVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion; |
LoadFile vs LoadFrom
- LoadFrom utilise Fusion et peut charger une autre assembly situé dans un autre chemin mais avec la même identité si celle-ci est déjà chargée dans le LoadFrom context.
- LoadFile n'utilise pas Fusion, c'est exactement l'assembly demandé qui est chargée. Les dépendances ne sont pas automatiquement trouvées dans son dossier. Si elles ne sont pas disponibles dans le Load context, il faut utiliser l'event AssemblyResolve pour les lier.
Method | Advantages | Disadvantages |
---|---|---|
Load | Gets the benefits of versioning and policy. Dependencies available in the Load context are automatically found. |
Dependencies in other contexts are not available unless you subscribe to the AppDomain.AssemblyResolve event. |
LoadFrom | Assemblies can be loaded from multiple paths, not just from beneath the ApplicationBase. Dependencies already loaded in this context will automatically be found. |
If a Load context assembly tries to load this assembly by display name, it will fail to be found, by default (e.g., when mscorlib.dll deserializes this assembly). Worse, an assembly with the same identity, but at a different path, could be found on the probing path, causing an InvalidCastException, MissingMethodException, or unexpected method behavior, later on. |
LoadFile | Avoids probing costs for loading this assembly. You can load multiple assemblies with the same identity into the same appdomain. |
Nothing can bind to this assembly unless you’ve subscribed to the AssemblyResolve event. Dependencies can only be loaded from the Load context or using the AssemblyResolve event. |
Assembly.Load vs LoadFile vs LoadFrom
OnAssemblyResolve
AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve; |
Fuslogvw.exe (Assembly Binding Log Viewer)
Attributs de l'assemblage
// Description object[] attributes = _assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false); if (attributes.Length != 0) { string description = ((AssemblyDescriptionAttribute)attributes[0]).Description; } // Product object[] attributes = _assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false); if (attributes.Length != 0) { string product = ((AssemblyProductAttribute)attributes[0]).Product; } // Copyright object[] attributes = _assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false); if (attributes.Length != 0) { string copyright = ((AssemblyCopyrightAttribute)attributes[0]).Copyright; } // Company object[] attributes = _assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false); if (attributes.Length != 0) { string company= ((AssemblyCompanyAttribute)attributes[0]).Company; } // Guid object[] attributes = _assembly.GetCustomAttributes(typeof(GuidAttribute), false); if (attributes.Length != 0) { string guid = ((GuidAttribute)attributes[0]).Value; } // ClientCompany object[] attributes = _assembly.GetCustomAttributes(typeof(ClientCompanyAttribute), false); if (attributes.Length != 0) { string clientCompany= ((ClientCompanyAttribute)attributes[0]).ClientCompany; } |