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 6340727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:53:27+00:00 2026-05-24T19:53:27+00:00

I need to do something like this in c# (pseudo): static var ns =

  • 0

I need to do something like this in c# (pseudo):

static var ns = new Non_Serializable_Nor_Marshal()

var app = new AppDomain();
app.execute(foo)

void foo()
{
    var host = AppDomain.Current.Parent; //e.g. the original one
    host.execute(bar)
}

void bar()
{
    ns.Something();
}

IOW I have a non serializeable nor marshal object in one appdomain.
I want to create a second domain and execute foo(). From within that second domain I want to execute bar() on the original domain.

How do I pass the original domain to the child one?

  • 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-24T19:53:27+00:00Added an answer on May 24, 2026 at 7:53 pm

    If you don’t want to use interop, you can also use a little trick using AppDomainManager. You can basically automatically ‘wire’ the ‘primary’ domain into any domains automatically – albiet the way I do it means you discard your real primary domain.

    Here is the class that does all the magic:

    /// <summary>
    /// Represents a <see cref="AppDomainManager"/> that is
    /// aware of the primary application AppDomain.
    /// </summary>
    public class PrimaryAppDomainManager : AppDomainManager
    {
        private static AppDomain _primaryDomain;
    
        /// <summary>
        /// Gets the primary domain.
        /// </summary>
        /// <value>The primary domain.</value>
        public static AppDomain PrimaryDomain
        {
            get
            {
                return _primaryDomain;
            }
        }
    
        /// <summary>
        /// Sets the primary domain.
        /// </summary>
        /// <param name="primaryDomain">The primary domain.</param>
        private void SetPrimaryDomain(AppDomain primaryDomain)
        {
            _primaryDomain = primaryDomain;
        }
    
        /// <summary>
        /// Sets the primary domain to self.
        /// </summary>
        private void SetPrimaryDomainToSelf()
        {
            _primaryDomain = AppDomain.CurrentDomain;
        }
    
        /// <summary>
        /// Determines whether this is the primary domain.
        /// </summary>
        /// <value>
        ///     <see langword="true"/> if this instance is the primary domain; otherwise, <see langword="false"/>.
        /// </value>
        public static bool IsPrimaryDomain
        {
            get
            {
                return _primaryDomain == AppDomain.CurrentDomain;
            }
        }
    
        /// <summary>
        /// Creates the initial domain.
        /// </summary>
        /// <param name="friendlyName">Name of the friendly.</param>
        /// <param name="securityInfo">The security info.</param>
        /// <param name="appDomainInfo">The AppDomain setup info.</param>
        /// <returns></returns>
        public static AppDomain CreateInitialDomain(string friendlyName, Evidence securityInfo, AppDomainSetup appDomainInfo)
        {
            if (AppDomain.CurrentDomain.DomainManager is PrimaryAppDomainManager)
                return null;
    
            appDomainInfo = appDomainInfo ?? new AppDomainSetup();
            appDomainInfo.AppDomainManagerAssembly = typeof(PrimaryAppDomainManager).Assembly.FullName;
            appDomainInfo.AppDomainManagerType = typeof(PrimaryAppDomainManager).FullName;
    
            var appDomain = AppDomainManager.CreateDomainHelper(friendlyName, securityInfo, appDomainInfo);
            ((PrimaryAppDomainManager)appDomain.DomainManager).SetPrimaryDomainToSelf();
            _primaryDomain = appDomain;
            return appDomain;
        }
    
        /// <summary>
        /// Returns a new or existing application domain.
        /// </summary>
        /// <param name="friendlyName">The friendly name of the domain.</param>
        /// <param name="securityInfo">An object that contains evidence mapped through the security policy to establish a top-of-stack permission set.</param>
        /// <param name="appDomainInfo">An object that contains application domain initialization information.</param>
        /// <returns>A new or existing application domain.</returns>
        /// <PermissionSet>
        ///     <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="ControlEvidence, ControlAppDomain, Infrastructure"/>
        /// </PermissionSet>
        public override AppDomain CreateDomain(string friendlyName, Evidence securityInfo, AppDomainSetup appDomainInfo)
        {
            appDomainInfo = appDomainInfo ?? new AppDomainSetup();
            appDomainInfo.AppDomainManagerAssembly = typeof(PrimaryAppDomainManager).Assembly.FullName;
            appDomainInfo.AppDomainManagerType = typeof(PrimaryAppDomainManager).FullName;
    
            var appDomain = base.CreateDomain(friendlyName, securityInfo, appDomainInfo);
            ((PrimaryAppDomainManager)appDomain.DomainManager).SetPrimaryDomain(_primaryDomain);
    
            return appDomain;
        }
    }
    

    And you need to alter your Main() (application entry) slightly:

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main(string[] args)
    {
        new Program().Run(args);
    }
    
    void Run(string[] args)
    {
        var domain = PrimaryAppDomainManager.CreateInitialDomain("PrimaryDomain", null, null);
        if (domain == null)
        {
            // Original Main() code here.
        }
        else
        {
            domain.CreateInstanceAndUnwrap<Program>().Run(args);
        }
    }
    

    Now at any point you can get PrimaryAppDomainManager.PrimaryDomain to get a reference to the primary domain, just remember that it isn’t the inital domain created by the .Net runtime – it’s one we create immediately.

    You can look at the comments in my blog post for an way to get the .Net runtime to hook this in for you automatically using the app.config.

    Edit: I forgot to add the extension method I use, here it is:

    /// <summary>
    /// Creates a new instance of the specified type.
    /// </summary>
    /// <typeparam name="T">The type of object to create.</typeparam>
    /// <param name="appDomain">The app domain.</param>
    /// <returns>A proxy for the new object.</returns>
    public static T CreateInstanceAndUnwrap<T>(this AppDomain appDomain)
    {
        var res = (T)appDomain.CreateInstanceAndUnwrap(typeof(T));
        return res;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need something like this http://jonraasch.com/blog/a-simple-jquery-slideshow but w/o the absolute positioning. Is it possible?
Ok, I need something like this: datediff(second, date_one, date_two) < 1 dates are stored
What I need is something like this: /<[\w\d]+ ([\w\d]+\=[w\d])+\/>/ Something that would match several
I need to do something like this: <input type=button value=click id=mybtn onclick=myfunction('/myController/myAction', 'myfuncionOnOK('/myController2/myAction2', 'myParameter2');',
I guess this is a simple question. I need to do something like this:
i want something like this the user enter a website link i need check
I need(for rapid prototyping and libraries integration) something like this(extensions for usual arrays) double[]
I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
I need to create a wpf treeviewlist to look something like this: AAAA BBBB
I need to match a image url like this: http://site.com/site.com/files/images/img (5).jpg Something like this

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.