Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6015767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:53:01+00:00 2026-05-23T02:53:01+00:00

I am trying to create instance of MyClass by factory method in othe application

  • 0

I am trying to create instance of MyClass by factory method in othe application domain is there any solution to this problem?
Edited:
The question is how to do it

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:53:01+00:00Added an answer on May 23, 2026 at 2:53 am

    There are a few options for solving this probelm. Depending on how you want your solution to scale and how complex it already is, you may want to look at MAF (see the System.AddIn namespace) as this handles loading AddIns and supports AppDomain separation already. It also implements a whole load of functionality to do with lifetime management of objects created in the AppDomains and loading/unloading AddIns and versioning.

    If you would rather implement your own, or just want more understanding of AppDomains then here is an example that will hopefully help. It doesn’t do anything with AppDomain setup, security or lifetime management and to make the code more compact there is no error handling but it can be used as a guide.

    If you start with an example where a Person object is created with a factory:

    public class Person
    {
        internal Person(string name)
        {
            Name = name;
        }
    
        public string Name { get; private set; }
    }
    
    public class PersonFactory
    {
        public static Person CreatePerson(string name)
        {
            return new Person(name);
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Person p = PersonFactory.CreatePerson("John Smith");
        }
    }
    

    You can then add to this to create a Person in another AppDomain with a few fairly simple changes to the code above.

  2. Make the class Person a MarshalByRefObject
  3. Add another property to Person that will output their AppDomain for testing
  4. Add another static method to the factory for creating in a different AppDomain
  5. The amended code is:

    public class Person : MarshalByRefObject
    {
        internal Person(string name)
        {
            Name = name;
        }
    
        public string Name { get; private set; }
        public string AppDomainName { get { return AppDomain.CurrentDomain.FriendlyName; } }
    }
    
    public class PersonFactory
    {
        public static Person CreatePerson(string name)
        {
            return new Person(name);
        }
    
        public static Person CreatePersonInAppDomain(string name, AppDomain domain)
        {
            return (Person)domain.CreateInstanceAndUnwrap(
                typeof(Person).Assembly.FullName, 
                typeof(Person).FullName, 
                false, 
                BindingFlags.NonPublic | BindingFlags.Instance, 
                null, 
                new object[] { name }, 
                null, 
                null
                );
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {            
            AppDomain domain = AppDomain.CreateDomain("NewDomain");
    
            Person person1 = PersonFactory.CreatePerson("John Smith");
            Person person2 = PersonFactory.CreatePersonInAppDomain("Jane Smith", domain);
    
            Console.WriteLine("Person: Name={0}, Domain={1}", person1.Name, person1.AppDomainName);
            Console.WriteLine("Person: Name={0}, Domain={1}", person2.Name, person2.AppDomainName);
        }
    }
    

    The output should be:

    Person: Name=John Smith, AppDomain=[your exe name]
    Person: Name=Jane Smith, AppDomain=NewDomain
    

    So, what is going on?

    Since person2 is an object in another AppDomain, it either needs to be Serializable or derive from MarshalByRefObject. In this example, I have derived from MarshalByRefObject so the real instance exists only in the second AppDomain and the reference in the original AppDomain is actually a proxy. If I had chosen the Serializable implementation then a copy of the person would be passed back to the original AppDomain.

    This is something else that needs to be considered as each time the new AppDomain is called any parameters need to be marshalled, which will have some impact on performance. Also, the lifetime of the object needs to be considered too as MarshalByRefObjects will eventually timeout and be collected by the garbage collector. You will need to investigate lifetime management to extend this further.

  • 0
  • Reply
  • Share
    Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp
    • Report

Sidebar

Related Questions

I'm trying to create a single instance Qt application and I'm at the point
I am trying to create a new MyClass instance in MyClass's definition. Why does
I’m trying to create an instance of: java.awt.geom.Point2D.Double in ColdFusion. Point2D.Double is a nested
I'm trying to create an instance of a class at run time. The classes
I'm trying to create an instance of a generic class using a Type object.
I am trying to create a generic class which new's up an instance of
I am trying create a delegate representation of constructor by emitting a Dynamic Method,
I'm trying to create a class with a constructor: class MyClass { public int
Imagine C# code like this ISomeInterface someVar = creator.Create(typeof(SomeClass), typeof(ISomeInterface)); What should happen? Method
I'm trying to embed groovy in java application and got a weird problem. Let's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.