Up casting - c#

前端 未结 6 1061
星月不相逢
星月不相逢 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:31

    Actually, your idea of creating a new instance of b and copying a's field values to it seems like an interesting approach. using reflection to achieve this should fit the bill nicely.

    Something like: iterate through all of a's fields, get their values as stored in a and store them in the same field in b.

    Still, this is not "downcasting" -- since it means a completely different object is created -- as opposed to reinterpreting the meaning of an existing object.

提交回复
热议问题