Why can't you use GetType when casting?

自古美人都是妖i 提交于 2019-12-11 14:12:13

问题


I asked a still unanswered question that will shed more light on this question.

Why can't I do this...

_wizardDialog.UIRoot.Controls.Clear()
_wizardDialog.UIRoot.Controls.Add(TryCast(wizardUserControl, wizardUserControl.GetType))

Why does using GetType in this way fail. The argument for try cast are object and type. Since wizardUserControl.GetType returns a type how come this is not legal. Visual Studio is complaining wizardUserControl.GetType is not defined.

The bottom line is how can I get WizardUserControl to return the type that is being passed in to my method. The method that is being called into here should not have to have the type hard-coded...that's the point of all of this OOP stuff...right? So how do you do this.

Please read the other question and answer there if you can...that is the problem I am tyring to solve.

I am learning the oop stuff.

Seth


回答1:


Think about it this way::

  • GetType() is a function. It returns an Object.

    • You do not know what the type is, or you wouldn't need to ask :)
  • When you are casting an object, you are telling the COMPILER what it's type is.

You can't both ask a type what it is, which happens at runtime, and tell the compiler what it is at compile time, in the same place.

I guess you can also think of something like GetType(String) as a keyword. It is clearer in this example:

  // Makes sense, we tell the compiler what the object is.  We could still get a 
  // *runtime* exception, if we were lying to the compiler.
  Dim car = CType(vehicle, Car); 

  // this doesn't make sense, since we don't know what is in "anObj"
  Dim anObj As Object = "(I don't know what it is, thats why it's an object)"
  Dim car = ctype(anObj, anObj.GetType() )

  // and this is the clearest, in vb.  you can see the type is being used kinda like
  // a keyword.  it won't change, but a call to GetType could
  If TypeOf anObj is Car Then
   ...

In the second case, you don't know what type "anObj" is. Since




回答2:


GetType() does not return a type. It returns an instance of class Type, which describes a type at runtime. However, an instance of Type is not substitutable where a compile-time type reference is expected (such as TryCast). They're just different things.

Think about it this way. TryCast operator has a definite compile-time result type. If you use a Type object obtained from elsewhere (and it could be a conditional with Random, so there's no way to predict the result at compile-time in general), then what should be the compile-time result type of TryCast?




回答3:


There's absolutely nothing wrong with "hard-coding" a type in your code. The general idea of OOP programming (specifically the idea of "programming to the interface") in this regard is that you specify the class that most generically describes what you need to do. For example, if I'm writing code where I'm adding instances of a UserControl to a form where all of those instances inherit from a base class and I need functionality from that base class (your WizardUserControlBase class) then that's the type that I would use the WizardUserControlBase class to refer to my controls.

As far as this question goes, OOP is not about not having types "hard-coded" (at some level it HAS to be hard-coded, even if it's just Object, since that's still a class). Disregarding that, I'm not quite sure what you're trying to accomplish by (as it looks like you're trying to) casting it to its own type. You should just be able to call _wizardDialog.UIRoot.Controls.Add(wizardUserControl)



来源:https://stackoverflow.com/questions/1190877/why-cant-you-use-gettype-when-casting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!