Ответ 1
Следует, наверное, отметить, что создание AppDomains просто для того, чтобы обойти что-то, что можно исправить с помощью постоянной строки, вероятно, является неправильным способом сделай это. Если вы пытаетесь сделать то же самое, что и ссылка, которую вы заметили, вы можете просто сделать это:
var configFile = Assembly.GetExecutingAssembly().Location + ".config";
if (!File.Exists(configFile))
throw new Exception("do your worst!");
Рекурсивная точка входа: o)
static void Main(string[] args)
{
if (AppDomain.CurrentDomain.IsDefaultAppDomain())
{
Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
var currentAssembly = Assembly.GetExecutingAssembly();
var otherDomain = AppDomain.CreateDomain("other domain");
var ret = otherDomain.ExecuteAssemblyByName(currentAssembly.FullName, args);
Environment.ExitCode = ret;
return;
}
Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("Hello");
}
Быстрый пример с использованием нестатической вторичной точки входа и MarshalByRefObject...
class Program
{
static AppDomain otherDomain;
static void Main(string[] args)
{
otherDomain = AppDomain.CreateDomain("other domain");
var otherType = typeof(OtherProgram);
var obj = otherDomain.CreateInstanceAndUnwrap(
otherType.Assembly.FullName,
otherType.FullName) as OtherProgram;
args = new[] { "hello", "world" };
Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
obj.Main(args);
}
}
public class OtherProgram : MarshalByRefObject
{
public void Main(string[] args)
{
Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
foreach (var item in args)
Console.WriteLine(item);
}
}