MSDN.WhiteKnight - Stack Overflow answers
Ответ на "Обнаружение атрибута в сборке без предварительной загрузки в домен .NET"
Answer 1110046
В комментариях правильно указали на несколько способов, которые позволяют сделать это:
- Assembly.ReflectionOnlyLoad (.NET Framework)
- System.Reflection.Metadata (.NET Standard 1.1+)
- System.Reflection.MetadataLoadContext (.NET Standard 2.0+)
В .NET Core проще всего воспользоваться третьим способом. Подключаем пакет System.Reflection.MetadataLoadContext. Вот так мы можем прочитать значение атрибута сборки:
using System; using System.IO; using System.Collections.Generic; using System.Reflection; using System.Runtime.InteropServices; namespace MetadataLoadContextSample { class Program { static int Main(string[] args) { string inputFile = @"C:\Test\Example.dll"; //проверяемая сборка string attrFile = @"C:\Test\InitLoader.dll"; //сборка с определением атрибута string[] runtimeAssemblies = Directory.GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll"); var paths = new List<string>(runtimeAssemblies); paths.Add(inputFile); paths.Add(attrFile); var resolver = new PathAssemblyResolver(paths); var context = new MetadataLoadContext(resolver); using (context) { Assembly assembly = context.LoadFromAssemblyPath(inputFile); foreach (CustomAttributeData attr in assembly.GetCustomAttributesData()) { if (String.Equals(attr.AttributeType.Name, "InitLoaderAttribute", StringComparison.InvariantCulture)) { Console.WriteLine(attr.ConstructorArguments[0].Value); } } } Console.ReadLine(); return 0; } } }
Content is retrieved from StackExchange API.
Auto-generated by ruso-archive tools.