Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why System.Reflection.IntrospectionExtensions.GetTypeInfo has unreachable code?

Tags:

c#

.net

.net-4.5

New .NET4.5 API has the following logic in IntrospectionExtensions class

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();
}

Why this method has unreachable code? Is this a bug or done intentionally?

like image 912
serg.f Avatar asked Dec 30 '12 09:12

serg.f


1 Answers

The confusion is caused by the == operator defined on the Type class.

If you look at the IL, you will see the operator is called instead of ReferenceEquals.

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

So the code is in fact reachable :)

like image 138
leppie Avatar answered Nov 07 '22 10:11

leppie