c# - 为什么 System.Reflection.IntrospectionExtensions.GetTypeInfo 有无法访问的代码?

标签 c# .net .net-4.5

新的 .NET4.5 API 在 IntrospectionExtensions 类中具有以下逻辑

public static TypeInfo GetTypeInfo(this Type type)
{
  if (type == (Type) null)
    throw new ArgumentNullException("type");
  IReflectableType reflectableType = (IReflectableType) type;
  if (reflectableType == null) 
    return (TypeInfo) null; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< HERE!
  else
    return reflectableType.GetTypeInfo();
}

为什么这个方法有无法访问的代码?这是错误还是有意为之?

最佳答案

混淆是由 Type 类上定义的 == 运算符引起的。

如果您查看 IL,您会看到调用的是运算符而不是 ReferenceEquals

L_0002: call bool System.Type::op_Equality(class System.Type, class System.Type)

所以代码实际上是可以访问的:)

关于c# - 为什么 System.Reflection.IntrospectionExtensions.GetTypeInfo 有无法访问的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14089706/

相关文章:

c# - 如何从类中调用变量

C# 泛型类型转换 Decimal 到 Float - "Cannot convert type ' T1 ?' to ' T 2'"

c# - 尝试包装多余的代码是否可以接受?

c# - 在单独的进程中运行代码

c# - 使用泛型方法创建接口(interface)

c# - ADO.NET 与 EntityFramework

c# - 使用 Json.NET 将任何类型的对象转换为 JObject

c# - 巢状查询会传回奇怪的结果。 Elasticsearch

c# - 锁定对象字典以减少 C# 中的延迟?

c# - 如何使用 async/await 实现命令模式