Example #1
0
        void IILCode.Generate(ILExpressed il)
        {
            var instanceIsValueType = _instance != null && _instance.ParameterType != null && _instance.ParameterType.IsValueType;
            if (_instance != null) {
                if (instanceIsValueType)
                    _instance.LoadAddress(il);
                else
                    _instance.Load(il);
            }

            for (var i = 0; i < _parameters.Length; i++) {
                var parameter = (IILCodeParameter) _parameters[i] ?? ILCodeParameter.Null;

                var methodParameter = _methodParameters[i];

                if (methodParameter.IsOut)
                    parameter.LoadAddress(il);
                else
                    parameter.Load(il);
            }

            if (_method.IsStatic || instanceIsValueType)
                il.Call(_method);
            else
                il.CallVirt(_method);
        }
Example #2
0
        void IILCode.Generate(ILExpressed il)
        {
            var variableType = il.TypeCache.Extend(_enumerable.ParameterType);
            var collectionContainer = variableType.Container.AsCollection();

            if (collectionContainer == null)
                throw new InvalidOperationException("Could not enumerate the type " + variableType.Ref.FullName);

            var elementType = collectionContainer.ElementType;
            il.Var.Load(_enumerable);

            var enumerableType = typeof(IEnumerable<>).MakeGenericType(elementType);
            var getEnumeratorMethod = enumerableType.GetMethod("GetEnumerator");
            il.CallVirt(getEnumeratorMethod);

            var enumeratorType = typeof(IEnumerator<>).MakeGenericType(elementType);
            var itLocal = il.DeclareLocal("it", enumeratorType);
            il.Var.Set(itLocal);

            il.Try();

            var itHeadLabel = il.DefineLabel();
            var itBodyLabel = il.DefineLabel();
            il.TransferLong(itHeadLabel);
            var itVarLocal = il.DeclareLocal("cv", elementType);
            var getCurrentMethod = enumeratorType.GetProperty("Current").GetGetMethod();
            il.MarkLabel(itBodyLabel);
            il.Var.Load(itLocal);
            il.Call(getCurrentMethod);
            il.Var.Set(itVarLocal);

            _iterateBody.Invoke(il, itVarLocal);

            il.MarkLabel(itHeadLabel);
            il.Var.Load(itLocal);
            il.CallVirt(EnumeratorMoveNext);

            il.TransferLongIfTrue(itBodyLabel);

            il.Finally();
            il.Var.Load(itLocal);
            il.LoadNull();
            il.CompareEquals();

            var endLabel = il.DefineLabel();
            il.TransferLongIfTrue(endLabel);

            il.Var.Load(itLocal);
            il.CallVirt(DisposableDispose);

            il.MarkLabel(endLabel);
            il.EndTry();
        }
        protected override void OnSet(ILExpressed il)
        {
            if (_instance.VariableType.IsValueType)
                il.Var.LoadAddress(_instance);
            else
                il.Var.Load(_instance);

            if (VariableType.IsValueType)
                il.Call(_info.GetSetMethod());
            else
                il.CallVirt(_info.GetSetMethod());
        }