Пример #1
0
        static T CreateFakeBusObject <T>(FakeTransport transport)
        {
            var typeImplementer = new TypeImplementer("test", false);
            var t = typeImplementer.GetImplementation(typeof(TestObj));

            T instObj = (T)Activator.CreateInstance(t);

            BusObject inst = BusObject.GetBusObject(instObj);

            inst.conn        = new Connection(transport);
            inst.bus_name    = "something";
            inst.object_path = new ObjectPath("DontCare");

            return((T)instObj);
        }
        public void ParameterProviderAddsMappingForSubClassWhenBaseClassIsMapped()
        {
            var tpp = new TraceParameterProvider();

            tpp.ForAnything()
            .With <BaseClass>()
            .Trace(c => c.Message);

            var proxy2 = new TypeImplementer(typeof(ISubClassEventSource), tpp).EventSource;

            Assert.DoesNotThrow(delegate
            {
                var proxy = new TypeImplementer(typeof(ISubClassEventSource), tpp).EventSource;
            });
        }
Пример #3
0
        //this requires a seekable stream for now
        public void WriteArray <T> (T[] val)
        {
            Type elemType = typeof(T);

            if (elemType == typeof(byte))
            {
                if (val.Length > ProtocolInformation.MaxArrayLength)
                {
                    ThrowArrayLengthException((uint)val.Length);
                }

                Write((uint)val.Length);
                stream.Write((byte[])(object)val, 0, val.Length);
                return;
            }

            if (elemType.IsEnum)
            {
                elemType = Enum.GetUnderlyingType(elemType);
            }

            Signature sigElem   = Signature.GetSig(elemType);
            int       fixedSize = 0;

            if (endianness == Connection.NativeEndianness && elemType.IsValueType && !sigElem.IsStruct && elemType != typeof(bool) && sigElem.GetFixedSize(ref fixedSize))
            {
                int byteLength = fixedSize * val.Length;
                if (byteLength > ProtocolInformation.MaxArrayLength)
                {
                    ThrowArrayLengthException((uint)byteLength);
                }

                Write((uint)byteLength);
                WritePad(sigElem.Alignment);

                byte[] data = new byte[byteLength];
                Buffer.BlockCopy(val, 0, data, 0, data.Length);
                stream.Write(data, 0, data.Length);

                return;
            }

            long origPos = stream.Position;

            Write((uint)0);

            //advance to the alignment of the element
            WritePad(sigElem.Alignment);

            long startPos = stream.Position;

            TypeWriter <T> tWriter = TypeImplementer.GetTypeWriter <T> ();

            foreach (T elem in val)
            {
                tWriter(this, elem);
            }

            long endPos = stream.Position;
            uint ln     = (uint)(endPos - startPos);

            stream.Position = origPos;

            if (ln > ProtocolInformation.MaxArrayLength)
            {
                ThrowArrayLengthException(ln);
            }

            Write(ln);
            stream.Position = endPos;
        }
