/// <summary>
        /// Intercepts the specified invocation.
        /// </summary>
        /// <param name="invocation">The invocation.</param>
        public void Intercept(IInvocation invocation)
        {
            Contract.Assume(this.interfaceMap.Subject != null);
            Contract.Assume(invocation != null);
            Contract.Assume(invocation.Arguments != null);
            Contract.Assume(invocation.Method != null);

            Type[] lArgumentTypes = Type.GetTypeArray(invocation.Arguments);
            string lMethodName    = invocation.Method.Name;

            Contract.Assume(!string.IsNullOrEmpty(lMethodName));

            MappedMethod mapping = this.interfaceMap.GetMappedMethod(
                this.interfaceMap.Subject.GetType(),
                lMethodName,
                lArgumentTypes,
                invocation.GenericArguments);

            Contract.Assume(mapping != null);

            invocation.ReturnValue = mapping(invocation.Arguments);
            if (invocation.InvocationTarget != null)
            {
                invocation.Proceed();
            }
        }
        /// <summary>
        /// Gets the mapped method.
        /// </summary>
        /// <param name="typeOfTheSubject">The type of the subject.</typeparam>
        /// <param name="name">The method name.</param>
        /// <param name="argTypes">The arguments passed in to the method.</param>
        /// <param name="genericArgs">The generic argument types.</param>
        /// <returns>
        /// The mapped method
        /// </returns>
        public MappedMethod GetMappedMethod(Type typeOfTheSubject, string name, Type[] argTypes, params Type[] genericArgs)
        {
            argTypes    = argTypes ?? Type.EmptyTypes;
            genericArgs = genericArgs ?? Type.EmptyTypes;

            Contract.Assume(!string.IsNullOrEmpty(name));
            IMethodMapping mapping = this.Lookup(name, argTypes, genericArgs);

            if (mapping == default(IMethodMapping))
            {
                Fasterflect.MethodInvoker invoker = typeOfTheSubject.GetMethod(name, argTypes, genericArgs);

                // TODO: Abstract IMethodMapping creation?
                mapping = new MethodMapping()
                {
                    Name                 = name,
                    ArgumentTypes        = argTypes,
                    GenericArgumentTypes = genericArgs ?? Type.EmptyTypes,
                    Subject              = (subject, args) => invoker(subject, args)
                };

                this.Add(mapping);
            }

            MappedMethod method = (args) =>
            {
                return(mapping.Subject(this.Subject, args));
            };

            return(method);
        }
        public void Compile(Mappings result, ModuleDefinition module)
        {
            foreach (var typeDefinition in module.GetAllTypes())
            {
                var type = new MappedType(typeDefinition.FullName, GetOrDefault(typeDefinition.Name));

                foreach (var methodDefinition in typeDefinition.Methods)
                {
                    if (methodDefinition.Name.IsObfuscated() && !Names.ContainsKey(methodDefinition.Name))
                    {
                        continue; // dead method
                    }
                    var method = new MappedMethod(methodDefinition, GetOrDefault(methodDefinition.Name));

                    foreach (var parameterDefinition in methodDefinition.Parameters)
                    {
                        method.Parameters.Add(GetOrDefault(parameterDefinition.Name) ?? parameterDefinition.Name);
                    }

                    if (!methodDefinition.Name.IsObfuscated() && !method.Parameters.Any())
                    {
                        continue;
                    }

                    type.Methods.Add(method);
                }

                foreach (var fieldDefinition in typeDefinition.Fields)
                {
                    var field = new MappedMember(fieldDefinition.Name, GetOrDefault(fieldDefinition.Name) ?? fieldDefinition.Name.Clean());

                    if (!fieldDefinition.Name.IsObfuscated() && fieldDefinition.Name == field.Mapped)
                    {
                        continue;
                    }

                    type.Fields.Add(field);
                }

                foreach (var propertyDefinition in typeDefinition.Properties)
                {
                    var field = new MappedMember(propertyDefinition.Name, GetOrDefault(propertyDefinition.Name));

                    if (!propertyDefinition.Name.IsObfuscated())
                    {
                        continue;
                    }

                    type.Properties.Add(field);
                }

                if (!typeDefinition.Name.IsObfuscated() && !type.Fields.Any() && !type.Methods.Any() && !type.Properties.Any() && !type.Nested.Any())
                {
                    continue;
                }

                result.Types.Add(type);
            }
        }
        /// <summary>
        /// Processes the method invokation.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <typeparam name="TSubject">The type of the subject.</typeparam>
        /// <param name="name">The method name.</param>
        /// <param name="argTypes">The arg types.</param>
        /// <param name="genericArgs">The generic type arguments</param>
        /// <returns>
        /// The result of the call (null if the method is declared as void)
        /// </returns>
        public MappedMethod <TResult> GetMappedMethod <TResult, TSubject>(string name, Type[] argTypes, params Type[] genericArgs)
        {
            argTypes = argTypes ?? Type.EmptyTypes;

            MappedMethod mapping = this.GetMappedMethod <TSubject>(name, argTypes, genericArgs);

            return((args) => (TResult)mapping(args));
        }
