我正在迁移。net 4。. x应用程序到。net 6,但上述方法现在已弃用,并且在调用时抛出异常。还有其他选择吗?
这是代码,它被传递一个程序集文件名列表,并加载在AssemblyInfo.cs中发现具有某个程序集级别属性的任何程序集:
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve +=
(s, e) => Assembly.ReflectionOnlyLoad(e.Name);
foreach (var assemblyFile in assemblyFilenames)
{
var reflectionOnlyAssembly = Assembly.ReflectionOnlyLoadFrom(assemblyFile);
if (FoundOurCustomAttr(reflectionOnlyAssembly))
{
var assembly = Assembly.LoadFrom(assemblyFile);
ProcessAssembly(assembly);
}
}
另一种选择是使用MetadataLoadContext包。
var paths = new List<string>(Directory.GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll"));
paths.AddRange(assemblyFilenames);
var resolver = new PathAssemblyResolver(paths);
var ctx = new MetadataLoadContext(resolver);
foreach (var assemblyFile in assemblyFilenames)
{
var assembly = ctx.LoadFromAssemblyPath(assemblyFile);
foreach(var a in assembly.CustomAttributes)
// a.AttributeType
}
可能是方法汇编。装载和装配。LoadFrom是一个有效的选项?