0

Why are these both different when called on same object?

protected override Type GetType(MethodInfo methodInfo, object[] arguments)
{
    var typeOne = (Type)arugments.Single();
    Debug.WriteLine(typeOne.ToString()); // This gives me NewWorld.OldWorld.Class

    var typeTwo = arugments.Single().GetType();
    Debug.WriteLine(typeTwo.ToString()); // This gives me System.RuntimeType

    return typeOne;
}

1 Answers1

2

This is going to get really meta...

Assuming your arguments is actually an array of Types, the expression (Type)arguments.Single() makes the compile-time type of the single argument become Type.

The type Type represents a type. In this case, you got NewWorld.OldWorld.Class, which most likely is the fully qualified name of a class. The single element in arguments (which is of type Type) represents the type NewWord.OldWorld.Class.

The second expression in question, arguments.Single().GetType() gets the runtime type of the object on which GetType is called, as an instance of Type. In this case, This will return the runtime type of arguments.Single(), which is RuntimeType, a subclass of Type.

Basically:

  • (Type)arguments.Single() tells the compiler that arguments contains a single element that is of type Type. It evaluates to a Type object that represents NewWord.OldWord.Class.

  • arguments.Single().GetType() gets the type of argument.Single(). This is not the same as what type arguments.Single() represents. It represents the type Class, but its type is RuntimeType. If you are still confused, here's an example with integers.

    int[] array = new int[] { 10 };
    

    array.Single() represents the number 10 but its type is System.Int32.

Sweeper
  • 213,210
  • 22
  • 193
  • 313