Esempio n. 1
0
        /// <summary>
        /// Try to generate a value for a given type that is not null (by calling the constructor).
        /// It returns null only if we can't find a constructor to call or a know value to return
        /// (like 0 for an int, string.Empty for a string ...).
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public string GenerateDefaultValue(TypeReference type, bool isRecursiveCall = false)
        {
            MethodDefinition constructorIfAny;

            if (TryGetConstructor(type, out constructorIfAny))
            {
                string res          = "new " + AnalysisUtils.GetFullTypeName(type, Configuration.TypesThatNeedToBeRenamed, Configuration.TypesThatNeedFullName, _outputOptions.OutputFullTypeName) + "(";
                bool   isFirstParam = true;
                foreach (ParameterDefinition param in constructorIfAny.Parameters)
                {
                    if (!isFirstParam)
                    {
                        res += ", ";
                    }
                    else
                    {
                        isFirstParam = false;
                    }
                    res += GenerateDefaultValue(param.ParameterType, true);
                }
                res += ")";
                return(res);
            }
            else
            {
                return(AnalysisUtils.GetSafeTypeReferenceDefaultValueAsString(type, _outputOptions.OutputFullTypeName));
            }
        }