Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Class.forName()

Class.forName()

Scheduled Pinned Locked Moved C#
csharpjavacomquestionworkspace
2 Posts 2 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jamie Hale
    wrote on last edited by
    #1

    In Java, you can create an instance of an object by name by getting the Class object by name, and then calling createInstance() - or something like that. Is this possible in C#? It looks like I might be able to do it through assemblies, but I'm not sure. Application: I want to be able to provide a configuration file that lists the class names of add-ins to load. The app would loop through these names, create instances and use them. I know I can do it with COM, but I'd rather not subject the authors of add-ins to writing COM servers. Anyways, is this possible? J

    C 1 Reply Last reply
    0
    • J Jamie Hale

      In Java, you can create an instance of an object by name by getting the Class object by name, and then calling createInstance() - or something like that. Is this possible in C#? It looks like I might be able to do it through assemblies, but I'm not sure. Application: I want to be able to provide a configuration file that lists the class names of add-ins to load. The app would loop through these names, create instances and use them. I know I can do it with COM, but I'd rather not subject the authors of add-ins to writing COM servers. Anyways, is this possible? J

      C Offline
      C Offline
      Chris Maunder
      wrote on last edited by
      #2

      You can use the Assembly class to load assemblies at runtime, the GetType function to get an object's type, and the Activator.CreateInstance static function to create an object of that type:

      Assembly a = Assembly.Load("MyLibrary");
      Type t = a.GetType("MyLibrary.MyClass");
      object obj = Activator.CreateInstance(t);

      cheers, Chris Maunder (CodeProject)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups