简体   繁体   English

字符串未被识别为有效的布尔值

[英]String was not recognized as a valid boolean

I am sending the string representation of a boolean through a socket and reading it the other end. 我通过套接字发送布尔值的字符串表示,并在另一端读取它。

void Send(bool value)
{
    Socket.Send(value.ToString());
}

void Receive()
{
    string message = Socket.Receive();

    Console.WriteLine(message) // Always equal to either 'True' or 'False (without quotes)

    bool result = bool.Parse(message) // here I get the format exception.
}

but I get the following exception when I try and parse my message : 但是当我尝试解析我的message时,我得到以下异常:

String was not recognized as a valid boolean . String was not recognized as a valid boolean

The value when I get the exception is: True . 我得到异常时的值是: True With NO whitespace. 没有空格。

At first glance, I would consider it having an issue with untrimmed space..., but that's not the case, as Boolean.Parse uses TryParse and that, in turn, trims the space in one of its attempts: 乍一看,我认为它有一个未修剪空间的问题...,但事实并非如此,因为Boolean.Parse使用TryParse ,反过来,它在其中一次尝试中修剪空间:

public static Boolean Parse (String value) {
    if (value==null) throw new ArgumentNullException("value");
    Contract.EndContractBlock();
    Boolean result = false;
    if (!TryParse(value, out result)) {
        throw new FormatException(Environment.GetResourceString("Format_BadBoolean"));            
    }
    else {
        return result;
    }
}

public static Boolean TryParse (String value, out Boolean result) {
    result = false;
    if (value==null) {
        return false;
    }
    // For perf reasons, let's first see if they're equal, then do the
    // trim to get rid of white space, and check again.
    if (TrueLiteral.Equals(value, StringComparison.OrdinalIgnoreCase)) {
        result = true;
        return true;
    }
    if (FalseLiteral.Equals(value,StringComparison.OrdinalIgnoreCase)) {
        result = false;
        return true;
    }

    // Special case: Trim whitespace as well as null characters.
    value = TrimWhiteSpaceAndNull(value);

    if (TrueLiteral.Equals(value, StringComparison.OrdinalIgnoreCase)) {
        result = true;
        return true;
    }

    if (FalseLiteral.Equals(value,StringComparison.OrdinalIgnoreCase)) {
        result = false;
        return true;
    }

    return false;
}

Reference: http://referencesource.microsoft.com/#mscorlib/system/boolean.cs,e2a8f2e50ecf93c0,references 参考: http //referencesource.microsoft.com/#mscorlib/system/boolean.cs,e2a8f2e50ecf93c0,references

So, there must be something else going on. 所以,必须有其他事情发生。 Perhaps there is an issue with the format, UTF-8, ANSI, ASCII, etc. One of your requirements is that you want a bool, so that you won't have two cases for True and False , so why not do something like this: 也许格式存在问题,UTF-8,ANSI,ASCII等。你的一个要求就是你想要一个bool,这样你就不会有两个TrueFalse案例,所以为什么不做类似的事情呢?这个:

bool result = message.ToLower().Contains("true"); // true or false

EDIT: 编辑:

After reading some of the comments, it seems you're expecting cases beyond True or False , in which case the result could be invalid. 在阅读了一些评论后,您似乎期待案例超越TrueFalse ,在这种情况下结果可能无效。 I suggest something like this: 我建议像这样:

var lMessage = message.ToLower();
bool? result = lMessage.Equals("true") ? true : lMessage.Equals("false") ? false : null;

So, if the message contains True , it's true ; 所以,如果消息包含True ,那就是true ; if False , it's false ; 如果False ,这是false ; otherwise, it's null , which indicates an invalid message. 否则,它为null ,表示无效的消息。 You can then check to see if result is null and either display the invalid message or do something else. 然后,您可以检查result是否为null并显示无效消息或执行其他操作。 I'm not sure what your routine is from there on. 我不确定你的常规是什么。

Try This: 尝试这个:

void Receive()
{
    string message = Socket.Receive();

    Console.WriteLine(message) // Always equal to either 'True' or 'False (without quotes)

    bool result = message.ToLower().Equals("true");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM