Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2006

Tip: Looking for answers? Try searching our database.

Instantiating an object of a type identified by reflection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
42 - 13 Mar 2006 09:44 GMT
Hi,

I've got a little blurb of code (part of a plugin architecture):

public IPlugin CreateInstance(string assemblyPathName, string className)
{
// Load Assembly
Assembly theAssembly = Assembly.LoadFrom(assemblyPathName);

// Get Class
Type theClass = theAssembly.GetType(className);            

// Call Constructor
IPlugin plugin = (IPlugin)theClass.InvokeMember(
    null,
    BindingFlags.CreateInstance,
    null,
    null,
    null);
return plugin
}

It works perfectly, but the constructor call seems clumsy. Is there a
more direct way? I feel like I've gone the long way around...
like I called the c++

z = x.operator +(4);

instead of just the natural

z = x+4;

...

e.g. I feel like I should somehow be able to write:

plugin = new theTypeClass();

or

plugin = theTypeClass.GetInstance();

Anyhow... just wanted to check whether I've done it the 'right' way. As
I said it works; but it looks over-wrought.

(As an aside, the assemblypathname, and classname are enumerated in an
earlier part of the program which scans the plugin folder for dlls with
public, non-abstract classes that that implement IPlugin... this also
struck me as odd -- again I just have this inkling that there should be
a way of querying an assembly for instantiable classes with a given
interface without rummaging through its "private" parts. Have I missed
something there too?)

-regards,
Dave
InK_ - 13 Mar 2006 12:58 GMT
Hi

try Activator.CreateInstance(...)
e.g. this one:
public static ObjectHandle CreateInstance(
  string assemblyName,
  string typeName
);

Signature

Regards,
Inna Stetsyak aka InK_

> Hi,
>
[quoted text clipped - 51 lines]
> -regards,
> Dave

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.