开发者

Trace my mistake in this Code Snippet in C#?

I just want to use the switch statement instead of the commented lines below. If the default value is an integer.

So here my code is not working please identify my mistake.

ParameterInfo[] pif = m.GetParameters();
foreach (ParameterInfo p in pif) {
    string ParamType = p.ParameterType.ToString();
    string ConvPType = ConvertToShortForm(ParamType);
    if (p.IsOut)
        ConvPType = ConvPType.Replace("ref", "out");
    strMethodName += 开发者_JAVA百科ConvPType;
    strMethodName += " ";
    strMethodName += p.Name;

    if (p.IsOptional) {
        var optional_value = p.DefaultValue;
        switch (optional_value) {
            case "":
                strMethodName += @"""" + @"""";
                break;
            case null:
                strMethodName = strMethodName + "=" + "null";
                break;
            case "False":
                strMethodName += " = " + p.DefaultValue.ToString().ToLower();
                break;
            default: strMethodName += ", ";
                break;
        }
    //...
    }
//...
}

The commented lines:

//if (p.DefaultValue != null)
//    strMethodName += " = " + p.DefaultValue.ToString().ToLower();
//if (p.DefaultValue == null)
//    strMethodName = strMethodName + "=" + "null";
//if (strMethodName.EndsWith("= "))
//    strMethodName += @"""" + @"""";


You should use p.DefaultValue.ToString() in order to use optional_value as the expression in switch statement.

MSDN says that expression in switch statement should be of integral type or of string type. You passed expression of type object. There is your mistake.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