lundi 29 février 2016

C# GetType weirdness

We have a very simple program invoking Type.GetType static method. Both should return a valid type instance. Only the second one actually is. Looks like something odd is happening with the stack crawl used by GetType, but what exactly is the issue here? Is it bug or some obscure feature?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GetTypeBeingWeird
{
    public class TestClass { }

    class Program
    {
        static void Main(string[] args)
        {
            var fullName = typeof(TestClass).FullName;
            Console.WriteLine("Full name: {0}", fullName);

            new[] { fullName }.Select(Type.GetType).ToList().ForEach(t => Console.WriteLine("Method group: '{0}'", t));
            new[] { fullName }.Select(t => Type.GetType(t)).ToList().ForEach(t => Console.WriteLine("Closure: '{0}'", t));

            return;
        }
    }
}

Running:

Full name: GetTypeBeingWeird.TestClass
Method group: ''
Closure: 'GetTypeBeingWeird.TestClass'





Aucun commentaire:

Enregistrer un commentaire