Convert/Cast base type to Derived type

前端 未结 5 1599
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:34

    As earlier answers have said, you need to instantiate a new instance of MyResults then copy the properties over from your Results object. You asked what a "copy constructor" was - it is just a constructor that takes an object and uses it to populate the object being constructed. For example:

        public class Results
        {
            public string SampleProperty1 { get; set; }
            public string SampleProperty2 { get; set; }
        }
    
        public class MyResults : Results
        {
            public MyResults(Results results)
            {
                SampleProperty1 = results.SampleProperty1;
                SampleProperty2 = results.SampleProperty2;
            }
        }
    

    A copy constructor is usually more convenient, readable and reusable than using code like this:

    MyResults myResults = new MyResults
    {
        SampleProperty1 = results.SampleProperty1,
        SampleProperty2 = results.SampleProperty2
    };
    

    If there are lots of properties and/or you are making lots of changes to the class you could use reflection (e.g. C# Using Reflection to copy base class properties ) or a tool such as AutoMapper ( http://automapper.codeplex.com ) to copy the properties. But often that can be overkill.

    0 讨论(0)
  • 2020-12-20 17:36
    Results results = new MyResults();
       ...
    
    return (MyResults)results;
    

    should work. If not, then the problem is somewhere alse.

    0 讨论(0)
  • 2020-12-20 17:42

    You can't. If results doesn't refer to a MyResults (e.g. if CallFrameworkMethod returns a base Results instance), then casting won't make it so: you'll need to create a new MyResults, based on the existing non-MyResults. Casting is about changing the compile-time type of the reference, not about changing the concrete type of the referenced object.

    You can use tools such as Reflection or AutoMapper to help with the initialisation of the new MyResults object -- but a new MyResults object there must be, because you cannot tell a base Results object to become a MyResults object.

    0 讨论(0)
  • 2020-12-20 17:45

    How about:

    ...
    MyResults results  = new MyResults();
    ...
    

    And you maybe also need to create a COnstructor in your MyResults class:

    public class MyResults : Results
    {
        public MyResults() : base() {}
    }
    

    What exactly means "nothing here"?

    EDIT

     results = (CallFrameworkMethod() as MyResults);
    

    It doesnt throw the exception, but if it would be useful for you - it depends on what you would like to do further...

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题