I have two objects, and I want to combine 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 know the structure of Foo at runtime. A bar can be of any type at runtime. I would like to have a method that will be given a type, and it combines this type with Foo. For example, in the above script, the method was set to Bar at run time, and I combined it with Foo.
What would be the best way to do this? Can this be done using LINQ Expressions or do I need to generate it dynamically or is there another way? I'm still exploring the new LINQ namespace in C # 3.0, so sorry ignorance if this cannot be done with LINQ Expressions. This is also the first time I've ever had to do something like this with C #, so I'm not completely sure of all the options available to me.
Thanks for any options provided.
EDIT
This is strictly for adding meta information to the type provided to me for serialization. This scenario prevents user objects from knowing the meta-information that must be added before it is serialized. I asked two options before asking this question, and I just wanted to know if there were any more before deciding which one to use.
Here are two options I came up with:
Manipulating a serialized string of the type given to me after it was serialized by adding meta-information.
Wrapping the type given to me, which is similar to what @Zxpro mentioned, but mine was slightly different, which is good. This will simply force the user of my API to follow the standard, which is not so bad as everyone is concerned with the configuration configuration:
public class Foo<T> { public string Name { get; set; } public T Content { get; set; } }
EDIT
Thanks to everyone for their answers. I decided to wrap the object as described above, and I answered @Zxpro since most liked this approach.
If anyone else comes across this question, feel free to post messages if you think there might be a better way.