// // System.Runtime.Remoting.Messaging.CADMessages.cs // // Author: // Patrik Torstensson // // (C) Patrik Torstensson // // // Copyright (C) 2004 Novell, Inc (http://www.novell.com) // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // using System; using System.Collections; using System.IO; using System.Reflection; using System.Runtime.Serialization; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Messaging; using System.Runtime.Remoting.Proxies; namespace System.Runtime.Remoting.Messaging { internal class CADArgHolder { public int index; public CADArgHolder (int i) { index = i; } } internal class CADObjRef { internal ObjRef objref; internal int SourceDomain; internal byte[] TypeInfo; public CADObjRef (ObjRef o, int sourceDomain) { objref = o; TypeInfo = o.SerializeType (); SourceDomain = sourceDomain; } public string TypeName { get { return objref.TypeInfo.TypeName; } } public string URI { get { return objref.URI; } } } [Serializable] internal class CADMethodRef { internal string FullTypeName; internal IntPtr MethodHandlePtr; public RuntimeMethodHandle MethodHandle { get { return new RuntimeMethodHandle (MethodHandlePtr); } } public CADMethodRef (IMethodMessage msg) { MethodHandlePtr = msg.MethodBase.MethodHandle.Value; FullTypeName = msg.MethodBase.DeclaringType.AssemblyQualifiedName; } } internal class CADMessageBase { protected object [] _args; protected byte [] _serializedArgs = null; protected int _propertyCount = 0; protected CADArgHolder _callContext; internal byte[] serializedMethod; public CADMessageBase (IMethodMessage msg) { CADMethodRef methodRef = new CADMethodRef (msg); serializedMethod = CADSerializer.SerializeObject (methodRef).GetBuffer (); } internal MethodBase method { get { return GetMethod (); } } internal MethodBase GetMethod () { CADMethodRef methRef = (CADMethodRef)CADSerializer.DeserializeObjectSafe (serializedMethod); MethodBase _method; Type tt = Type.GetType (methRef.FullTypeName, true); if (tt.IsGenericType || tt.IsGenericTypeDefinition) { _method = MethodBase.GetMethodFromHandleNoGenericCheck (methRef.MethodHandle); } else { _method = MethodBase.GetMethodFromHandle (methRef.MethodHandle); } if (tt != _method.DeclaringType) { // The target domain has loaded the type from a different assembly. // We need to locate the correct type and get the method from it Type [] signature = GetSignature (_method, true); if (_method.IsGenericMethod) { MethodBase [] methods = tt.GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance); Type [] base_args = _method.GetGenericArguments (); foreach (MethodBase method in methods) { if (!method.IsGenericMethod || method.Name != _method.Name) continue; Type [] method_args = method.GetGenericArguments (); if (base_args.Length != method_args.Length) continue; MethodInfo method_instance = ((MethodInfo) method).MakeGenericMethod (base_args); Type [] base_sig = GetSignature (method_instance, false); if (base_sig.Length != signature.Length) { continue; } bool dont = false; for (int i = base_sig.Length - 1; i >= 0; i--) { if (base_sig [i] != signature [i]) { dont = true; break; } } if (dont) continue; return method_instance; } return _method; } MethodBase mb = tt.GetMethod (_method.Name, BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance, null, signature, null); if (mb == null) throw new RemotingException ("Method '" + _method.Name + "' not found in type '" + tt + "'"); return mb; } return _method; } static protected Type [] GetSignature (MethodBase methodBase, bool load) { ParameterInfo[] pars = methodBase.GetParameters (); Type[] signature = new Type [pars.Length]; for (int n=0; n