我需要在c#(伪)中执行类似的操作:
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我在一个appdomain中有一个非序列化的或marshal对象。 我想创建第二个域并执行foo()。从第二个域内我想在原始域上执行bar()。
如何将原始域名传递给子域名?
答案 0 :(得分:5)
如果您不想使用互操作,您还可以使用AppDomainManager使用小技巧。您基本上可以自动将“主要”域“自动”连接到任何域中 - 与我这样做的方式相同意味着您放弃了真正的主域名。
这是完成所有魔术的课程:
/// <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;
}
}
您需要稍微更改Main()
(申请条目):
/// <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);
}
}
现在,您可以随时获取PrimaryAppDomainManager.PrimaryDomain
以获取对主域的引用,只需记住它不是.Net运行时创建的初始域 - 它是我们立即创建的域。
您可以查看我的blog post中的注释,了解如何让.Net运行时使用app.config自动将其挂钩。
编辑:我忘了添加我使用的扩展方法,这里是:
/// <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;
}
答案 1 :(得分:0)
您可以尝试引用mscoree然后使用其方法。我在我的一个项目中使用过它。 mscoree将在没有任何输入的情况下跟踪您的AppDomain。
/// <summary>
/// Returns the primary application domain.
/// </summary>
/// <returns>The primary application domain.</returns>
public static AppDomain GetPrimaryAppDomain()
{
return GetAppDomain(Process.GetCurrentProcess().MainModule.ModuleName);
}
/// <summary>
/// Returns the application domain with the given friendly name.
/// </summary>
/// <param name="friendlyName">The friendly name of the application domain.</param>
/// <returns>The application domain with the given friendly name.</returns>
/// <exception cref="System.ArgumentNullException">Thrown if friendlyName is null.</exception>
public static AppDomain GetAppDomain(string friendlyName)
{
if (friendlyName == null)
{
throw new ArgumentNullException("friendlyName");
}
IntPtr handle = IntPtr.Zero;
CorRuntimeHostClass host = new CorRuntimeHostClass();
try
{
host.EnumDomains(out handle);
object domain = null;
while (true)
{
host.NextDomain(handle, out domain);
if (domain == null)
{
return null;
}
AppDomain appDomain = (AppDomain)domain;
if (appDomain.FriendlyName == friendlyName)
{
return appDomain;
}
}
}
finally
{
host.CloseEnum(handle);
Marshal.ReleaseComObject(host);
host = null;
}
}
(改编自http://www.dolittle.com/blogs/einar/archive/2007/05/18/cross-appdomain-singleton.aspx)