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.