Exemplo n.º 1
0
        /// <summary>
        ///     <para>Default call for atributtes by the Service Proxy before the methody body.</para>
        /// </summary>
        /// <param name="method">
        ///     <para>Method .</para>
        /// </param>
        /// <param name="il">
        ///     <para>Il from  assembly to override the method.</para>
        /// </param>
        /// <returns>
        ///     <para>true if can execute method</para>
        /// </returns>
        public override bool MtnBeforeExecution(MethodInfo method, ILHelper il)
        {
            if (method.ReturnType == typeof(void))
            {
                throw new InvalidOperationException("Cache needs a return type, void is not allowed");
            }

            ParameterInfo[] parameters = method.GetParameters();

            il
            .CreateLocalVar("Mtn.CacheKey", typeof(string), false)
            .ArrayCreate(typeof(object), parameters.Length + 1)
            .CreateLocalVar("Mtn.CacheParameters", typeof(object[]), false)
            .SetVar("Mtn.CacheParameters")
            .LoadVar("Mtn.CacheParameters")
            .LoadInt(0)
            .LoadString(GetKeyPrefix(method))
            .ArrayAdd()
            ;


            for (int i = 0; i < parameters.Length; i++)
            {
                ParameterInfo parm = parameters[i];
                if (parm.ParameterType.IsByRef || parm.ParameterType.IsPointer)
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  "Parameter '{0}' is not allowed. Cannot use reference or pointer types in cache method.",
                                  parm.Name));
                }

                il
                .LoadVar("Mtn.CacheParameters") // load variable by name
                .LoadInt(i + 1)                 // load a int value in stack
                .LoadArgument(i + 1)            // load a parameter
                .Box(parm.ParameterType)        // do boxing "cast"
                .ArrayAdd();                    // add to array
            }

            il
            .LoadVar("Mtn.CacheParameters")
            .InvokeMethod(typeof(CacheResult), "GetKey")
            .SetVar("Mtn.CacheKey")
            .LoadInt(Convert.ToInt32(ContainerType))
            .Box(ContainerType.GetType())
            .LoadVar("Mtn.CacheKey")
            .InvokeMethod(typeof(CacheResult), "GetCache")
            .SetVar("Mtn.ReturnValue")
            .LoadVar("Mtn.ReturnValue")
            .GotoIfNotNullOrTrue("MtnAfterCall")
            ;

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     <para>Default call for atributtes by the Service Proxy before the methody body.</para>
        /// </summary>
        /// <param name="method">
        ///     <para>Method .</para>
        /// </param>
        /// <param name="il">
        ///     <para>Il from  assembly to override the method.</para>
        /// </param>
        /// <returns>
        ///     <para>true if can execute method</para>
        /// </returns>
        public override bool MtnBeforeExecution(MethodInfo method, ILHelper il)
        {
            const BindingFlags bindFlag =
                BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;

            il
            .CreateLocalVar("Mtn.IsAuthorized", typeof(bool), false)
            .LoadVar("Mtn.IsAuthorized")
            .InvokeMethod(typeof(Authorization), "IsAuthorized", bindFlag)
            .SetVar("Mtn.IsAuthorized")
            .LoadVar("Mtn.IsAuthorized")
            .GotoIfNotNullOrTrue("MtnAfterAuthorization");

            il
            .InvokeMethod(typeof(Authorization).GetProperty("UnauthorizedValue").GetGetMethod())
            .SetVar("Mtn.ReturnValue")
            .LoadVar("Mtn.ReturnValue")
            .GotoIfNotNullOrTrue("MtnAfterAll");

            il.MarkLabel("MtnAfterAuthorization");
            return(true);
        }
Exemplo n.º 3
0
 /// <summary>
 /// <para>Define the variables that will be used later by internal methods </para>
 /// </summary>
 /// <param name="il">ILHelper</param>
 private static void DefineLocalVariables(ILHelper il)
 {
     il
     .CreateLocalVar("Mtn.ReturnValue", typeof(object), false);
 }
Exemplo n.º 4
0
        /// <summary>
        ///     <para>Default call for atributtes by the Service Proxy before the methody body.</para>
        /// </summary>
        /// <param name="method">
        ///     <para>Method .</para>
        /// </param>
        /// <param name="il">
        ///     <para>Il from  assembly to override the method.</para>
        /// </param>
        /// <returns>
        ///     <para>true if can execute method</para>
        /// </returns>
        public override bool MtnBeforeExecution(MethodInfo method, ILHelper il)
        {
            const BindingFlags bindFlag =
                BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;

            var name = "MtnAfterPermission" + Guid.NewGuid();

            // for security reasons set to int.MinValue
            if (Value1 == null)
            {
                Value1 = int.MinValue;
            }
            if (Value2 == null)
            {
                Value2 = int.MinValue;
            }
            if (Value3 == null)
            {
                Value3 = int.MinValue;
            }
            if (Value4 == null)
            {
                Value4 = int.MinValue;
            }

            // create local variables
            il
            .CreateLocalVar("Mtn.IsPermitted", typeof(bool), false)
            .CreateLocalVar("Mtn.PermissionParameter1", Value1.GetType(), true)
            .CreateLocalVar("Mtn.PermissionParameter2", Value2.GetType(), true)
            .CreateLocalVar("Mtn.PermissionParameter3", Value3.GetType(), true)
            .CreateLocalVar("Mtn.PermissionParameter4", Value4.GetType(), true);

            // load Value1
            il
            .LoadVar("Mtn.PermissionParameter1")
            .LoadObject(Value1)
            .Box(Value1.GetType())
            .SetVar("Mtn.PermissionParameter1");

            // load Value2
            il
            .LoadObject(Value2)
            .Box(Value2.GetType())
            .SetVar("Mtn.PermissionParameter2");

            // load Value3
            il
            .LoadObject(Value3)
            .Box(Value3.GetType())
            .SetVar("Mtn.PermissionParameter3");

            // load Value4
            il
            .LoadObject(Value4)
            .Box(Value4.GetType())
            .SetVar("Mtn.PermissionParameter4");

            // put all variables in stack
            il
            .LoadVar("Mtn.PermissionParameter1")
            .UnboxAny(typeof(object))
            .LoadVar("Mtn.PermissionParameter2")
            .UnboxAny(typeof(object))
            .LoadVar("Mtn.PermissionParameter3")
            .UnboxAny(typeof(object))
            .LoadVar("Mtn.PermissionParameter4")
            .UnboxAny(typeof(object));

            // invoke method
            il
            .InvokeMethod(typeof(Permission), "HasPermission", bindFlag)
            .SetVar("Mtn.IsPermitted")
            .LoadVar("Mtn.IsPermitted")
            .GotoIfNotNullOrTrue(name);

            // if is invalid returns the default value
            il
            .InvokeMethod(typeof(Permission).GetProperty("UnallowedResult").GetGetMethod())
            .SetVar("Mtn.ReturnValue")
            .LoadVar("Mtn.ReturnValue")
            .GotoIfNotNullOrTrue("MtnAfterAll");

            il.MarkLabel(name);
            return(true);
        }