C# typeof无法解析符号

C# typeof无法解析符号,c#,typeof,C#,Typeof,在下面的类中,我在typeof上得到一个编译错误 为什么编译器不能解析类型 public class TestTypeOf { public struct Teststruct { public int d1; public int d2; } Teststruct cds; public Teststruct ToString (IntPtr lParam) { var t = typeof(cd

在下面的类中,我在
typeof
上得到一个编译错误
为什么编译器不能解析类型

public class TestTypeOf
{
    public struct Teststruct
    {
        public int d1;
        public int d2;
    }
    Teststruct cds;

    public Teststruct ToString (IntPtr lParam)
    {
        var t = typeof(cds);    // cannot resolve symbol 'cds'
        cds.d1 = 1;
        cds.d2 = 2;
        return cds;
    }

    public TestTypeOf ()
    {
        cds = new Teststruct();
    }

}

typeof
操作符在编译时直接处理类型。例如
typeof(DateTime)
。如果需要对象的类型,请使用
GetType()
方法。它源于
对象
,因此在任何对象上都可用

var hello = "Hello, world!";
var type = hello.GetType();

你应该通过一个类型:好的,明白了:谢谢!为什么(无法解释的)否决票?这是违反直觉的,事实上与基于c语言的常规相反。我没有投反对票,但是你可以很容易地找到/阅读文档。@Evantimboli,谢谢你的反馈,但是文档很模糊:它应该强调它与其他人不同步。这将减少混乱。