简体   繁体   English

C# 返回(动态或匿名?)对象,其返回值来自其他方法作为属性

[英]C# return (dynamic or anonymous?) object with return values from other methods as properties

I would like to return an object, which stores the return values from other class methods as properties on the return object.我想返回一个对象,它将其他类方法的返回值存储为返回对象的属性。 The problem is that I do not know which is the best way to do this in C#.问题是我不知道在 C# 中哪个是最好的方法。 Currently I am using a sort of JavaScript-ish approach.目前我正在使用一种类似于 JavaScript 的方法。 Because I do not know the return type, I use the dynamic keyword.因为不知道返回类型,所以使用了dynamic关键字。

class Test {

    public static dynamic MyExportingMethod() {
        return new {
            myString = MyStringMethod(),
            myInt = MyIntMethod()
        };
    }

    public static string MyStringMethod() {
        return "Hello";
    }

    public static int MyIntMethod() {
        return 55;
    }

   }

And then being able to access them like so,然后能够像这样访问它们,

var myReturnObjWithProps = Test.MyExportingMethod();
myReturnObjWithProps.myString; // should be "Hello"

So my questions are, should I use the dynamic return type?所以我的问题是,我应该使用动态返回类型吗? Am I not just returning an anonymous object?我不只是返回一个匿名对象吗?

Make a class for the return type.为返回类型创建一个类。 You want something strongly typed, both for performance and for clarity.为了性能和清晰度,您需要强类型的东西。

class Foo
{
    string MyString { get; set}
    int MyInt { get; set}
}

class Test 
{

    public static Foo MyExportingMethod() 
    {
        return new Foo
        {
            MyString = MyStringMethod(),
            MyInt = MyIntMethod()
        };
    }

    public static string MyStringMethod() 
    {
        return "Hello";
    }

    public static int MyIntMethod() 
    {
        return 55;
    }
}

You should use dynamic sparingly.您应该谨慎使用dynamic This way anyone can see what your method is returning.这样任何人都可以看到您的方法返回的内容。 If you return dynamic the caller has no way of knowing what to look for in the dynamic without looking at the source for your method.如果您返回动态,则调用者无法知道在动态中查找什么,而无需查看您的方法的源代码。 That way the abstraction goes away and well-structured programming is all about abstraction.这样抽象就消失了,结构良好的编程就是关于抽象的。

should I use the dynamic return type?我应该使用动态返回类型吗?

Yes - a method that returns dynamic effectively returns an object , so you have to use dynamic in order to access it's properties at runtime without reflection.是的 - 有效地返回dynamic的方法会返回一个object ,因此您必须使用dynamic才能在运行时访问它的属性而无需反射。

Am I not just returning an anonymous object?我不只是返回一个匿名对象吗?

You are, but the declared type of the method is effectively object so its properties cannot be referenced at compile-time.您是,但是该方法的声明类型实际上是object因此在编译时无法引用其属性。

Bottom line - you should avoid returning anonymous types from methods if at all possible.底线 - 如果可能,您应该避免从方法中返回匿名类型。 Use a defined type, or keep the creation and usage of the anonymous type in one method so you can use var and let the compiler infer the type.使用定义的类型,或在一个方法中保留匿名类型的创建和使用,以便您可以使用var并让编译器推断类型。

An alternative to creating a new class simply for a method return would be ValueTuple :为方法返回创建新类的另一种方法是ValueTuple

public static (string myString, int myInt) MyExportingMethod()
{
    return (MyStringMethod(), MyIntMethod());
}

var (myString, myInt) = Test.MyExportingMethod();
myString; // == "Hello"

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

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