可以找到类型或命名空间 - 即使我声明了变量

l--*_*''' -1 .net c#

我收到此错误:

找不到类型或命名空间名称'myObject'

在这条线上:

if (typeof(myObject) != typeof(String))
Run Code Online (Sandbox Code Playgroud)

这是周围的代码:

 for (int rCnt = 1; rCnt <= EmailList.Rows.Count; rCnt++)
            {
                object myObject = (EmailList.Cells[rCnt, 1] as Excel.Range).Value2;
                if (typeof(myObject) != typeof(String))
                    continue;
                cell = (string)(EmailList.Cells[ rCnt,1] as Excel.Range).Value2;
                if (cell!=null)
                    emails.Add(cell.ToString());
            }
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我显然是在声明myObject.非常感谢你的指导.

Bol*_*ock 7

typeof操作者需要一个类型标识符,而不是一个实例标识符,作为参数.

您想myObject.GetType()获取对象的类型:

if (myObject.GetType() != typeof(String))
Run Code Online (Sandbox Code Playgroud)

甚至可以使用is运算符:

if (!(myObject is String))
Run Code Online (Sandbox Code Playgroud)