Convert/Cast base type to Derived type

前端 未结 5 1598
Happy的楠姐
Happy的楠姐 2020-12-20 17:12

I am extending the existing .NET framework class by deriving it. How do I convert an object of base type to derived type?

public class Results { //Framework          


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-20 17:51

    No, you can't avoid copying the content into a instance of the derived type. Well, if you can change CallFrameworkMethod to be a generic method, and MyResults has a zero-argument constructor, then CallFrameworkMethod could create a new instance of your derived type directly, then use only the members of the parent type.

    But probably you'll have to end up copying to a new object. Remember that this copying code can certainly be reused in another method, you don't have to rewrite it everywhere you need it.

提交回复
热议问题