Hi @Jiang, Zhiyou , Welcome to Microsoft Q&A,
In code, when you set a different directory as a subdirectory, the .NET runtime treats the child class as a different type than the class in the parent class, even though they have the same name and namespace.
Do you mean this?
using System;
namespace _9_13_x
{
internal class Program
{
static void Main(string[] args)
{
var adl = new ScriptDynamicLoader();
// Use IAnimal interface instead of Animal.
var animal = adl.remoteLoader.getObject();
adl.remoteLoader.printObject(animal);
Console.ReadLine();
}
}
public class ScriptDynamicLoader
{
private AppDomain appDomain;
public readonly RemoteLoader remoteLoader;
public ScriptDynamicLoader()
{
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = @"C:\Users\Administrator\Desktop\";
appDomain = AppDomain.CreateDomain("ScriptDomain", null, setup);
remoteLoader = appDomain.CreateInstanceFromAndUnwrap(typeof(RemoteLoader).Assembly.Location, typeof(RemoteLoader).FullName) as RemoteLoader;
}
}
public class RemoteLoader : MarshalByRefObject
{
public IAnimal getObject()
{
var animal = new Animal();
animal.Name = "Dog";
animal.Ege = 10;
Console.WriteLine(AppDomain.CurrentDomain.Id);
return animal;
}
public void printObject(IAnimal animal)
{
animal.print();
}
}
// Define a shared interface without MarshalByRefObject.
public interface IAnimal
{
void print();
}
// Modify the Animal class to inherit from MarshalByRefObject and implement the shared interface.
public class Animal : MarshalByRefObject, IAnimal
{
public string Name;
public int Ege;
public void print()
{
Console.WriteLine($"The name is {Name}, the age is {Ege}. The domain id is {AppDomain.CurrentDomain.Id}.");
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.