简体   繁体   English

使用 switch 声明不同类型的变量

[英]Declare different type of variables using switch

I am trying to create a new variable based on a particular value received from a function.我正在尝试根据从函数接收到的特定值创建一个新变量。

The variable can be of different classes.变量可以是不同的类。

Here's what I am trying:这是我正在尝试的:

switch (request)
{
    case "ClassA":
        {
            var x = new ClassA();
            break;
        }
    case "ClassB":
        {
            var x = new ClassB();
            break;
        } 
    case "ClassC":
        {
            var x = new ClassC();
            break;
        } 
    case "ClassD":
        {
            var x = new ClassD();
            break;
        }
    default:
        break;
}

This is fine till here and no issues.一直到这里都很好,没有问题。 The issue arises, when I try to use the value of x out of the scope of the switch statement.当我尝试使用超出 switch 语句范围的x值时,问题就出现了。 The system says that x does not exists in the current context.系统说x在当前上下文中不存在。

Is there any way to achieve this?有没有办法实现这一目标?

You must declare x outside switch .您必须在switch之外声明x and declare it only once.并且只声明一次。 and if classes does not have same parent you must use dynamic as a type of x.如果类没有相同的父级,则必须使用dynamic作为 x 的类型。

ParentClass x = null;// dynamic x = null; in case that x is not known type
switch (request)
        {
            case "ClassA":
                {
                    x = new ClassA();
                    break;
                }
            case "ClassB":
                {
                    x = new ClassB();
                    break;
                } 
            case "ClassC":
                {
                    x = new ClassC();
                    break;
                } 
            case "ClassD":
                {
                    x = new ClassD();
                    break;
                }
            default:
                break;
        }

This looks like a good place for an interface or abstract class .这看起来是一个interfaceabstract class的好地方。

Basically, to most easily solve your issue, you would implement an interface as follows:基本上,为了最轻松地解决您的问题,您将实现如下interface

interface IDescriptiveName
{
    DescriptiveNameType Type { get; }
}

enum DescriptiveNameType
{
    ClassA
}

Each class would then implement DescriptiveNameType Type and return their type.然后每个类将实现DescriptiveNameType Type并返回它们的类型。 (Generally DescriptiveNameType is an enum .) Ie (通常DescriptiveNameType是一个enum 。)即

public class ClassA : IDescriptiveName
{
    public DescriptiveNameType Type { get { return DescriptiveNameType.ClassA; } }
}

Then, based on the value of ClassA.Type (which for ClassA would be ClassA ) you could cast x and work with it.然后,根据ClassA.Type的值(对于ClassA将是ClassA ),您可以转换x并使用它。

IDescriptiveName x = null;

// original switch logic simply remove var

if (x.Type == DescriptiveNameType.ClassA)
{
    // do something for ClassA
}

Rather than use a switch statement, use a Dictionary to provide the class lookup:不使用 switch 语句,而是使用Dictionary来提供类查找:

private readonly Dictionary<string, Func<object>> classLookup = 
    new Dictionary<string, Func<object>>
{
    { "ClassA", () => new ClassA() },
    { "ClassB", () => new ClassB() },
    { "ClassC", () => new ClassC() },
    { "ClassD", () => new ClassD() }
};

public object GetObject(string className)
{
    return classLookup.ContainsKey(className)
        ? classLookup[className]()
        : null;
}

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

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