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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:23:40+00:00 2026-05-16T16:23:40+00:00

First thing to note – I KNOW DELEGATION AND DECORATOR PATTERNS! Second – I

  • 0

First thing to note – I KNOW DELEGATION AND DECORATOR PATTERNS!

Second – I am using C# .NET 4.0, so if you come up with a solution that is specific for it, that’s fine. But if solution will work for any OOP language and platform, that would be great.

And here the question goes…

I have a partial class (lets name it Class1), which I cannot modify. Thus, I can just extend it or/and inherit from it. This class provides a perfect data model for me, the only thing I need is to add some attributes to its properties (for validation, defining label text value in MVC etc – for now, I do not need answers like ‘you can do what you need without attributes’, that’s not the matter of my question).

It is not a problem to use another class as a data model, so I can, say, create Class2 : Class1 and use Class2 as a model. Properties that need attributes would be defined as public new <type> <propertyname>. This will limit me to rewriting only the properties that need attributes, leaving all other untouched.

The smaller problem is that I do not what to redefine getters and setters for the properties, as all they gonna contain is return base.<propertyname> and base.<propertyname> = value, and if there are lots of such properties, this means lots of “stupid” coding. Is there a way to avoid this?

The bigger problem is that I have to parametrize my Class2 with Class1 instance and make something like class2.<propertyname> = class1.<propertyname> for each single property I have – too much of “stupid” coding. I can avoid it using reflection – find all properties with public getters and setters in Class1 and call prop.SetValue(child, prop.GetValue(parent, null), null); in the loop. This provides a generic function for simple cases, which is quite fine, as I mostly have simple models – lots of properties with public getters and setters without body and another logic. But I want more generic solution, and I do not like reflection. Any ideas?

Here goes the full code of the extension method that creates Class2 basing on Class1

    public static Child ToExtendedChild<Parent, Child>(this Parent parent)
        where Child : Parent, new()
    {
        Child child = new Child();

        var props = typeof(Parent).GetProperties().Where(p => p.GetAccessors().Count() >= 2);

        foreach (var prop in props)
        {
            prop.SetValue(child, prop.GetValue(parent, null), null);
        }

        return child;
    }

(by the way, this method may not ideally implement my solution, so any corrections would also be appreciated)

Thanks in advance!

  • 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-16T16:23:41+00:00Added an answer on May 16, 2026 at 4:23 pm

    The smaller problem doesn’t seem to be much of a problem. Maybe I’m misunderstanding the question, but assuming you’re simply deriving a subclass, there should be no reason to redefine either the properties or their associated getters/setters.

    The bigger problem might be resolved using something a little simpler. Using reflection for a lot of your object initialization seems a little expensive. If you’re dealing with a class that is primarily a big bag or properties, maybe you should as if you need access to all of those properties in any given situation. You mention MVC and validation, is the entire model being used in the controller method you’re validation is taking place in? If not, why not look at using a viewmodel that only exposes those pieces you need in that method?

    Your reflection initializer is interesting, but if you’re going to be doing a lot of this then you might consider investing a little time with Automapper. Otherwise maybe consider moving away from a generic solution to something that just tackles the problem at hand, i.e. mapping properties from an instance of an object to another instance of a derived object. Maybe you can create a copy constructor in the parent class and use that in your derived class?

    public class Foo {
        public string PropOne { get; set; }
        public string PropTwo { get; set; }
    
        public Foo(string propOne, string propTwo) {
            PropOne = propOne;
            PropTwo = propTwo;
        }
    
        public Foo(Foo foo) {
            PropOne = foo.PropOne;
            PropTwo = foo.PropTwo;
        }
    }
    
    public class Pho : Foo {
        // if you have additional properties then handle them here
        // and let the base class take care of the rest.
        public string PropThree { get; set; }
        public Pho(string propOne, string propTwo, string propThree) 
            : base(propOne, propTwo) {
            PropThree = propThree; 
        }
        public Pho(Pho pho) : base(pho) {
            PropThree = pho.PropThree;
        }
        // otherwise you can just rely on a copy constructor
        // to handle the initialization.
        public Pho(Foo foo) : base(foo) {}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NOTE: I am not set on using VI, it is just the first thing
First thing i want to say that it's not an easy question to explain,
The first thing that comes to my mind is to do a bunch of
To begin with, I don't know the first thing about Python ... so I
The first thing i would like to say is that this is not exactly
First thing is I am on Ubuntu 12.04 LTS. When I type rails s
I was just reading this line: The first thing the format() method does is
I'm just taking my first steps with Azure and the first thing I see
I have some jQuery/JS below. The first thing to run is the alert box
I've just started learning MATLAB, and the first thing i tried is to plot

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.