什么是用C#在运行时合并两个对象的最佳方式? [英] What is the best way to merge two objects during runtime using C#?

查看:149
本文介绍了什么是用C#在运行时合并两个对象的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个对象,我想将它们合并:

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; }
}

要创建:

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.

什么是做到这一点的最好方法是什么?它可以使用LINQ表达式做或做我必须动态地生成它,或有另一种方式?我仍然在学习C#3.0中新的LINQ的命名空间,所以原谅无知,如果它不能使用LINQ表达式来完成。这也是我第一次曾经有过用C#做一些动态的这个样子,所以我不太清楚我的一切提供给我的选项。

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.

修改


这是严格的加元信息给我进行序列化的类型。这种情况下保持了用户的对象一无所知,需要以复加,这是连载前的元信息。我想出了两个选项问这个问题之前,我只是想看看是否有下去了,决定使用哪一个了。

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.

两个选项我有想出是:

处理序列化后给我的类型的序列化的字符串,加入元信息

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

包装纸给我的类型,它是类似于@Zxpro提及,但我的略有不同,这是罚款。它只是让我的API的用户必须遵循约定,这不是一件坏事,因为大家就要约定优于配置:

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; }
}

修改


谢谢大家对他们的答案。我决定在包装对象像上面我给的答案@Zxpro,因为大多数喜欢这种做法也。

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.

推荐答案

如果你不介意他们进行分组,而不是合并的:

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; }
}

这篇关于什么是用C#在运行时合并两个对象的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