Пример #1
0
        private void SetMethodReturnType(ServicePack retPack, MethodInfo method, IDynamicMethodBuilder methodBuilder)
        {
            if (!allMethodsVoid)
            {
                var matchReturnType = MapType(method.ReturnType, retPack);

                methodBuilder.SetReturnType(matchReturnType);
            }
        }
Пример #2
0
        private void SetMethodParams(ServicePack retPack, MethodInfo method, IDynamicMethodBuilder methodBuilder)
        {
            var methodParams = method.GetParameters();

            foreach (var param in methodParams)
            {
                var matchParamType = MapType(param.ParameterType, retPack);

                methodBuilder.SetParameter(matchParamType, param.Name);
            }
        }
Пример #3
0
        private void BuildMatchType(IDynamicTypeBuilder typeBuilder, ServicePack retPack)
        {
            SetAttributes(typeBuilder, typeAttributes);

            if (typeCategory != TypeCategories.Implementation)
            {
                SetProperties(typeBuilder, retPack, targetType);

                SetMethods(typeBuilder, retPack);
            }
        }
Пример #4
0
        private Type CreateMatchType(ServicePack retPack)
        {
            var typeBuilder = CreateDynamicTypeBuilder();

            BuildMatchType(typeBuilder, retPack);

            var matchType = typeBuilder.Build();

            typeContainer.Save(matchType);

            return(matchType);
        }
Пример #5
0
        private void MapSubTypes(Type propertyType, ServicePack retPack)
        {
            var props = propertyType.GetProperties();

            foreach (var prop in props)
            {
                if (CheckMapPossiblity(prop.PropertyType, out Type typeToMap))
                {
                    MapType(typeToMap, retPack);
                }
            }
        }
Пример #6
0
        public ServicePack Pack()
        {
            var retPack = new ServicePack();

            if (!TryToGetFromGlobalContainer(targetType, out Type matchType))
            {
                matchType = CreateMatchType(retPack);
            }

            retPack.SetMatchType(matchType);

            retPack.AddRelatedType(matchType, targetType);

            return(retPack);
        }
Пример #7
0
        private void SetMethods(IDynamicTypeBuilder typeBuilder, ServicePack retPack)
        {
            var searchBindingFlags = GetSearchBindingFlags();

            var typeMethods = targetType.GetMethods(searchBindingFlags).Where(method => method.IsVirtual);

            foreach (var method in typeMethods)
            {
                var methodBuilder = typeBuilder.SetMethod(method.Name);

                SetMethodReturnType(retPack, method, methodBuilder);

                SetMethodParams(retPack, method, methodBuilder);

                SetAttributes(methodBuilder, allMembersAttributes);
            }
        }
Пример #8
0
        private void SetProperties(IDynamicTypeBuilder typeBuilder, ServicePack retPack, Type type)
        {
            var typeProps = type.GetProperties();

            foreach (var prop in typeProps)
            {
                var matchType = MapType(prop.PropertyType, retPack);

                var propertyBuilder = typeBuilder.SetProperty(prop.Name, matchType);

                SetAttributes(propertyBuilder, allMembersAttributes);

                if (matchType != prop.PropertyType)
                {
                    MapSubTypes(prop.PropertyType, retPack);
                }
            }
        }
Пример #9
0
        private Type WrapForArraysOrGenerics(Type type, ServicePack retPack, Type matchType)
        {
            Type retType = null;

            if (type.IsGenericType)
            {
                var typeToWrap = matchType ?? type.GetGenericArguments()[0];
                retType = type.GetGenericTypeDefinition().MakeGenericType(new[] { typeToWrap });
            }

            if (type.IsArray)
            {
                var typeToWrap = matchType ?? type.GetElementType();
                retType = typeToWrap.MakeArrayType();
            }

            if (matchType != null && retType != null)
            {
                retPack.AddRelatedType(retType, type);
            }

            return(retType);
        }
Пример #10
0
        private Type MapType(Type type, ServicePack retPack)
        {
            Type matchType = null;

            if (CheckMapPossiblity(type, out Type typeToMap))
            {
                if (retPack.RelatedTypes.Values.Any(sType => sType.Equals(typeToMap)))
                {
                    matchType = retPack.RelatedTypes.Single(kVT => kVT.Value.Equals(typeToMap)).Key;
                }
                else if (TryToGetFromGlobalContainer(type, out Type gottenMatchType))
                {
                    matchType = gottenMatchType;

                    retPack.AddRelatedType(matchType, type);
                }
                else
                {
                    var propMatcher = new ServiceMatcher(typeToMap, TypeCategories.Dto, optimizationPackage: optimizationPackage);

                    SetInvolvedTypesAttributes(propMatcher);

                    SetInvolvedTypeMembersAttributes(propMatcher);

                    var propServicePack = propMatcher.Pack();

                    matchType = propServicePack.MatchType;

                    AddRelatedTypesToRetPack(retPack, propServicePack);
                }
            }

            matchType = WrapForArraysOrGenerics(type, retPack, matchType);

            return(matchType ?? type);
        }