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"), 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 (Object.ReferenceEquals (ob1, ob2))
104 throw new TestException ("StringBuilders are ReferenceEquals", ec);
106 StringBuilder sb1 = (StringBuilder) ob1;
107 StringBuilder sb2 = (StringBuilder) ob2;
109 if (sb1.ToString () != sb2.ToString ())
110 throw new TestException ("Strings are not equal", ec);
112 else if (!ob1.Equals (ob2))
113 throw new TestException ("Objects are not equal", ec);
117 public class Remo: MarshalByRefObject
123 domid = System.Threading.Thread.GetDomainID ();
126 public int GetDomainId ()
131 public void CheckThisDomain (int ec)
133 if (domid != System.Threading.Thread.GetDomainID ())
134 throw new TestException ("Wrong domain", ec);
139 CheckThisDomain (10);
142 public void Run2 (int a, string b)
144 CheckThisDomain (20);
145 Test.CheckValue (a, 88, 21);
146 Test.CheckValue (b, "hola", 22);
149 public void Run3 (int a, Dada d, string b)
151 CheckThisDomain (30);
152 Test.CheckValue (a, 99, 31);
153 Test.CheckValue (b, "adeu", 32);
154 Test.CheckValue (d.p, 22, 33);
157 public string Run4 (int a, Dada d, string b)
159 CheckThisDomain (40);
160 Test.CheckValue (a, 200, 41);
161 Test.CheckValue (b, "que", 42);
162 Test.CheckValue (d.p, 22, 43);
166 public Dada Run5 (int a, Dada d, string b)
168 CheckThisDomain (50);
169 Test.CheckValue (a, 200, 51);
170 Test.CheckValue (b, "que", 52);
171 Test.CheckValue (d.p, 22, 53);
176 public Dada Run6 (int a, out Dada d, string b)
178 CheckThisDomain (60);
179 Test.CheckValue (a, 99, 61);
180 Test.CheckValue (b, "adeu", 62);
187 public Dada Run7 (Dada d)
189 CheckThisDomain (70);
190 Test.CheckValue (d.p, 22, 71);
195 public void Run8 ([In,Out] byte[] bytes)
197 CheckThisDomain (80);
198 Test.CheckDomain (bytes, typeof(byte[]), 81);
199 for (int n=0; n < bytes.Length; n++) {
200 Test.CheckValue (bytes[n], (byte)n, 82);
201 bytes[n] = (byte) (bytes.Length - n);
205 public void Run9 ([In,Out] StringBuilder sb)
207 CheckThisDomain (90);
208 Test.CheckValue (sb, new StringBuilder ("un"), 91);
214 throw new Exception ("peta");
225 public class MyException: Exception
227 public MyException (string s): base (s) {}
231 public class TestException: Exception
233 public int Code = -1;
235 public TestException (SerializationInfo i, StreamingContext ctx): base (i, ctx) {
236 Code = i.GetInt32 ("Code");
239 public TestException (string txt, int code): base (txt + " (code: " + code + ")")
244 public override void GetObjectData (SerializationInfo info, StreamingContext context)
246 base.GetObjectData (info, context);
247 info.AddValue ("Code", Code);