gpt4 book ai didi

.net - 混合 MarshalByRefObject 和 Serializable

转载 作者:行者123 更新时间:2023-12-04 14:49:45 25 4
gpt4 key购买 nike

各种消息来源解释说

When an object derives form MarshalByRefObject, an object reference will be passed from one application domain to another rather than the object itself. When an object is marked with [Serializable], the object will be automatically serialized, transported from one application domain to another and then deserialized to produce an exact copy of the object in the second application domain. Note then that while MarshalByRefObject passes a reference, [Serializable] causes the object to be copied. [source]



我正在设计我的第一个使用 AppDomains 的应用程序,我想知道当您引用 MarshalByRefObjects 时会发生什么情况在未实现 MarshalByRefObject 的可序列化对象中,因为到目前为止我找不到有关该主题的任何文档。

例如,如果我尝试返回 List<MBR> 会发生什么?哪里 MBR : MarshalByRefObject跨越 AppDomain 边界?我能得到一份 List<MBR> 的副本吗?凡 MBRTransparentProxy到原始对象?有没有关于混合这两种机制的技术细节的文档?

最佳答案

我刚刚用 List<MBR> 做了一个快速测试它似乎像我希望的那样工作:

public class MBR : MarshalByRefObject
{
List<MBR> _list;
public MBR() { _list = new List<MBR> { this }; }
public IList<MBR> Test() { return _list; }
public int X { get; set; }
}

// Later...
var mbr = AppDomainStarter.Start<MBR>(@"C:\Program Files", "test", null, true);
var list = mbr.Test();
list[0].X = 42;
list.Clear();
Debug.WriteLine(string.Format("X={0}, Count={1}", mbr.X, mbr.Test().Count));

输出为 X=42, Count=1 ,并且调试器显示 List<MBR>包含 __TransparentProxy .很明显, MarshalByRefObject在另一个按值编码的对象内通过引用成功编码。

如果有人能找到一些文档或技术细节,我仍然希望看到一些文档或技术细节。

对于任何好奇的人,我写了这个方便的沙盒 AppDomainStarter:
/// <summary><see cref="AppDomainStarter.Start"/> starts an AppDomain.</summary>
public static class AppDomainStarter
{
/// <summary>Creates a type in a new sandbox-friendly AppDomain.</summary>
/// <typeparam name="T">A trusted type derived MarshalByRefObject to create
/// in the new AppDomain. The constructor of this type must catch any
/// untrusted exceptions so that no untrusted exception can escape the new
/// AppDomain.</typeparam>
/// <param name="baseFolder">Value to use for AppDomainSetup.ApplicationBase.
/// The AppDomain will be able to use any assemblies in this folder.</param>
/// <param name="appDomainName">A friendly name for the AppDomain. MSDN
/// does not state whether or not the name must be unique.</param>
/// <param name="constructorArgs">Arguments to send to the constructor of T,
/// or null to call the default constructor. Do not send arguments of
/// untrusted types this way.</param>
/// <param name="partialTrust">Whether the new AppDomain should run in
/// partial-trust mode.</param>
/// <returns>A remote proxy to an instance of type T. You can call methods
/// of T and the calls will be marshalled across the AppDomain boundary.</returns>
public static T Start<T>(string baseFolder, string appDomainName,
object[] constructorArgs, bool partialTrust)
where T : MarshalByRefObject
{
// With help from http://msdn.microsoft.com/en-us/magazine/cc163701.aspx
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = baseFolder;

AppDomain newDomain;
if (partialTrust) {
var permSet = new PermissionSet(PermissionState.None);
permSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
permSet.AddPermission(new UIPermission(PermissionState.Unrestricted));
newDomain = AppDomain.CreateDomain(appDomainName, null, setup, permSet);
} else {
newDomain = AppDomain.CreateDomain(appDomainName, null, setup);
}
return (T)Activator.CreateInstanceFrom(newDomain,
typeof(T).Assembly.ManifestModule.FullyQualifiedName,
typeof(T).FullName, false,
0, null, constructorArgs, null, null).Unwrap();
}
}

关于.net - 混合 MarshalByRefObject 和 Serializable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8773814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com