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

  • Home
  • SEARCH
  • 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 344973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:02:25+00:00 2026-05-12T11:02:25+00:00

I have two objects and I want to merge them: public class Foo {

  • 0

I have two objects and I want to merge them:

public class Foo
{
    public string Name { get; set; }
}

public class Bar
{
    public Guid Id { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }
    public string Property4 { get; set; }
}

To create:

public class FooBar
{
    public string Name { get; set; }
    public Guid Id { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }
    public string Property4 { get; set; }
}

I will only know the structure of Foo at runtime. Bar can be any type at runtime. I would like to have a method that will be given a type and it combine that type with Foo. For instance, the scenario above, the method was given a Bar type at runtime and I combined it with Foo.

What would be the best way to do this? Can it be done using LINQ Expressions or do I have to generate it Dynamically or is there another way? I am still learning the new LINQ namespace in C# 3.0, so excuse the ignorance if it can’t be done using LINQ Expressions. This is also the first time I have ever had to do something dynamic like this with C#, so I am not quite sure of all the options I have available to me.

Thanks for any options given.

EDIT


This is strictly for adding meta information to the type given to me for serialization. This scenario keeps the user’s objects ignorant of the meta information that needs to be added, before it is serialized. I have come up with two options before asking this question and I just wanted to see if there was anymore, before deciding on which one to use.

The two options I have come up with are:

Manipulating the serialized string of the type given to me after serializing it, by adding the meta information.

Wrapping the type given to me, which is similar to what @Zxpro mentioned, but mine differed slightly, which is fine. It will just make the user of my API have to follow the convention, which isn’t a bad thing, since everybody is about convention over configuration:

public class Foo<T>
{
    public string Name { get; set; }
    public T Content { get; set; }
}

EDIT


Thanks everybody for their answers. I decided on wrapping the object like above and I gave the answer to @Zxpro, since a majority liked that approach also.

If anybody else comes across this question, feel free to post, if you think there might be a better way.

  • 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-12T11:02:25+00:00Added an answer on May 12, 2026 at 11:02 am

    If you don’t mind them being grouped rather than merged:

    public class FooEx<T>
    {
        public Foo Foo { get; set; }
        public T Ex { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 175k
  • Answers 175k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The preprocessor runs on the .RC file as well. Define… May 12, 2026 at 3:08 pm
  • Editorial Team
    Editorial Team added an answer Maybe you can send the message via the network (http://growl.info/documentation/developer/protocol.php)… May 12, 2026 at 3:08 pm
  • Editorial Team
    Editorial Team added an answer Your instincts are spot on. The DAL should not care… May 12, 2026 at 3:08 pm

Related Questions

I'm looking for a built-in method that combines two associative arrays or objects into
I have two classes that I would like to merge into a composite. These
Here's my situation: I have 2 person objects, person1 and person2, which were created
Simple idea: I have two images that I want to merge, one is 500x500

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.