예제 #1
0
        public string CallBaseConstructorIfAny(MethodDefinition constructor, TypeReference declaringType = null)
        {
            MethodDefinition baseConstructor;

            if (declaringType == null)
            {
                declaringType = constructor.DeclaringType;
            }
            if (TryGetBaseConstructorMatchingBestThisContructor(constructor, out baseConstructor))
            {
                if (!baseConstructor.HasParameters)
                {
                    return("");
                }
                else
                {
                    string res = " : base(";
                    bool   isFirstParameter = true;
                    ParameterDefinition matchingParameter;
                    foreach (ParameterDefinition param in baseConstructor.Parameters)
                    {
                        matchingParameter = null;
                        if (!isFirstParameter)
                        {
                            res += ", ";
                        }
                        else
                        {
                            isFirstParameter = false;
                        }
                        if (AnalysisUtils.TryGetParameterMatchingVariableInMethodArguments(param.Name, param.ParameterType, constructor.Parameters, out matchingParameter))
                        {
                            res += "@" + matchingParameter.Name;
                        }
                        else
                        {
                            if (param.ParameterType.IsGenericParameter)
                            {
                                if (constructor.DeclaringType.BaseType.IsGenericInstance)
                                {
                                    int genericParameterIndex = -1;
                                    if (baseConstructor.DeclaringType.HasGenericParameters)
                                    {
                                        int index = 0;
                                        foreach (GenericParameter gp in baseConstructor.DeclaringType.GenericParameters)
                                        {
                                            if (gp.Name == param.ParameterType.Name)
                                            {
                                                genericParameterIndex = index;
                                                break;
                                            }
                                            index++;
                                        }
                                    }
                                    if (genericParameterIndex > -1)
                                    {
                                        res += GenerateDefaultValue(((GenericInstanceType)constructor.DeclaringType.BaseType).GenericArguments[genericParameterIndex]);
                                    }
                                }
                            }
                            else
                            {
                                res += GenerateDefaultValue(param.ParameterType);
                            }
                        }
                    }
                    res += ")";
                    return(res);
                }
            }
            else
            {
                return("");
            }
        }