简单的问题。为什么表达式typeof(this)不是有效的 C#?还是表达式sizeof(this)?当我尝试运行一个简短的测试程序时,它会显示 Type Expected,然后抛出一堆其他语法错误。我的意思是,typeof是一个方法(或者你传递参数的关键字,不确定),它是相同的sizeof,那么为什么它不是this一个有效的参数呢?this我的意思是,你可以用(例如)调用很多方法,那么MyMethod(this)有什么特别之处呢?typeofsizeof
这在为诸如DependencyProperty. 例如,查看propa片段。它必须typeof(ownerclass)在那里使用。如果可以typeof(this)代替的话会好很多。
测试程序:
using System;
public struct TestingClass
{
public static void Main()
{
TestingClass tc = new TestingClass();
tc.Test();
Console.ReadKey(true);
}
public void Test()
{
Console.WriteLine(typeof(this)); //error here
Console.WriteLine(sizeof(this)); //and here too
}
}