Esempio n. 5
0
        /// <summary>
        /// Maps a member from the type's definition to its constructed version.
        /// </summary>
        public IEntity MapMember(IEntity source)
        {
            if (source == null)
            {
                return(null);
            }

            if (_mappedMembers.ContainsKey(source))
            {
                return(_mappedMembers[source]);
            }

            IEntity mapped = null;

            switch (source.EntityType)
            {
            case EntityType.Method:
                mapped = new MappedMethod(_typeSystemServices, ((ExternalMethod)source).MethodInfo, this);
                break;

            case EntityType.Constructor:
                mapped = new MappedConstructor(_typeSystemServices, ((ExternalConstructor)source).ConstructorInfo, this);
                break;

            case EntityType.Field:
                mapped = new MappedField(_typeSystemServices, ((ExternalField)source).FieldInfo, this);
                break;

            case EntityType.Property:
                mapped = new MappedProperty(_typeSystemServices, ((ExternalProperty)source).PropertyInfo, this);
                break;

            case EntityType.Type:
                mapped = MapType((IType)source);
                break;

            case EntityType.Event:
                mapped = new MappedEvent(_typeSystemServices, ((ExternalEvent)source).EventInfo, this);
                break;

            default:
                throw new ArgumentException(
                          string.Format("Invalid entity type for mapping: {0}.", source.EntityType));
            }

            _mappedMembers[source] = mapped;
            return(mapped);
        }
Esempio n. 6
0
        /// <summary>
        /// Creates functionmetadata object with the supplied inputs.
        /// </summary>
        /// <param name="memberType">What type of member e.g. property,function.</param>
        /// <param name="name">The name of the function</param>
        /// <param name="implementationMethod">The method that implements the funcion in this host language.</param>
        /// <param name="returnType">The return values type</param>
        /// <param name="description">Description of the function.</param>
        /// <returns></returns>
        private FunctionMetaData AddMethodInfo(MemberTypes memberType, string name, string implementationMethod, Type returnType, string description)
        {
            var funcdef = new FunctionMetaData(name, null);

            funcdef.Doc = new Docs.DocTags();

            // Todo:
            funcdef.ReturnType  = LangTypeHelper.ConvertToLangType(returnType);
            funcdef.Doc.Summary = description;

            var mappedMethod = new MappedMethod();

            mappedMethod.DataTypeMethod     = name;
            mappedMethod.HostLanguageMethod = implementationMethod;
            mappedMethod.FuncDef            = funcdef;
            _methodMap[name] = mappedMethod;

            _allMembersMap[name] = memberType;
            return(funcdef);
        }
Esempio n. 7
0
        /// <summary>
        /// Creates functionmetadata object with the supplied inputs.
        /// </summary>
        /// <param name="memberType">What type of member e.g. property,function.</param>
        /// <param name="name">The name of the function</param>
        /// <param name="implementationMethod">The method that implements the funcion in this host language.</param>
        /// <param name="returnType">The return values type</param>
        /// <param name="description">Description of the function.</param>
        /// <returns></returns>
        private FunctionMetaData AddMethodInfo(MemberTypes memberType, string name, string implementationMethod, Type returnType, string description, bool convertParams)
        {
            var funcdef = new FunctionMetaData(name, null);
            funcdef.Doc = new DocTags();

            // Todo: 
            funcdef.ReturnType = LangTypeHelper.ConvertToLangType(returnType);
            funcdef.Doc.Summary = description;

            var mappedMethod = new MappedMethod();
            mappedMethod.DataTypeMethod = name;
            mappedMethod.HostLanguageMethod = implementationMethod;
            mappedMethod.FuncDef = funcdef;
            mappedMethod.ConvertParameters = convertParams;
            _methodMap[name] = mappedMethod;

            _allMembersMap[name] = memberType;
            return funcdef;
        }
        protected void MapMethods(string pattern, string method, RequestDelegate requestDelegate)
        {
            var methodItem = new MappedMethod(pattern, method, requestDelegate);

            _methods.Add(methodItem);
        }