示例#1
0
        public void GeneratePropertyCode(StringBuilder sb)
        {
            if (PropertyType.IsNested)
            {
                Console.WriteLine("TODO: Support for nested class: " + PropertyType.FullName);
                return;
            }

            var targetType = PropertyType;

            var validMapping = WrapperRepository.GetValidMapping(targetType);

            string getBody = string.Empty;

            if (validMapping != null)
            {
                Type collectionType = Repository.GetMethodReturnListType(GetterMethod);

                if (collectionType == null)
                {
                    Console.WriteLine("TODO: Recognize collection " + FullNameDest);
                }
                else if (collectionType.IsNested)
                {
                    Console.WriteLine("TODO: Support for nested class: " + collectionType.FullName);
                }
                else
                {
                    if (SetterMethod != null)
                    {
                        Console.WriteLine("TODO: Can't generate setter wrapper for wrapped collection " + FullNameDest);
                    }

                    if (Repository.TypeIsWrapped(collectionType))
                    {
                        Repository.MarkUsageOfType(collectionType);
                    }

                    string collectionTypeName = Repository.TypeIsWrapped(collectionType)
                                                    ? Repository.GetTargetFullName(collectionType)
                                                    : collectionType.FullName;

                    if (ClassInfo.IsInterface)
                    {
                        sb.AppendFormat(
                            "      {0}<{1}> {2} {{ get; }}\r\n", validMapping.ToTypeName, collectionTypeName, Name);
                    }
                    else
                    {
                        getBody =
                            @"
      public {0}<{1}> {2}
      {{
         get
         {{
            return new {3}<{1}>(
               WObj.{4}());
         }}
       }}
";

                        sb.AppendFormat(
                            getBody,
                            /* {0} */ validMapping.ToTypeName,
                            /* {1} */ collectionTypeName,
                            /* {2} */ Name,
                            /* {3} */ Repository.TypeIsWrapped(collectionType)
                                          ? validMapping.FullWrapperName
                                          : validMapping.ShallowWrapperName,
                            /* {4} */ GetterMethod.Name);
                    }
                }
            }
            else
            {
                var propTypeIsWrapped = Repository.TypeIsWrapped(PropertyType);

                if (propTypeIsWrapped)
                {
                    Repository.MarkUsageOfType(PropertyType);
                }

                string typeName = propTypeIsWrapped
                                      ? Repository.GetTargetFullName(PropertyType)
                                      : PropertyType.FullName;

                if (ClassInfo.IsInterface)
                {
                    string propBody =
                        SetterMethod != null
                            ? "{ get; set; }"
                            : " { get; }";

                    sb.AppendFormat("      {0} {1} {2}\r\n", typeName, Name, propBody);
                }
                else
                {
                    if (!propTypeIsWrapped)
                    {
                        var setterBodyTemplate =
                            @"
         set
         {{
            WObj.{0}(value);
         }}
";

                        var propBodyTemplate =
                            @"
      public {0} {1}
      {{
         get
         {{
            return WObj.{2}();
         }}{3}
      }}
";

                        sb.AppendFormat(
                            propBodyTemplate,
                            /* {0} */ typeName,
                            /* {1} */ Name,
                            /* {2} */ GetterMethod.Name,
                            /* {3} */ SetterMethod != null
                                          ? string.Format(setterBodyTemplate, SetterMethod.Name)
                                          : string.Empty);
                    }
                    else
                    {
                        var setterBodyTemplate =
                            @"
         set
         {{
            WObj.{0}(({1})value.WrappedObject);
         }}
";

                        var propBodyTemplate =
                            @"
      public {0} {1}
      {{
         get
         {{
            return ObjectWrapper.CreateWrapper<{0}>(
               WObj.{2}());
         }}{3}
      }}

";

                        sb.AppendFormat(
                            propBodyTemplate,
                            /* {0} */ typeName,
                            /* {1} */ Name,
                            /* {2} */ GetterMethod.Name,
                            /* {3} */ SetterMethod != null
                                          ? string.Format(setterBodyTemplate, SetterMethod.Name, PropertyType.FullName)
                                          : string.Empty);
                    }
                }
            }
        }
        internal void GenerateMethodCode(StringBuilder sb)
        {
            sb.AppendLine("// Generating method code for " + TargetMethodInfo.Name);

            var validListMapping         = WrapperRepository.GetValidMapping(TargetMethodInfo.ReturnType);
            var returnValueIsWrappedList = validListMapping != null;

            string returnTypeName         = null;
            bool   returnTypeIsWrapped    = false;
            Type   listElementType        = null;
            bool   listElementsAreWrapped = false;

            if (returnValueIsWrappedList)
            {
                listElementType = Repository.GetMethodReturnListType(TargetMethodInfo);

                if (listElementType == null)
                {
                    listElementType = typeof(object);
                }

                listElementsAreWrapped =
                    Repository.TypeIsWrapped(listElementType);

                var genericParameter =
                    listElementsAreWrapped
                        ? Repository.GetTargetFullName(listElementType)
                        : listElementType.FullName;

                returnTypeName = string.Format("{0}<{1}>", validListMapping.ToTypeName, genericParameter);
            }
            else
            {
                returnTypeIsWrapped = Repository.TypeIsWrapped(TargetMethodInfo.ReturnType);

                returnTypeName = returnTypeIsWrapped
                    ? Repository.GetTargetFullName(TargetMethodInfo.ReturnType)
                    : TargetMethodInfo.ReturnType.FullName;

                if (!returnTypeIsWrapped)
                {
                    string nativeTypeName = Repository.TranslateToNativeTypeName(TargetMethodInfo.ReturnType);

                    if (!string.IsNullOrEmpty(nativeTypeName))
                    {
                        returnTypeName = nativeTypeName;
                    }
                }
                else
                {
                    Repository.MarkUsageOfType(TargetMethodInfo.ReturnType);
                }
            }

            // Change from camelCase to UpperCamelCase

            string origName        = TargetMethodInfo.Name;
            string transformedName = origName.Substring(0, 1).ToUpper() + origName.Substring(1);

            // Check for condition where a method name has same name as property
            if (transformedName == ClassInfo.TargetNameWithoutNamespace)
            {
                if (transformedName == "Cache")
                {
                    transformedName = "AddToCache";
                }
            }

            // No "public" prefix on interface definition
            var publicStr = ClassInfo.IsInterface ? "" : "public ";

            var virtualStr = (!ClassInfo.IsInterface && TargetMethodInfo.IsVirtual) ? "virtual " : string.Empty;

            sb.AppendFormat("      {0}{1}{2} {3}(", publicStr, virtualStr, returnTypeName, transformedName);

            bool firstParameter = true;

            var parameters =
                TargetMethodInfo.GetParameters().Select(pi => (new WrapperParameterInfo(this, pi))).ToArray();

            foreach (var mp in parameters)
            {
                if (Repository.TypeIsWrapped(mp.ParameterType))
                {
                    Repository.MarkUsageOfType(mp.ParameterType);
                }
            }

            foreach (var mp in parameters)
            {
                if (!firstParameter)
                {
                    sb.Append(", ");
                }
                firstParameter = false;

                var parameterTypeName = mp.ParameterTypeName.Replace('+', '.');
                sb.AppendFormat("{0} {1}", parameterTypeName, mp.ParameterName);
            }
            sb.Append(")");

            if (ClassInfo.IsInterface)
            {
                sb.Append(";\r\n");
            }
            else
            {
                sb.Append("\r\n      {\r\n");

                // Generate function call
                var functionCallSb = new StringBuilder();

                functionCallSb.AppendFormat("WObj.{0}(", TargetMethodInfo.Name);

                firstParameter = true;
                foreach (var mp in parameters)
                {
                    if (!firstParameter)
                    {
                        functionCallSb.Append(", ");
                    }
                    firstParameter = false;

                    if (mp.IsWrapped)
                    {
                        functionCallSb.AppendFormat(
                            "({0}){1}.WrappedObject", mp.ParameterType.FullName, mp.ParameterName);
                    }
                    else
                    {
                        functionCallSb.Append(mp.ParameterName);
                    }
                }

                functionCallSb.Append(")");

                if (TargetMethodInfo.ReturnType != typeof(void))
                {
                    if (returnValueIsWrappedList)
                    {
                        const string template = @"
         return new {0}<{1}>({2});
";
                        sb.AppendFormat(
                            template,
                            listElementsAreWrapped
                                ? validListMapping.FullWrapperName
                                : validListMapping.ShallowWrapperName,
                            listElementsAreWrapped
                                ? Repository.GetTargetFullName(listElementType)
                                : listElementType.FullName,
                            functionCallSb);
                    }
                    else
                    {
                        if (returnTypeIsWrapped)
                        {
                            sb.AppendFormat(
                                "         return ObjectWrapper.CreateWrapper<{0}>({1});\r\n",
                                returnTypeName,
                                functionCallSb);
                        }
                        else
                        {
                            sb.AppendFormat("         return {0};\r\n", functionCallSb);
                        }
                    }
                }
                else
                {
                    sb.AppendFormat("         {0};\r\n", functionCallSb);
                }

                sb.Append("      }\r\n\r\n");
            }
        }