Пример #1
0
        public void DeclareLocalOverloadDoesNotUnbalanceTheStack()
        {
            var method = new DynamicMethod(String.Empty, typeof(void), Type.EmptyTypes);
            var generator = method.GetILGenerator();
            var il = new ILWriter(generator);

            var localBuilder = il.DeclareLocal<object>();
            il.VerifyStack();
        }
Пример #2
0
        public void DeclareLocalOverloadDoesNotUnbalanceTheStack()
        {
            var method    = new DynamicMethod(String.Empty, typeof(void), Type.EmptyTypes);
            var generator = method.GetILGenerator();
            var il        = new ILWriter(generator);

            var localBuilder = il.DeclareLocal <object>();

            il.VerifyStack();
        }
Пример #3
0
        public void CatchBlockOverloadDoesNotUnbalanceTheStack()
        {
            var method = new DynamicMethod(String.Empty, typeof(void), Type.EmptyTypes);
            var generator = method.GetILGenerator();
            var il = new ILWriter(generator);

            var exceptionBlock = generator.BeginExceptionBlock();
            generator.Emit(OpCodes.Leave, exceptionBlock);
            il.CatchBlock<Exception>(exceptionBlock, () => { });
            il.VerifyStack();
        }
Пример #4
0
        public void CatchBlockOverloadDoesNotUnbalanceTheStack()
        {
            var method    = new DynamicMethod(String.Empty, typeof(void), Type.EmptyTypes);
            var generator = method.GetILGenerator();
            var il        = new ILWriter(generator);

            var exceptionBlock = generator.BeginExceptionBlock();

            generator.Emit(OpCodes.Leave, exceptionBlock);
            il.CatchBlock <Exception>(exceptionBlock, () => { });
            il.VerifyStack();
        }
        void WriteTranslatorBody <TFrom, TTo>(ILWriter writer)
        {
            var sourceProperties         = typeof(TFrom).GetPublicPropertiesWithGetter();
            var targetProperties         = typeof(TTo).GetPublicPropertiesWithSetter().ToDictionary(p => p.Name);
            var targetedSourceProperties = sourceProperties.GetTranslatableProperties <TTo>();

            if (targetProperties.Count == 0)
            {
                writer.ReturnNull();
                return;
            }

            var localTargetInstance = writer.DeclareLocal <TTo>();

            writer.New <TTo>();
            writer.SetLocal(localTargetInstance);

            foreach (var sourceProperty in sourceProperties)
            {
                var explicitMapping = GetExplicitMappingIfPresent <TTo>(sourceProperty);

                var targetName = (explicitMapping == null) ? sourceProperty.Name : explicitMapping.PropertyName;

                if (!targetProperties.ContainsKey(targetName))
                {
                    if (this.shouldThrowExceptions)
                    {
                        throw new PropertyIsMissingException();
                    }
                    continue;
                }

                var targetProperty = targetProperties[targetName];

                var propertiesAreSameType = sourceProperty.PropertyType == targetProperty.PropertyType;

                if (!propertiesAreSameType && !sourceProperty.PropertyType.HasConversionTo(targetProperty.PropertyType))
                {
                    if (this.shouldThrowExceptions)
                    {
                        throw new PropertyTypeMismatchException();
                    }
                    continue;
                }

                var localSourceValue = writer.DeclareLocal(sourceProperty.PropertyType);

                writer.LoadFirstParameter();
                writer.GetPropertyValue(sourceProperty);
                writer.SetLocal(localSourceValue);

                writer.LoadLocal(localTargetInstance);
                writer.LoadLocal(localSourceValue);

                if (!propertiesAreSameType)
                {
                    writer.Cast(sourceProperty.PropertyType, targetProperty.PropertyType);
                }

                writer.SetPropertyValue(targetProperty);
            }

            writer.LoadLocal(localTargetInstance);
            writer.Return();
            writer.VerifyStack();
        }