C # - Marshall par problème de valeur!
-
12-10-2019 - |
Question
Voici la chose, j'ai un problème de créer un nouvel objet à l'aide du mécanisme à distance « maréchal en valeur ».
Voici ma classe:
[Serializable]
internal class Empleado_MBV
{
public Empleado_MBV()
{
Id = 123456789;
Nombres = "NotEntry";
Apellidos = "NotEntry";
FechaNacimiento = DateTime.MinValue;
Direccion = "NotEntry";
Metapreferencias = "NotEntry";
}
private List<Multas> _multas;
internal List<Multas> Multas
{
get { return _multas; }
set { _multas = value; }
}
private int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _nombres;
public string Nombres
{
get { return _nombres; }
set { _nombres = value; }
}
private string _apellidos;
public string Apellidos
{
get { return _apellidos; }
set { _apellidos = value; }
}
private DateTime _FecNac;
public DateTime FechaNacimiento
{
get { return _FecNac; }
set { _FecNac = value; }
}
private string _direccion;
public string Direccion
{
get { return _direccion; }
set { _direccion = value; }
}
private string _metapreferencias;
public string Metapreferencias
{
get { return _metapreferencias; }
set { _metapreferencias = value; }
}
public string _AppDomainHost
{
get { return AppDomain.CurrentDomain.FriendlyName.ToString(); }
}
}
Mais lorsque je tente de créer un objet dans un autre « appdomain », la propriété « _AppDomainHost » de « Empleado » ne montre pas la « appdomain » J'avais créé, mais montrer la « appdomain » par défaut. Quelques idées?
AppDomain ad1 = AppDomain.CreateDomain("NewAppDomain");
//Crear new object in my new AD.
Empleado_MBV mbv_emp = (Empleado_MBV)ad1.CreateInstanceFromAndUnwrap("DEMO_MBV_MBR.exe", "DEMO_MBV_MBR.Empleado_MBV");
Console.WriteLine(AppDomain.CurrentDomain.FriendlyName.ToString());
Console.WriteLine("MBV : {0}",mbv_emp._AppDomainHost.ToString());
Console.ReadLine();
Résultat:
DEMO_MBV_MBR.vshost.exe
MBV: DEMO_MBV_MBR.vshost.exe
Le résultat que je veux:
DEMO_MBV_MBR.vshost.exe
MBV: NewAppDomain
La solution
Vous devez stocker AppDomain
dans le constructeur de Empleado_MBV
.
Ce que vous faites est en ce moment affichiez AppDomain
actuel en utilisant sa propriété statique Current
. Il retourne le AppDomain
où le code actuel est en cours d'exécution.
Exemple:
private string _appDomainHost;
public string _AppDomainHost
{
get { return _appDomainHost; }
}
et dans le constructeur:
_appDomainHost = AppDomain.CurrentDomain.FriendlyName.ToString();