在下面的课程中,我收到一个编译错误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();
}
}
Run Code Online (Sandbox Code Playgroud)
该typeof运算符在编译时直接处理类型。例如typeof(DateTime)。如果您想要对象的类型,请使用该GetType()方法。它派生自Object,因此它可用于任何对象。
var hello = "Hello, world!";
var type = hello.GetType();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1362 次 |
| 最近记录: |