简体   繁体   中英

c# combine multiple types into one

I'm facing a problem that I need to construct one object from multiple types and return to the front end, here is what I want(In C#).

The shape that front end wants is like this

{
...
"props" : // this is the place I need to fill-up.
...
}

For the controller, it's a custom response.

        public IActionResult Index()
        {
            return JohnDoe.Render();
        }

Behind the scenes, Render is going to get some data from two places.

        public object Foo()
        {
            return string, int, IEnumerable, instance, etc;
        }

        public object Bar()
        {
            return string, int, IEnumerable, instance, etc;
        }

I know the return statement is not valid, what I mean is that those are all the possibilities.

And here is eventually what I want.

        public object Combine()
        {
            var foo = Foo();
            var bar = Bar();

            return foo + bar;
        }

Again the return statement is not valid, I want to have a final object that contains both of them. It can be Dictionary<string, object> or anonymous object new {} , or something else does not matter as long as one object has both of them.

Obviously what I have here is not possible to achieve it. Here are the two options I came up with.

  1. Just use a wrapper to wrap them, Here is what I do.
        public object Combine()
        {
            var foo = Foo();
            var bar = Bar();

            return new { foo, bar };
        }

I know I can have a Type rather than an anonymous object, the thing is that this will introduce two keys foo and bar , which I do not want to if possible.

  1. Make the foo and bar only return instance . NO string , int , Array , IEnumerable etc. If I do it in this way, things get a little bit easier. All I need to do is looping through the properties getting the values and map to either new {} or Dictionary<string, object> . This way I do not need to introduce new keys.

Update : so basically I want to avoid introducing new keys if I can, as I just want to return the original without a wrapper. That's why I came up with option 2, only instance is allowed. Let me know your thoughts, please.

How about option 3)

  • create a class that acts as a wrapper for Foo and Bar called FooBar ?
public class FooBar
{
    public Foo Foo { get; set; }
    public Bar Bar { get; set; }
}

Why?

C# is an object-oriented programming language. And it's based on the concept of wrapping pieces of data, and behavior related to that data, into special bundles, called objects, which you construct from a 'blueprint' called a class.

You can use a Tuple :

    public Tuple<Foo, Bar> Combine()
    {
        var foo = Foo();
        var bar = Bar();

        return Tuple.Create(foo, bar);
    }

Or a ValueTuple :

    public (Foo, Bar) Combine()
    {
        var foo = Foo();
        var bar = Bar();

        return (foo, bar);
    }

Also with named fields:

    public (Foo foo, Bar bar) Combine()
    {
        var foo = Foo();
        var bar = Bar();

        return (foo, bar);
    }

Or, as Dennis1679 suggest, make a custom type.

I think this is what you are asking

public sealed class FooBar : Foo, Bar
{

}

Unfortunately this is not possible in C#. But in essence this is what you are doing. You can do the following:

public sealed class FooBar: IFoo, IBar
{
    public FooBar(IFoo foo, IBar bar)
    {
        ///Assign its members
    }

    ///Implement its members
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM