【问题标题】:What is the difference between myCustomer.GetType() and typeof(Customer) in C#?C# 中的 myCustomer.GetType() 和 typeof(Customer) 有什么区别?
【发布时间】:2010-09-13 11:13:24
【问题描述】:

我已经在我维护的一些代码中看到了两者都完成了,但不知道有什么区别。有吗?

让我补充一点,myCustomer 是 Customer 的一个实例

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    在您的情况下,两者的结果完全相同。它将是您从System.Type 派生的自定义类型。这里唯一真正的区别是当你想从你的类的实例中获取类型时,你使用GetType。如果您没有实例,但您知道类型名称(并且只需要实际的 System.Type 来检查或比较),则可以使用 typeof

    重要区别

    编辑:让我补充一点,对GetType 的调用在运行时得到解决,而typeof 在编译时得到解决。

    【讨论】:

    • 我主要投票支持编辑。因为这是一个重要的区别。
    • 不一定完全相同。假设 VipCustomer 继承自 Customer,那么如果在某个时候 myCustomer = new VipCustomer();那么它的 GetType() 将不同于类型 Customer。正如您所说,运行时和编译时间是两个非常不同的东西。
    【解决方案2】:

    GetType() 用于在运行时查找对象引用的实际 类型。由于继承,这可能与引用对象的变量的类型不同。 typeof() 创建一个 Type 字面量,该字面量与指定的类型完全相同,并在编译时确定。

    【讨论】:

      【解决方案3】:

      是的,如果您从 Customer 继承类型,则会有所不同。

      class VipCustomer : Customer
      {
        .....
      }
      
      static void Main()
      {
         Customer c = new VipCustomer();
         c.GetType(); // returns typeof(VipCustomer)
      }
      

      【讨论】:

        【解决方案4】:

        首先,您需要一个实际实例(即 myCustomer),其次您不需要

        【讨论】:

          【解决方案5】:

          typeof(foo) 在编译期间被转换为常量。 foo.GetType() 发生在运行时。

          typeof(foo) 也直接转换为其类型的常量(即 foo),所以这样做会失败:

          public class foo
          {
          }
          
          public class bar : foo
          {
          }
          
          bar myBar = new bar();
          
          // Would fail, even though bar is a child of foo.
          if (myBar.getType == typeof(foo))
          
          // However this Would work
          if (myBar is foo)
          

          【讨论】:

            【解决方案6】:

            typeof 在编译时执行,而 GetType 在运行时执行。这就是这两种方法的不同之处。这就是为什么在处理类型层次结构时,只需运行 GetType 即可找到类型的确切类型名称。

            public Type WhoAreYou(Base base)
            {
               base.GetType();
            }
            

            【讨论】:

              【解决方案7】:

              typeof 运算符将类型作为参数。它在编译时解决。 GetType 方法在对象上调用并在运行时解析。 第一种是在需要使用已知Type的时候使用,第二种是在不知道对象是什么的时候获取对象的类型。

              class BaseClass
              { }
              
              class DerivedClass : BaseClass
              { }
              
              class FinalClass
              {
                  static void RevealType(BaseClass baseCla)
                  {
                      Console.WriteLine(typeof(BaseClass));  // compile time
                      Console.WriteLine(baseCla.GetType());  // run time
                  }
              
                  static void Main(string[] str)
                  {
                      RevealType(new BaseClass());
              
                      Console.ReadLine();
                  }
              }
              // *********  By Praveen Kumar Srivastava 
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-03-01
                • 2011-04-05
                • 2014-12-07
                • 2011-10-18
                • 1970-01-01
                相关资源
                最近更新 更多