Click here to Skip to main content
Licence CPOL
First Posted 1 Sep 2006
Views 17,421
Downloads 318
Bookmarked 14 times

How To Load A Class Into a New Appdomain, and Use It: Quick and Simple

By | 1 Sep 2006 | Article
Appdomain loading

Introduction

This is a quick smash-and-grab type article. Download the code and you'll see how to load a class into a secondary appdomain and use it.

One of the things I miss most about VB6, is the ability to load an object into a separate process, detached from my current process, so if something goes wrong in that object, my main process does not hang (application isolation). In the VB6 world, we create an out of process activex EXE. The .NET way requires us to use Appdomains.

My first read on appdomains left me confused and asking myself "why is this so hard? All I want to do is create an out of process object and use it." After reading Wrox's book "Visual Basic.NET Reflection Handbook", the lights went on and I better understood how these types of things work.

I'm not an expert in this area, but this code will save you some time researching how to create an 'out of process' object the .NET way...

First, you create the app domain:

' Create the app domain
otherDomain = System.AppDomain.CreateDomain("DuffBeerIsGreatDomain")

Next, you create the object:

' Do it the easy way, but you have to pay attention to case in the object.classname

Dim herHello As echo.Hello = _
    CType(otherDomain.CreateInstanceAndUnwrap("echo", "echo.Hello"), echo.Hello)

Now, invoke a method on the object:

MessageBox.Show(herHello.echo("As createinstanceandunwrap"))

It is *that* simple. Now obviously, there are pitfalls, one of them being that in the above createinstanceandunwrap call, the echo.Hello parameter is case sensitive..

The sample code contains example calls which avoid this issue.

Enjoy!

History

  • 1st September, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Random Coding Monkey

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 1 Sep 2006
Article Copyright 2006 by Random Coding Monkey
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid