Re: get class from name
From: Himanshu Swami (himanshu.swami_at_online.nospam)
Date: 04/30/04
- Next message: Chris Harvey: "RE: Error while installing Microsoft Alerts Version 6.0 SDK"
- Previous message: Steven Cheng[MSFT]: "RE: Array Serialization"
- In reply to: Joshua Ellul: "Re: get class from name"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 30 Apr 2004 00:47:17 -0400
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" <jellul@onvol.net> wrote in message
news:eBKj7llLEHA.3696@TK2MSFTNGP09.phx.gbl...
> 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" <mberther@hotmail.com> wrote in message
> news:%23NM1QhlLEHA.3596@tk2msftngp13.phx.gbl...
> > 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...
> >
> > > 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" <mberther@hotmail.com>
> > >> Newsgroups: microsoft.public.dotnet.framework
> > >> Sent: Thursday, April 29, 2004 21:15
> > >> Subject: Re: get class from name
> > >> 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);
> > >>
> > >> Could someone tell me how I can get a class from its name?
> > >>
> > >> --
> > >> Matt Berther
> > >> http://www.mattberther.com
> > >> "Matt Berther" <mberther@hotmail.com> wrote in message
> > >> news:%23hRjQ5hLEHA.1052@TK2MSFTNGP12.phx.gbl...
> > >>
> > >> 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);
> > >>
> > >> Could someone tell me how I can get a class from its name?
> > >>
> > >> --
> > >> Matt Berther
> > >> http://www.mattberther.com
> > --
> >
> > --
> > Matt Berther
> > http://www.mattberther.com
>
>
- Next message: Chris Harvey: "RE: Error while installing Microsoft Alerts Version 6.0 SDK"
- Previous message: Steven Cheng[MSFT]: "RE: Array Serialization"
- In reply to: Joshua Ellul: "Re: get class from name"
- Messages sorted by: [ date ] [ thread ]