简体   繁体   中英

c# extract base class properties only based on derived class and return new base class object

I have 2 classes StaggingAttorney and Attorney . I will use the StaggingAttorney to collect information about an attorney and once I have all the information I will use it to create an Attorney profile using the best results. The 2 classes look like this;

private class StaggingAttorney : CourtCase.Attorney
{
    public bool scraping = false;

    public bool scraped = false;

    public string caseNumber;

    public CourtCase.Attorney toAttorney()
    {
        CourtCase.Attorney attorney = new CourtCase.Attorney();
        return attorney;
    }

}

...and...

public class Attorney
{
    public string names;
    public string matchString;
    ...    
    public List<Identity> IdentityMatches = new List<Identity>();

    public List<Identity> getIdentityMatches()
    {
        return IdentityMatches;
    }
    public class Identity
    {
        public string names;
        public string barNumber;
        public string email;

        public string phoneNumber { get; internal set; }
        public object faxNumber { get; internal set; }
    }
}

I have created a method called CourtCase.Attorney toAttorney() which you can see above. In this method I want to return a new CourtCase.Attorney with all CourtCase.Attorney inherited properties in the the StaggingAttorney

As @derloopkat suggested, you can simply cast your "StaggingAttorney" instance to his parent class. ( "Attorney" in this case)

But if you really need a new instance of an "Attorney" with the same values than the parent "StaggingAttorney" just access to the parent fields of your "StaggingAttorney" object.

private class StaggingAttorney : CourtCase.Attorney
{
    public bool scraping = false;

    public bool scraped = false;

    public string caseNumber;

    public CourtCase.Attorney toAttorney()
    {
        CourtCase.Attorney attorney = new CourtCase.Attorney()
        {
            names = this.names,
            matchString = this.matchString,
            [... Initialize the other properties ...]
        };
        return attorney;
    }

}

When you create an instance of a child class you are also creating the parent. So there are no many scenarios where you need to make another new instance from a child.ToParent() method. Having a conversion method like this makes more sense when one class is not inheriting from the other.

var attorney = new StaggingAttorney() { scraped = false };
attorney.names = "John"; //During the scraping process
attorney.scraped = true;
CourtCase.Attorney court = (CourtCase.Attorney)attorney; //casting
Console.WriteLine(court.names); //returns "John"

No need to copy data, because the child inherited names from its parent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM