3 using System.Runtime.Remoting;
4 using System.Runtime.InteropServices;
6 using System.Runtime.Serialization;
8 public class Test: MarshalByRefObject
10 public static int Main (string[] args)
12 AppDomain domain = AppDomain.CreateDomain ("testdomain1");
13 Test server = (Test) domain.CreateInstanceAndUnwrap(typeof(Test).Assembly.FullName, "Test");
14 return server.RunTest ();
23 AppDomain domain = AppDomain.CreateDomain ("testdomain");
24 Remo server = (Remo) domain.CreateInstanceAndUnwrap(typeof(Test).Assembly.FullName,"Remo");
25 if (System.Threading.Thread.GetDomainID () == server.GetDomainId ())
26 throw new TestException ("Object not created in new domain", 1);
32 server.Run2 (88, "hola");
33 server.Run3 (99, d, "adeu");
35 string r = server.Run4 (200, d, "que");
36 CheckValue (r, "vist", 140);
39 server.Run5 (200, d, "que");
40 throw new TestException ("Exception expected", 150);
42 catch (Exception ex) {
43 CheckValue (ex.Message, "peta", 151);
47 d = server.Run6 (99, out d2, "adeu");
48 CheckValue (d.p, 987, 161);
49 CheckValue (d2.p, 987, 162);
53 CheckValue (d.p, 22, 170);
54 CheckValue (d2.p, 33, 170);
56 byte[] ba = new byte[5];
57 for (byte n=0; n<ba.Length; n++)
62 for (int n=0; n<ba.Length; n++)
63 CheckValue (ba[n], (byte) (ba.Length - n), 180);
65 StringBuilder sb = new StringBuilder ("un");
67 Test.CheckValue (sb, new StringBuilder ("un-dos"), 190);
70 catch (TestException ex)
72 Console.WriteLine ("TEST ERROR ({0}): {1}", ex.Code, ex);
77 Console.WriteLine ("TEST ERROR: " + ex);
83 public static void CheckDomain (object ob, Type t, int ec)
85 if (ob == null) return;
86 if (ob.GetType () != t) {
87 if (t.ToString() == ob.GetType().ToString())
88 throw new TestException ("Parameter not properly marshalled", ec);
90 throw new TestException ("Wrong type (maybe wrong domain?)", ec);
94 public static void CheckValue (object ob1, object ob2, int ec)
96 if ((ob1 == null || ob2 == null) && ob1 != ob2)
97 throw new TestException ("Null objects are not equal", ec);
99 if (ob2.GetType () != ob1.GetType ())
100 throw new TestException ("Wrong type (maybe wrong domain?)", ec);
102 if (ob1 is StringBuilder) {
103 if (!((StringBuilder)ob1).Equals ((StringBuilder)ob2))
104 throw new TestException ("Objects are not equal", ec);
106 else if (!ob1.Equals (ob2))
107 throw new TestException ("Objects are not equal", ec);
111 public class Remo: MarshalByRefObject
117 domid = System.Threading.Thread.GetDomainID ();
120 public int GetDomainId ()
125 public void CheckThisDomain (int ec)
127 if (domid != System.Threading.Thread.GetDomainID ())
128 throw new TestException ("Wrong domain", ec);
133 CheckThisDomain (10);
136 public void Run2 (int a, string b)
138 CheckThisDomain (20);
139 Test.CheckValue (a, 88, 21);
140 Test.CheckValue (b, "hola", 22);
143 public void Run3 (int a, Dada d, string b)
145 CheckThisDomain (30);
146 Test.CheckValue (a, 99, 31);
147 Test.CheckValue (b, "adeu", 32);
148 Test.CheckValue (d.p, 22, 33);
151 public string Run4 (int a, Dada d, string b)
153 CheckThisDomain (40);
154 Test.CheckValue (a, 200, 41);
155 Test.CheckValue (b, "que", 42);
156 Test.CheckValue (d.p, 22, 43);
160 public Dada Run5 (int a, Dada d, string b)
162 CheckThisDomain (50);
163 Test.CheckValue (a, 200, 51);
164 Test.CheckValue (b, "que", 52);
165 Test.CheckValue (d.p, 22, 53);
170 public Dada Run6 (int a, out Dada d, string b)
172 CheckThisDomain (60);
173 Test.CheckValue (a, 99, 61);
174 Test.CheckValue (b, "adeu", 62);
181 public Dada Run7 (Dada d)
183 CheckThisDomain (70);
184 Test.CheckValue (d.p, 22, 71);
189 public void Run8 ([In,Out] byte[] bytes)
191 CheckThisDomain (80);
192 Test.CheckDomain (bytes, typeof(byte[]), 81);
193 for (int n=0; n < bytes.Length; n++) {
194 Test.CheckValue (bytes[n], (byte)n, 82);
195 bytes[n] = (byte) (bytes.Length - n);
199 public void Run9 ([In,Out] StringBuilder sb)
201 CheckThisDomain (90);
202 Test.CheckValue (sb, new StringBuilder ("un"), 91);
208 throw new Exception ("peta");
219 public class MyException: Exception
221 public MyException (string s): base (s) {}
225 public class TestException: Exception
227 public int Code = -1;
229 public TestException (SerializationInfo i, StreamingContext ctx): base (i, ctx) {
230 Code = i.GetInt32 ("Code");
233 public TestException (string txt, int code): base (txt + " (code: " + code + ")")
238 public override void GetObjectData (SerializationInfo info, StreamingContext context)
240 base.GetObjectData (info, context);
241 info.AddValue ("Code", Code);