Вопрос

I want to create a object of type System.Drawing.Point at runtime, I tried to use the code below:

String typename = "System.Drawing.Point";
Type tp = Type.GetType(typename);
Object instance = Activator.CreateInstance(tp);

But I always get tp == null. But, for example if I use System.Double everything is ok.

Это было полезно?

Решение 2

You need to qualify the assembly in your type name, so

String typename = "System.Drawing.Point, System.Drawing";

will work (if System.Drawing.dll is referenced and loaded).

typeName

Type: System.String

The assembly-qualified name of the type to get. [..] If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

Другие советы

Type.GetType will try to load the type from mscorlib. Use assembly-qualified name of the type.

String typename = "System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";

Type tp = Type.GetType(typename);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top