我收到错误代码CS0162“检测到无法访问的代码” [英] I am getting a error code of CS0162 "unreachable code detected"

查看:1437
本文介绍了我收到错误代码CS0162“检测到无法访问的代码”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<预LANG = C#> <跨度类= 代码关键字> BOOL CheckSubproject(){
<跨度类= 代码预处理器>了#pragma警告禁用649 / /检测到无法访问的代码0162更改为649
如果(sub!=


return ;
string result = ;
// (字符串)是检测到的无法访问的代码



if (project == bmte){
List< fileinfo> files = SearchFiles(dataPath);
FileInfo adAssistant = files.Find(x = > x.Name == < span class =code-string> AdAssistant.cs
);
if (adAssistant!= null
result = pro;
else
result = 标准的;
}

if (结果!= ){
FileInfo fileInfo = SearchFiles(dataPath).Find(x = > x.Name = = Fixer.cs);

if (fileInfo!= null ){
StreamReader fileR = new StreamReader(fileInfo.FullName);
string code = fileR.ReadToEnd();
fileR.Close();

正则表达式re = 正则表达式( @ const\s + string\s + sub\s + \ = \s + {2} \;);
code = re.Replace(code, const string sub = \ +结果+ \;);

StreamWriter fileW = new StreamWriter(fileInfo.FullName);
fileW.WriteLine(code);
fileW.Close();

StopDownloadIssues();
AssetDatabase.Refresh();

}
}
返回 false ;
<跨度类= 代码预处理器>了#pragma警告恢复649 //可达代码检测#CS0162
}





我尝试了什么:



我尝试将字符串设置为Public,Local和static。这个字符串似乎没什么用。我有#pragma警告禁用并启用代码649,但删除代码时我得到60多个错误。与代码唯一的错误是用字符串。

解决方案
寻找代码

 BOOL CheckSubproject(){ 
#pragma warning disable 649 //检测到无法访问的代码0162更改为649
if(sub!=)
返回true;
string result =;
//(字符串)是无法访问的代码,如果编译器标记字符串,则检测到
...



result =;
as unreachable 这意味着编译器推断 sub 变量的值必须 区别于。

因此,当sub ==NEVER将使用时,执行相应的行。


引用:

错误代码CS0162无法检测到无法访问的代码



此错误消息不会告诉您出现错误看到它,它告诉你前面的代码是这样的,即无法访问的代码行永远不会被执行。

在编译时,编译器可以检测到 sub 将永远不会为空,因此将始终返回true并且永远不会执行以下代码。

这可以故意进行,用于测试,因此警告。你的pragma将警告转换为错误。



所以你可以对字符串进行任何更改,它永远不会解决问题,因为它不是问题。


        bool CheckSubproject() {
            #pragma warning disable 649 // Unreachable code detected 0162 changed to 649
            if (sub != "")


                return true;
                string result = "";
//(string) is the unreachable code that is detected
            


            if (project == "bmte") {
                            List<fileinfo> files = SearchFiles(dataPath);
                            FileInfo adAssistant = files.Find(x => x.Name == "AdAssistant.cs");
                            if (adAssistant != null)
                                result = "pro";
                            else
                                result = "standard";
                        }
                        
            if (result != "") {
                FileInfo fileInfo = SearchFiles(dataPath).Find(x => x.Name == "Fixer.cs");

                if (fileInfo != null) {
                    StreamReader fileR = new StreamReader(fileInfo.FullName);
                    string code = fileR.ReadToEnd();
                    fileR.Close();

                    Regex re = new Regex(@"const\s+string\s+sub\s+\=\s+""{2}\;");
                    code = re.Replace(code, "const string sub = \"" + result + "\";");

                    StreamWriter fileW = new StreamWriter(fileInfo.FullName);
                    fileW.WriteLine(code);
                    fileW.Close();

                    StopDownloadIssues();
                    AssetDatabase.Refresh();
                    
                }
            }
            return false;
                     #pragma warning restore 649 // Unreachable code detected  # CS0162
        }



What I have tried:

I have tried setting the string to Public, Local, and static. Nothing seems to work with this string. I have a #pragma warning disable and enable for code 649, but when removing the code I get over 60 errors. With the code the only error is with the string.

解决方案

Looking the code

bool CheckSubproject() {
#pragma warning disable 649 // Unreachable code detected 0162 changed to 649
if (sub != "")
    return true;
string result = "";
//(string) is the unreachable code that is detected
...


if the compiler marks the string result=""; as unreachable it means that the compiler deduces that sub variable has a value that is necessarily distinct that "".
So, the lines that correspond to be executed when sub=="" NEVER will used.


Quote:

error code of CS0162 "unreachable code detected"


This error message do not tell you that there is an error where you see it, it tells you that preceding code is made in such a way that the line of "unreachable code" will never be executed.
At compile time, the compiler can detect that sub will never be empty and thus will always "return true" and following code will never be executed.
This can be done on purpose, for testing, thus the warning. Your pragma transform the warning to an error.

So you can do any change on string, it will never solve the problem because it is not the problem.


这篇关于我收到错误代码CS0162“检测到无法访问的代码”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