简体   繁体   English

c# 多种类型合二为一

[英]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#).我面临一个问题,我需要从多种类型构造一个 object 并返回到前端,这就是我想要的(在 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.对于 controller,这是自定义响应。

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

Behind the scenes, Render is going to get some data from two places.在幕后, Render将从两个地方获取一些数据。

        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.我知道return语句无效,我的意思是这些都是可能的。

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. return语句再次无效,我想要一个包含它们的最终object 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.它可以是Dictionary<string, object>或匿名 object new {} ,或者只要一个 object 两者兼有,其他东西都可以。

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.我知道我可以有一个Type而不是匿名的 object,问题是这将引入两个键foobar ,如果可能的话我不想这样做。

  1. Make the foo and bar only return instance .使foobar只返回instance NO string , int , Array , IEnumerable etc. If I do it in this way, things get a little bit easier.没有stringintArrayIEnumerable等。如果我这样做,事情会变得容易一些。 All I need to do is looping through the properties getting the values and map to either new {} or Dictionary<string, object> .我需要做的就是遍历获取valuesproperties和 map 到new {}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.这就是为什么我想出了选项 2,只允许instance Let me know your thoughts, please.请让我知道你的想法。

How about option 3)选项3)怎么样

  • create a class that acts as a wrapper for Foo and Bar called FooBar ?创建一个 class 作为FooBar的包装器,称为FooBar
public class FooBar
{
    public Foo Foo { get; set; }
    public Bar Bar { get; set; }
}

Why?为什么?

C# is an object-oriented programming language. C# 是一种面向对象的编程语言。 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.它基于将数据片段以及与该数据相关的行为包装到称为对象的特殊捆绑包中的概念,您可以从称为 class 的“蓝图”构建这些包。

You can use a Tuple :您可以使用Tuple

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

        return Tuple.Create(foo, bar);
    }

Or a ValueTuple :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.或者,正如 Dennis1679 建议的那样,制作一个自定义类型。

I think this is what you are asking我想这就是你要问的

public sealed class FooBar : Foo, Bar
{

}

Unfortunately this is not possible in C#.不幸的是,这在 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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