Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9210635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:07:27+00:00 2026-06-18T01:07:27+00:00

Have a generic class that inherits from a non-generic class like the structure below:

  • 0

Have a generic class that inherits from a non-generic class like the structure below:

public class Result
{
     public string ErrorCode { get; set;}
     public string ErrorMessage { get; set;}
     public boo Success { get; set;}
     //Lots more properties

     public ClientResult ToClientResult()
     {
         //some pretty involved calculations of error coded and status
     }
 }

 public class Result<T> : Result
 {
     public T details {get; set;}

     public ClientResult<T> ToClientResult<T>()
     {
        //Need to call the parent class implementation and convert result to  generic ver  
     }
 }

My question is how can i call the parent ToClientResult() and convert the result to the generic version of ClientResult<T> and then i need to set a property of the ClientResult<T> to the details property of the Result<T> class.

I am sure i am missing an easy solution here, i really dont want to duplicate the parent class logic as it is pretty complicated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:07:28+00:00Added an answer on June 18, 2026 at 1:07 am

    You can’t cast an object of the parent type to a child type if it’s created as the parent type (using new ClientResult()). It just doesn’t work that way.

    What you could do is factor the complicated code out to another method which you use to do the heavy lifting in both the Result class and the Result<T> class:

    public class Result
    {
        public string ErrorCode { get; set;}
        public string ErrorMessage { get; set;}
        public boo Success { get; set;}
        //Lots more properties
    
         public ClientResult ToClientResult()
         {
             var clientResult = new ClientResult();
             SetupClientResult(clientResult);
             return clientResult;
         }
    
         protected void SetupClientResult(ClientResult clientResult) 
         {    
             //some pretty involved calculations of error coded and status           
         }
    
    }
    
    public class Result<T> : Result
    {
         public T details {get; set;}
    
         // This now shadows the original ToClientResult method. The trap here is that if
         // you are treating your Result<T> instance as a Result, this method will not be 
         // called, and the return type will be ClientResult and not ClientResult<T>.
         // See: http://stackoverflow.com/questions/392721/difference-between-shadowing-and-overriding-in-c?lq=1
         public ClientResult<T> ToClientResult()
         {
             var clientResult = new ClientResult<T>();
             SetupClientResult(clientResult);
    
             clientResult.SomeProperty = details;
    
             return clientResult;
         }
    }
    

    This is all assuming ClientResult<T> derives from ClientResult, which is a little hard to tell from your question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a repository class that inherits from a generic implementation: public namespace RepositoryImplementation
I have a custom list which inherits from Generic.List<T> like this: public class TransferFileList<T>
I have a class that inherits from a generic dictionary as follows: Class myClass
I have generic type that looks like: public class GenericClass<T, U> where T :
If I have a base class that inherits from a generic list, and the
If I have a base class that inherits from a generic list, and the
I have an interesting issue where a class inherits from a class that implements
I have an ECMContext class that inherits from DbContext. Within ECMContext there is a
if i have a .net 1.1 class that inherits from DictionaryBase and i am
I have a generic class that needs to limit an enum depending on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.