Пример #4
0
        /// <summary>
        /// Emits the implementation of a given interface method.
        /// </summary>
        /// <param name="executeMethod">The execute method to implement.</param>
        private void EmitMethodImpl(MethodInfo executeMethod, List <Tuple <FieldInfo, object> > valuesToSet)
        {
            /*
             * public TReturn Method (params)
             * {
             *		var scope = new EventActivityScope(true);
             *		try
             *		{
             *			_log.Method(params);
             *			object value = _execute.Method(params);
             *			_log.Method_Completed(value);
             *		}
             *		catch (Exception e)
             *		{
             *			_log.Method_Faulted(e);
             *			throw;
             *		}
             *		finally
             *		{
             *			scope.Dispose();
             *		}
             * }
             */

            var invocationContext = new InvocationContext(executeMethod, InvocationContextTypes.MethodCall);

            // start building the interface
            MethodBuilder m = _typeBuilder.DefineMethod(executeMethod.Name, MethodAttributes.Public | MethodAttributes.Virtual);

            ProxyHelper.CopyMethodSignature(executeMethod, m);
            var parameterTypes = executeMethod.GetParameters().Select(p => p.ParameterType).ToArray();

            ILGenerator mIL = m.GetILGenerator();

            // set up a place to hold the return value
            LocalBuilder returnValue = null;

            if (m.ReturnType != typeof(void))
            {
                returnValue = mIL.DeclareLocal(m.ReturnType);
            }

            // set up the activity scope
            LocalBuilder scope = null;

            if (_callWithActivityScope)
            {
                scope = mIL.DeclareLocal(typeof(EventActivityScope));
                mIL.Emit(OpCodes.Ldc_I4_1);
                mIL.Emit(OpCodes.Newobj, typeof(EventActivityScope).GetConstructor(new Type[] { typeof(bool) }));
                mIL.Emit(OpCodes.Stloc, scope);
            }

            // start the try block
            mIL.BeginExceptionBlock();

            // call the method on the log that matches the execute method
            var targetParameterTypes = parameterTypes.Select(p => p.IsGenericParameter ? TypeImplementer.GetTypeSupportedByEventSource(p) : p).ToArray();
            var logMethod            = DiscoverMethod(_logField.FieldType, executeMethod.Name, String.Empty, targetParameterTypes);

            if (logMethod != null)
            {
                // call the log method and throw away the result if there is one
                EmitBaseMethodCall(m, invocationContext, _logField, executeMethod, logMethod, valuesToSet);
                if (logMethod.ReturnType != typeof(void))
                {
                    mIL.Emit(OpCodes.Pop);
                }
            }

            // call execute
            EmitBaseMethodCall(m, invocationContext, _executeField, executeMethod, executeMethod, valuesToSet);
            if (executeMethod.ReturnType != typeof(void))
            {
                mIL.Emit(OpCodes.Stloc, returnValue);
            }

            // if there is a completed method, then call that
            var completedParameterTypes = (executeMethod.ReturnType == typeof(void)) ? Type.EmptyTypes : new Type[] { executeMethod.ReturnType };
            var completedMethod         = DiscoverMethod(_logField.FieldType, executeMethod.Name, TypeImplementer.CompletedSuffix, completedParameterTypes);

            if (completedMethod != null)
            {
                // load this._log
                mIL.Emit(OpCodes.Ldarg_0);
                mIL.Emit(OpCodes.Ldfld, _logField);

                // load the value from the local variable
                if (executeMethod.ReturnType != typeof(void))
                {
                    mIL.Emit(OpCodes.Ldloc, returnValue);
                }

                mIL.Emit(OpCodes.Call, completedMethod);
                if (completedMethod.ReturnType != typeof(void))
                {
                    mIL.Emit(OpCodes.Pop);
                }
            }

            // handle exceptions by logging them and rethrowing
            mIL.BeginCatchBlock(typeof(Exception));
            var faultedMethod = DiscoverMethod(_logField.FieldType, executeMethod.Name, TypeImplementer.FaultedSuffix, new Type[] { typeof(Exception) });

            if (faultedMethod != null)
            {
                // save the exception
                var exception = mIL.DeclareLocal(typeof(Exception));
                mIL.Emit(OpCodes.Stloc, exception);

                // load this._log
                mIL.Emit(OpCodes.Ldarg_0);
                mIL.Emit(OpCodes.Ldfld, _logField);

                // load the exception
                mIL.Emit(OpCodes.Ldloc, exception);

                // call the fault handler
                mIL.Emit(OpCodes.Call, faultedMethod);
                if (faultedMethod.ReturnType != typeof(void))
                {
                    mIL.Emit(OpCodes.Pop);
                }
            }

            mIL.Emit(OpCodes.Rethrow);

            // clean up the activity scope
            if (_callWithActivityScope)
            {
                mIL.BeginFinallyBlock();
                mIL.Emit(OpCodes.Ldloc, scope);
                mIL.Emit(OpCodes.Callvirt, typeof(EventActivityScope).GetMethod("Dispose"));
            }

            mIL.EndExceptionBlock();

            // return the result
            if (executeMethod.ReturnType != typeof(void))
            {
                mIL.Emit(OpCodes.Ldloc, returnValue);
            }
            mIL.Emit(OpCodes.Ret);
        }
		public void ParameterProviderAddsMappingForSubClassWhenBaseClassIsMapped()
		{
			var tpp = new TraceParameterProvider();
			tpp.ForAnything()
				.With<BaseClass>()
				.Trace(c => c.Message);

			var proxy2 = new TypeImplementer(typeof(ISubClassEventSource), tpp).EventSource;                

			Assert.DoesNotThrow(delegate
			{
				var proxy = new TypeImplementer(typeof(ISubClassEventSource), tpp).EventSource;                
			});
		}