C#.NET - Type.GetType(“System.Windows.Forms.Form”) returns null

后端 未结 3 1749
Happy的楠姐
Happy的楠姐 2021-01-28 10:42

I have a snippet of code in my application (which references System.Windows.Forms) which attempts to resolve type information for a Form class like so:

3条回答
  •  离开以前
    2021-01-28 11:06

    Check this Jon Skeet answer on this : https://stackoverflow.com/a/3758295/314488

    using System;
    using System.Windows.Forms;
    
    class Test
    {
        static void Main()
        {
            string name = typeof(Form).AssemblyQualifiedName;
            Console.WriteLine(name);
    
            Type type = Type.GetType(name);
            Console.WriteLine(type);
        }
    }
    Output:
    
    System.Windows.Forms.Form, System.Windows.Forms, Version=4.0.0.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089
    System.Windows.Forms.Form
    

    Note that if you're using a strongly named assembly (like Form in this case) you must include all the assembly information - versioning, public key token etc.

提交回复
热议问题