简体   繁体   中英

Why is this invocation of Activator.CreateInstance failing?

The runtime error is:

System.TypeLoadException: 'Could not load type 'Gorp' from assembly 'foopy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.'

Here is all the code for both of the two classes in the project:

using System;

namespace foopy
{
    class Program
    {
        static void Main(string[] args)
        {
            Object myObject;
            myObject = Activator.CreateInstance(null, "Gorp");
        }
    }
}

namespace foopy
{
    class Gorp
    {
    }
}

在此处输入图像描述

From the docs for that second parameter:

typeName String

The fully qualified name of the preferred type.

(emphasis mine).

You need to pass the fully-qualified name of the type, including the namespace. So "foopy.Gorp" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM