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 6346189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:58:14+00:00 2026-05-24T20:58:14+00:00

I have a base class: class MyBase { public int Id { get; set;

  • 0

I have a base class:

class MyBase
{
    public int Id { get; set; }
    public string CreatedBy { get; set; }
    public DateTime CreateAt { get; set; }
}

There is a subclass with readonly properties inherited from the base class:

class MySub : MyBase
{
    public string CreateAtStr{
        get { return CreateAt.ToString("yyyy-MMM-dd", CultureInfo.InvariantCulture); }
    }
}

I can only get results of MyBase type, I want them to be auto converted to MySub type.
How to do it? BTW: base class is entity class and sub class is for the view so it needs some customization.

  • 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-05-24T20:58:16+00:00Added an answer on May 24, 2026 at 8:58 pm

    You cannot convert a base class to a derived class, OOP doesn’t cover this case (simply because a base class instance is not a derived class instance).

    You could on the other hand create new instances of MySub and then use a mapping tool like AutoMapper (or perform it manually) to copy the values of all the base properties.

    I.e manual copying:

    List<MyBase> items = ...
    List<MySub> subs = items.Select( x=> new MySub() 
                             { 
                               Id = x.Id, 
                               CreatedBy = x.CreatedBy, 
                               CreatedAt = x.CreatedAt
                             })
                            .ToList();
    

    Edit:

    In your specific case (using only existing public properties of the base class), and if you do not mind using a method instead of a property an alternative could be an extension method:

    public static class FooExtensions
    {
        public static string CreateAtStr(this MyBase myBase)
        {
           return myBase.CreateAt.ToString("yyyy-MMM-dd", CultureInfo.InvariantCulture); 
        }
    }
    

    Now you do not need a derived class at all but can simply call CreateAtStr() on instances of type MyBase:

    MyBase myBase = new MyBase();
    string createdAt = myBase.CreateAtStr();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class with a property which (the get method) I want
I have two classes: public class MyBase { public virtual void DoMe() { }
Let's say I have a base class template MyBase: template <class T> class MyBase{
I have a hierarchy that looks something like this: class Base { public: void
I have an overridable sub in my base class Project1: Public Class BaseClass Protected
I have a set a variable in my Base Adapter class, now I want
I have a base class MyBase that contains a pure virtual function: void PrintStartMessage()
I have the following code: class MyBase { public virtual void foo() { ...
In my base class I have a generic method (ideally this would be a
I have base class BaseClass and derived classes DerivedA , DerivedB , and DerivedC

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.