简体   繁体   English

如何从方法的switch语句返回不确定类型的值

[英]How to return a value of indeterminate type from a switch statement from a method

I have the following switch statement: 我有以下switch语句:

switch (SomeType)
{
    case SomeType.A:
        var data = (Type_1)SomeType.Data;
    case SomeType.B:
        var data = (Type_2)SomeType.Data;
    case SomeType.C:
        var data = (Type_3) SomeType.Data;
    case SomeType.D:
        var data = (Type_4) SomeType.Data;
    case SomeType.E:
        var data = (Type_5) SomeType.Data;
    case SomeType.F:
        var data = (Type_6) SomeType.Data;
}

I have two problems with this: 我对此有两个问题:

One being the switch statement seems to treat the whole thing as one scope, therefore I can't set var data multiple times, I think I can overcome this because this needs to be wrapped in a method that returns the value, so I can just do something like return (Type_6) SomeType.Data; 一个是switch语句似乎将整个事情视为一个范围,因此我不能多次设置var data ,我想我可以克服这一点,因为这需要包装在一个返回值的方法中,所以我可以做类似return (Type_6) SomeType.Data; .

The second problem is how can I wrap this in a return method? 第二个问题是如何将其包装在return方法中? As far as I know in C# the method must explicitly define the return type. 据我所知,在C#中,该方法必须显式定义返回类型。 Any ideas? 有任何想法吗?

Dynamic 动态

You can use dynamic as a return type: 您可以将dynamic用作返回类型:

public dynamic GetData()
{
    dynamic data;
    switch (SomeType)
    {
        case SomeType.A:
            data = (Type_1)SomeType.Data;
        case SomeType.B:
            data = (Type_2)SomeType.Data;
        case SomeType.C:
            data = (Type_3) SomeType.Data;
        case SomeType.D:
            data = (Type_4) SomeType.Data;
        case SomeType.E:
            data = (Type_5) SomeType.Data;
        case SomeType.F:
            data = (Type_6) SomeType.Data;
        default: throw new NotSupportedException();
    }
    return data;
}

Just add some break statements or it won't compile. 只需添加一些break语句,否则它将无法编译。

But dynamic is highly contagious - if you use it in one method it will probably propagate through all methods that use the original method. 但是动态性具有高度的传染性 -如果以一种方法使用它,它可能会传播到所有使用原始方法的方法中。


Reworking architecture 重做架构

While dynamic is a quick fix, it is not the best solution, as it has been already pointed by commentators. 尽管动态解决方案是一种快速解决方案,但它并不是最佳解决方案,因为评论员已经指出了这一点。 Ideally, you should strive to preserve type information as much as possible - it simplifies development and reduces the likelihood of mistakes. 理想情况下,您应该努力保留尽可能多的类型信息-这样可以简化开发并减少出错的可能性。

You can use some base class or interface for all your possible data objects: 您可以对所有可能的数据对象使用一些基类或接口:

    public interface IData
    {
        // Common properties and methods
    }

    public IData GetData()
    {
        return SomeData.Data;
    }

But it may require a substantial reconsideration of your architecture, that may or may not be possible if different Data types differ too much. 但这可能需要对架构进行实质性的重新考虑,如果不同的Data类型相差太大,则可能会或可能不会。

Visitor Pattern 访客模式

If it is the case, then you may take a look at Visitor Pattern that deals with cases when you data has to be processed very differently. 如果是这种情况,那么您可以看看访问者模式 ,该模式处理的情况是必须对数据进行非常不同的处理。 It still requires the same base interface as return type, but now it will look like: 它仍然需要与返回类型相同的基本接口,但现在看起来像:

public interface IData
{
    void Accept(IDataVisitor visitor);
}

public interface IDataVisitor
{
    void Visit(DataA data);
    void Visit(DataB data);
    void Visit(DataC data);
}

public class DataA : IData { public void Accept(IDataVisitor visitor) {visitor.Visit(this);}}
public class DataB : IData { public void Accept(IDataVisitor visitor) {visitor.Visit(this);}}
public class DataC :  IData { public void Accept(IDataVisitor visitor) {visitor.Visit(this);}}

public class ConsoleVisitor : IDataVisitor
{
    public void Visit(DataA data)
    {
        Console.WriteLine("A" + data);
    }

    public void Visit(DataB data)
    {
        Console.WriteLine("B" + data);
    }

    public void Visit(DataC data)
    {
        Console.WriteLine("C" + (data);
    }
}

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

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