internal static string ToSerializedValue(this ImplementationMethod value)
        {
            switch (value)
            {
            case ImplementationMethod.TSql:
                return("TSql");

            case ImplementationMethod.AzurePowerShell:
                return("AzurePowerShell");
            }
            return(null);
        }
Exemple #2
0
        public override void CreateDeclarations(System.Reflection.Emit.TypeBuilder typeBuilder)
        {
            this.BaseMethod = typeBuilder.BaseType.GetMethods().OfType <MethodInfo>()
                              .Where(p => p.Name == this.MemberName)
                              .FirstOrDefault(p =>
                                              p.ReturnType == this.MemberType &&
                                              p.GetParameters().Length == this.ArgumentTypes.Count &&
                                              p.GetParameters().Select((x, i) => new { Index = i, Type = x.ParameterType }).All(x => this.ArgumentTypes[x.Index] == x.Type));

            if (BaseMethod != null && !BaseMethod.IsVirtual)
            {
                throw new MemberCreationException(this.MemberName, "The method has to be declared virtual on the base class.");
            }
            else if (BaseMethod != null)
            {
                IsOverride = true;
            }

            MethodAttributes methodAttributes = MethodAttributes.Public;

            if (IsProtected)
            {
                methodAttributes = MethodAttributes.Family | MethodAttributes.Virtual | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
            }
            else
            {
                methodAttributes = MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
            }

            var implementationAttributes = MethodAttributes.Private | MethodAttributes.SpecialName | MethodAttributes.HideBySig;

            Method = typeBuilder.DefineMethod(this.MemberName, methodAttributes);
            ImplementationMethod = typeBuilder.DefineMethod(string.Format("{0}_Implementation", this.MemberName), implementationAttributes);

            var genericParameters = GetGenericArguments().ToList();

            if (genericParameters.Any())
            {
                this._genericParameters.AddRange(Method.DefineGenericParameters(genericParameters.ToArray()));
                this._genericImplementationParameters.AddRange(ImplementationMethod.DefineGenericParameters(genericParameters.ToArray()));
            }

            this.Method.SetReturnType(TypeLookup(this.MemberType));
            this.Method.SetParameters(TypeLookup(this.ArgumentTypes).ToArray());

            this.ImplementationMethod.SetReturnType(ImplementationTypeLookup(this.MemberType));
            this.ImplementationMethod.SetParameters(ImplementationTypeLookup(this.ArgumentTypes).ToArray());

            if (IsOverride)
            {
                typeBuilder.DefineMethodOverride(this.Method, this.BaseMethod);
            }
        }
Exemple #3
0
 public virtual object Invoke(IEnumerable <ParameterDescriptor> parameterDescriptors)
 {
     try
     {
         if (ImplementationInstance == null)
         {
             return(null);
         }
         var parameters = parameterDescriptors?.Select(descriptor => descriptor.Value)?.ToArray();
         return(new MethodAccessor(ImplementationMethod, ImplementationMethod.IsCallByLookupVTable()).CreateMethodInvoker()(ImplementationInstance, parameters ?? EmptyArray <object> .Value));
     }
     catch (TargetInvocationException exception)
     {
         throw exception.InnerException;
     }
     catch (Exception exception)
     {
         throw exception;
     }
 }
        public static IStoredType Generate(int seed = 1, ImplementationMethod implementationMethod = ImplementationMethod.Hash)
        {
            string value1 = String.Format("Stored Type #{0}", seed);
            int    value2 = seed;

            switch (implementationMethod)
            {
            case ImplementationMethod.Hash:
                return(new StoredTypeNotifyPropertyChanged(value1, value2));

            case ImplementationMethod.NotifyPropertyChanged:
                return(new StoredTypeNotifyPropertyChanged(value1, value2));

            case ImplementationMethod.NoneUnsupported:
                return(new StoredTypeUnsupported(value1, value2));

            default:
                throw new NotImplementedException();
            }
        }
