What is the best way to merge two objects during runtime using C#?

后端 未结 7 533
野的像风
野的像风 2021-02-06 10:18

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


        
7条回答
  •  故里飘歌
    2021-02-06 11:19

    As others have pointed out, there is no way to "merge" them (if you're thinking of a select * with multiple tables in SQL, for example). Your closest analog would be taking the route that Zxpro has provided and "group" them in a generic class.

    What, exactly, would you want to accomplish with "merging" them, though? Declaring the properties explicitly would have the biggest convenience effect on writing code and compile-time safety, but if you can't specify a type then there's no opportunity for that. If you're just looking for a generic "property bag" container, then an existing data structure, such as a Dictionary or Hashtable should be able to handle that.

提交回复
热议问题