[tests] Test loading references from LoadFrom and LoadFile contexts
[mono-project.git] / mono / tests / remoting4.cs
blobe26edf524907691c5e61b8599ef67fcaa0786905
1 using System;
2 using System.Threading;
3 using System.Runtime.InteropServices;
4 using System.Runtime.Remoting;
5 using System.Runtime.Remoting.Messaging;
6 using System.Runtime.Remoting.Proxies;
7 using System.Runtime.Remoting.Channels;
8 using System.Runtime.Serialization;
10 namespace RemotingTest
12 class MyProxy : RealProxy
14 readonly MarshalByRefObject target;
16 public MyProxy (MarshalByRefObject target) : base (target.GetType())
18 this.target = target;
21 public override IMessage Invoke (IMessage request)
23 IMethodCallMessage call = (IMethodCallMessage)request;
24 Console.WriteLine ("Invoke " + call.MethodName);
26 Console.Write ("ARGS(");
27 for (int i = 0; i < call.ArgCount; i++)
29 if (i != 0)
30 Console.Write (", ");
31 Console.Write (call.GetArgName (i) + " " +
32 call.GetArg (i));
34 Console.WriteLine (")");
35 Console.Write ("INARGS(");
36 for (int i = 0; i < call.InArgCount; i++)
38 if (i != 0)
39 Console.Write (", ");
40 Console.Write (call.GetInArgName (i) + " " +
41 call.GetInArg (i));
43 Console.WriteLine (")");
45 IMethodReturnMessage res = RemotingServices.ExecuteMessage (target, call);
47 Console.Write ("RESARGS(");
48 for (int i = 0; i < res.ArgCount; i++)
50 if (i != 0)
51 Console.Write (", ");
52 Console.Write (res.GetArgName (i) + " " +
53 res.GetArg (i));
55 Console.WriteLine (")");
57 Console.Write ("RESOUTARGS(");
58 for (int i = 0; i < res.OutArgCount; i++)
60 if (i != 0)
61 Console.Write (", ");
62 Console.Write (res.GetOutArgName (i) + " " +
63 res.GetOutArg (i));
65 Console.WriteLine (")");
67 return res;
71 class R2
73 string sTest;
74 public R2()
76 sTest = "R2";
79 public void Print()
81 Console.WriteLine(sTest);
85 [Serializable]
86 class R2_MBV
88 string sTest;
89 public R2_MBV()
91 sTest = "R2";
94 public string Data
96 get
98 return sTest;
103 interface GenericIFace {
104 T Foo <T> ();
107 class R1 : MarshalByRefObject, GenericIFace
109 public R2 TestMBV() {
110 return new R2();
113 public T Foo <T> () {
114 return default (T);
118 class Class1
120 static int Main(string[] args)
122 Console.WriteLine("test " + AppDomain.CurrentDomain.FriendlyName);
123 AppDomain app2 = AppDomain.CreateDomain("2");
125 if (!RemotingServices.IsTransparentProxy(app2))
126 return 1;
128 ObjectHandle o = AppDomain.CurrentDomain.CreateInstance(typeof(R1).Assembly.FullName, typeof(R1).FullName);
129 R1 myobj = (R1) o.Unwrap();
131 // should not be a proxy in our domain..
132 if (RemotingServices.IsTransparentProxy(myobj))
134 Console.WriteLine("CreateInstance return TP for in our current domain");
135 return 2;
138 o = app2.CreateInstance(typeof(R1).Assembly.FullName, typeof(R1).FullName);
140 Console.WriteLine("type: " + o.GetType().ToString());
142 myobj = (R1) o.Unwrap();
143 if (!RemotingServices.IsTransparentProxy(myobj))
144 return 3;
146 Console.WriteLine("unwrapped type: " + myobj.GetType().ToString());
148 R2 r2 = null;
149 bool bSerExc = false;
151 // this should crash
154 r2 = myobj.TestMBV();
156 catch (SerializationException)
158 bSerExc = true;
161 if (!bSerExc)
162 return 4;
164 // Test generic virtual interface methods on proxies
166 o = app2.CreateInstance(typeof(R1).Assembly.FullName, typeof(R1).FullName);
167 myobj = (R1) o.Unwrap();
169 GenericIFace iface = (GenericIFace)myobj;
170 if (iface.Foo <int> () != 0)
171 return 5;
172 if (iface.Foo <string> () != null)
173 return 6;
175 // Test type identity (#504886, comment #10 ff.)
177 if (typeof (R1) != myobj.GetType ())
178 return 7;
180 AppDomain.Unload (app2);
182 Console.WriteLine("test-ok");
183 return 0;