Exemple #5
0
        public override sealed void Implement(DynamicTypeBuilder config, TypeBuilder typeBuilder)
        {
            Implement(config, typeBuilder, ImplementationMethod.GetILGenerator());
            var il = this.Method.GetILGenerator();
            var parameterArrayVar = il.DeclareLocal(typeof(object[]));

            bool isAsync = false;

            if (this.MemberType != null && typeof(Task).IsAssignableFrom(TypeLookup(this.MemberType)))
            {
                isAsync = true;
            }

            var ctxType = isAsync ? typeof(AsyncInterceptionContext) : typeof(InterceptionContext);

            var ctxVar = il.DeclareLocal(ctxType);
            var interceptionContextCtor = ctxType.GetConstructors().First();

            ConstructorInfo delegateCtor = null;

            if (this.MemberType == null)
            {
                Assembly assembly = null;
                if (this.ArgumentTypes.Count < 9)
                {
                    assembly = typeof(Action).GetTypeInfo().Assembly;
                }
                else
                {
                    assembly = typeof(Enumerable).GetTypeInfo().Assembly;
                }

                var type = GetActionType(this._argumentTypes.Count);
                delegateCtor = type.GetConstructor(new[] { typeof(object), typeof(IntPtr) });
                if (_argumentTypes.Count > 0)
                {
                    type = type.MakeGenericType(TypeLookup(this.ArgumentTypes).ToArray());
                }

                if (this._argumentTypes.OfType <GenericType>().Any())
                {
                    delegateCtor = TypeBuilder.GetConstructor(type, delegateCtor);
                }
                else
                {
                    delegateCtor = type.GetConstructor(new[] { typeof(object), typeof(IntPtr) });
                }
            }
            else
            {
                var type = GetFuncType(this.ArgumentTypes.Count + 1);
                delegateCtor = type.GetConstructor(new[] { typeof(object), typeof(IntPtr) });
                type         = type.MakeGenericType(TypeLookup(this.ArgumentTypes).Concat(new[] { TypeLookup(this.MemberType) }).ToArray());

                if (this._argumentTypes.OfType <GenericType>().Any() || this.MemberType is GenericType)
                {
                    delegateCtor = TypeBuilder.GetConstructor(type, delegateCtor);
                }
                else
                {
                    delegateCtor = type.GetConstructor(new[] { typeof(object), typeof(IntPtr) });
                }
            }

            var returnLabel = il.DefineLabel();

            il.Emit(OpCodes.Ldc_I4, this.ArgumentTypes.Count);
            il.Emit(OpCodes.Newarr, typeof(object));
            il.Emit(OpCodes.Stloc, parameterArrayVar);

            for (int i = 1; i <= this.ArgumentTypes.Count; i++)
            {
                il.Emit(OpCodes.Ldloc, parameterArrayVar);
                il.Emit(OpCodes.Ldc_I4, i - 1);
                il.Emit(OpCodes.Ldarg, i);
                var type = TypeLookup(this._argumentTypes[i - 1]);
                il.Emit(OpCodes.Box, type);
                il.Emit(OpCodes.Stelem_Ref);
            }

            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldstr, this.MemberName);

            il.Emit(OpCodes.Ldarg_0);
            if (this.ImplementationMethod.IsGenericMethod)
            {
                il.Emit(OpCodes.Ldftn, this.ImplementationMethod.MakeGenericMethod(this.GenericParameters.Select(p => p.AsType()).ToArray()));
            }
            else
            {
                il.Emit(OpCodes.Ldftn, this.ImplementationMethod);
            }

            il.Emit(OpCodes.Newobj, delegateCtor);
            il.Emit(OpCodes.Ldloc, parameterArrayVar);
            il.Emit(OpCodes.Newobj, interceptionContextCtor);
            il.Emit(OpCodes.Stloc, ctxVar);

            List <MethodInfo> interceptors = new List <MethodInfo>();

            if (isAsync)
            {
                interceptors = typeBuilder.BaseType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                               .Where(p => !p.IsPrivate && p.GetCustomAttributes(typeof(MethodInterceptorAttribute), true).Any() && p.GetParameters().Count() == 1 && p.GetParameters().First().ParameterType == typeof(AsyncInterceptionContext) && p.ReturnType == typeof(Task))
                               .ToList();
            }
            else
            {
                interceptors = typeBuilder.BaseType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                               .Where(p => !p.IsPrivate && p.GetCustomAttributes(typeof(MethodInterceptorAttribute), true).Any() && p.GetParameters().Count() == 1 && p.GetParameters().First().ParameterType == typeof(InterceptionContext))
                               .ToList();
            }

            var registerMethod = ctxType.GetMethod("RegisterInterceptor");

            foreach (var item in interceptors)
            {
                il.Emit(OpCodes.Ldloc, ctxVar);
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldftn, item);
                if (isAsync)
                {
                    il.Emit(OpCodes.Newobj, typeof(Func <AsyncInterceptionContext, Task>).GetConstructor(new[] { typeof(object), typeof(IntPtr) }));
                }
                else
                {
                    il.Emit(OpCodes.Newobj, typeof(Action <InterceptionContext>).GetConstructor(new[] { typeof(object), typeof(IntPtr) }));
                }

                il.Emit(OpCodes.Ldc_I4, (int)item.GetCustomAttributes(typeof(MethodInterceptorAttribute), true).OfType <MethodInterceptorAttribute>().First().InterceptionMode);
                if (isAsync)
                {
                    il.Emit(OpCodes.Newobj, typeof(DelegateAsyncMethodInterceptor).GetConstructors().First());
                }
                else
                {
                    il.Emit(OpCodes.Newobj, typeof(DelegateMethodInterceptor).GetConstructors().First());
                }

                il.EmitCall(OpCodes.Callvirt, registerMethod, null);
            }

            if (isAsync)
            {
                var taskType = TypeLookup(this.MemberType).GetTypeInfo().IsGenericType ? TypeLookup(this.MemberType).GetGenericArguments().First() : typeof(object);

                il.Emit(OpCodes.Ldloc, ctxVar);
                il.EmitCall(OpCodes.Callvirt, ctxType.GetMethod("ExecuteAsync").MakeGenericMethod(taskType), null);
            }
            else
            {
                il.Emit(OpCodes.Ldloc, ctxVar);
                il.EmitCall(OpCodes.Callvirt, typeof(InterceptionContext).GetMethod("Execute"), null);
                if (this.MemberType != null)
                {
                    il.Emit(OpCodes.Ldloc, ctxVar);
                    il.Emit(OpCodes.Callvirt, typeof(InterceptionContext).GetProperty("Result").GetGetMethod());
                    var returnType = TypeLookup(this.MemberType);
                    if (returnType.GetTypeInfo().IsValueType || returnType.IsGenericParameter)
                    {
                        il.Emit(OpCodes.Unbox_Any, TypeLookup(this.MemberType));
                    }
                    else
                    {
                        il.Emit(OpCodes.Castclass, returnType);
                    }
                }
            }

            il.Emit(OpCodes.Ret);
        }
 public static string ToSerialString(this ImplementationMethod value) => value switch
 {