【问题标题】:Ignoring Version in an assembly-qualified name passed to Type.GetType()忽略传递给 Type.GetType() 的程序集限定名称中的版本
【发布时间】:2010-11-12 15:51:53
【问题描述】:

当传递给GetType() 的程序集限定名称指定的Version 与实际加载的DLL 版本不同时,是否可以通过Type.GetType() 获得Type?如果是这样,GetType() 的行为是什么?

无论程序集是什么版本,我都想从程序集中获得Type。我有一个函数,它以程序集限定名称作为参数:

Type someType = Type.GetType(someName);

someName 值对应于我想要获取的 Type,但它可能没有指定与我的应用程序中加载的相同的 Version

【问题讨论】:

  • 这个问题不够精确......你想从程序集中获取类型,但不知道要从哪个程序集中获取哪种类型是什么意思?这听起来不太合乎逻辑。 “程序集限定名称”的全部目的是用名称定义版本和令牌。请编辑问题以澄清。

标签: .net reflection types version


【解决方案1】:

我已经成功使用了:

Type type = Type.GetType(typeName, AssemblyResolver, null);

private static System.Reflection.Assembly AssemblyResolver(System.Reflection.AssemblyName assemblyName)
{
    assemblyName.Version = null;
    return System.Reflection.Assembly.Load(assemblyName);
}

【讨论】:

  • 这是正确答案。 - 这救了我的培根。谢谢! :)
【解决方案2】:

在测试中我发现GetType() 将返回正确的类型,即使当前加载的程序集的版本与程序集限定名称的Version 字段中的值不匹配。

【讨论】:

  • 我没有强命名的应用程序,这绝对不是。
  • 这取决于 .net 版本,例如4.7.2 与 5.0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-19
  • 2011-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多