Up casting - c#

前端 未结 6 1015
星月不相逢
星月不相逢 2021-01-17 15:57
public class a{
  public string x1 {get;set;}
  public string x2 {get;set;}
  public string x3 {get;set;}
}

public class b:a{
}

Obviously v

6条回答
  •  一整个雨季
    2021-01-17 16:34

    This type of cast is wrong, because you can't cast parents to their children.
    Type of a doesn't know about metainformation of type b. So you need provide the explicit cast operator to do such things, but in this case you must remove inheritance.
    Other option is to define some interface, such as in other questions.

    More information:
    http://msdn.microsoft.com/en-us/library/ms173105.aspx
    http://msdn.microsoft.com/en-us/library/85w54y0a.aspx

提交回复
热议问题