get class from name

  • Thread starter Thread starter Joshua Ellul
  • Start date Start date
Hello Joshua,

By getting a class, I assume you mean an instance of it.

Type t = Type.GetType("Your.Fully.Qualified.Class.Name");
object o = Activator.CreateInstance(t);
 
(sorry for replying on your email)
Hi,

Thank for the reply... Yeah an instantiation of the class is what I
required...

However, it does not work for any classes that are in a dll..

E.g. I have a dll "netpetJ.dll" with namespace "netpetJ" with a class
"testclass"

Type t = Type.GetType("netpetJ.testclass");

the gettype function is returning null... do you know what might be the
problem?

thanks,

josh

----- Original Message -----
From: "Matt Berther" <[email protected]>
Newsgroups: microsoft.public.dotnet.framework
Sent: Thursday, April 29, 2004 21:15
Subject: Re: get class from name
 
Joshua said:
(sorry for replying on your email)
Hi,

Thank for the reply... Yeah an instantiation of the class is what I
required...

However, it does not work for any classes that are in a dll..

E.g. I have a dll "netpetJ.dll" with namespace "netpetJ" with a class
"testclass"

Type t = Type.GetType("netpetJ.testclass");

the gettype function is returning null... do you know what might be the
problem?

GetType() generally searches only the 'current' assembly and
mscorlib.dll. If your type is in another assembly, then you need to
pass in the fully qualified typename.

Also, GetType() doesn't search dynamically loaded assemblies. See the
docs for information on these issues.

----- Original Message -----
From: "Matt Berther" <[email protected]>
Newsgroups: microsoft.public.dotnet.framework
Sent: Thursday, April 29, 2004 21:15
Subject: Re: get class from name
 
Hello Joshua,

No biggie, I replied to your email. I'll post my answer here for everyone elses benefit.
(sorry for replying on your email)

If it hasn't been loaded, you'll need to load the assembly.

Assembly asm = AppDomain.CurrentDomain.Load("netpetJ.dll");
Type t = Type.GetType("netpetJ.testclass");
object o = Activator.CreateInstance( t );

Not that before you can call methods on it, you'll need to cast it to the appropriate type.

If you're developing a plug-in style architecture, you'll probably want some sort of base class or interface that your classes would inherit or implement. At that point, you'd do something like:

IMyInterface o = (IMyInterface)Activator.CreateInstance( t );
o.MyMethod();

Let me know if you have more questions...
 
Thanks a lot!!!!

I managed by looping through Assemblies attached and for each assembly each
type... then when I found the type I required I could use that type.

Thanks a Mil,

Josh

Matt Berther said:
Hello Joshua,

No biggie, I replied to your email. I'll post my answer here for everyone elses benefit.


If it hasn't been loaded, you'll need to load the assembly.

Assembly asm = AppDomain.CurrentDomain.Load("netpetJ.dll");
Type t = Type.GetType("netpetJ.testclass");
object o = Activator.CreateInstance( t );

Not that before you can call methods on it, you'll need to cast it to the appropriate type.

If you're developing a plug-in style architecture, you'll probably want
some sort of base class or interface that your classes would inherit or
implement. At that point, you'd do something like:
 
There is a simpler way:

When using Type.GetType() to get the type information for a class, you could
use the following type string and .NET reflection will automatically load
the assembly for you:

RootNamespace.Namespace1.Namespace2.ContainingClassName+NestedClassName,
AssemblyName, Version=0.0.0.0, Culture=neutral, PublicKeyToken=blahblahblah

Note:
1. Only first entry is absolutely needed for classes within the same
assembly, the rest are optional
2. Only first two entries are absolutely needed for classes within other
assemblies, the rest are optional
3. '.' is for separating namespaces from their containing namespaces and
classes from their containing namespaces
4. '+' is for separating nested classes from their container classes

For some reason, classes within the System.dll assembly do not need to
qualified with the assembly name. My guess is that any assemblies that are
referenced by the project do not qualifiers but I haven't verified that.
Specifying assembly names in type strings is always good practice - keeps
things clear.

Good luck!

----------------------------------------------------------------------------
----
Himanshu Swami

Joshua Ellul said:
Thanks a lot!!!!

I managed by looping through Assemblies attached and for each assembly each
type... then when I found the type I required I could use that type.

Thanks a Mil,

Josh

Matt Berther said:
Hello Joshua,

No biggie, I replied to your email. I'll post my answer here for
everyone
elses benefit.
If it hasn't been loaded, you'll need to load the assembly.

Assembly asm = AppDomain.CurrentDomain.Load("netpetJ.dll");
Type t = Type.GetType("netpetJ.testclass");
object o = Activator.CreateInstance( t );

Not that before you can call methods on it, you'll need to cast it to
the
appropriate type.
If you're developing a plug-in style architecture, you'll probably want
some sort of base class or interface that your classes would inherit or
implement. At that point, you'd do something like:
 
Back
Top