具有多种返回类型的方法 [英] Method with Multiple Return Types

查看:92
本文介绍了具有多种返回类型的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了许多与此相似的问题,但是没有一个问题真正涉及到我真正想做的事情.我想做的是从外部源中读取一个变量列表,这些变量的数据类型也包含在字符串数组中:

I've looked through many questions that are similar to this, but none of them really touched on what I precisely want to do. What I am trying to do is read from an external source a list of variables that also include their data type into a string array:

示例:

ID/Key      Type    Value/Data; 
varName1    bool    true;
varName2    string  str;
varName3    int     5;

然后我将这些对象存储在字典中,作为包含多个字符串的对象,其中ID也是键.

I then store these are objects into a dictionary as objects containing several strings, with the ID also serving as the key.

我现在要做的是创建一个使用switch语句的方法,该语句将字符串转换为正确的数据类型,然后返回该字符串,而无需在方法调用中指定任何内容.该函数应如下所示:

What I want to do is now create a method that uses a switch statement that casts the string into the correct datatype, and returns it without having to specify anything in the method call. The function should look something like this:

public ??? Method(string key)
{
    if(dictionary.ContainsKey(ID))
    {
        Var temp = dictionary[ID];

        switch (temp.Type)
        {
            case "bool":
                return Convert.ToBoolean(temp.Value);

            case "int"
                return Convert.ToInt(temp.Value);

            case "string"
                return temp.Value;
        }
    }

    return "NULL"; 
}

方法调用应如下所示:

int x = Method(string key);
string word = Method(string key);
bool isTrue = Method(string key);

也许我错过了一些东西,但是我还没有找到真正能做到这样的东西.也欢迎对此发表任何想法.

Maybe I've missed something, but I have yet to find something that really does something quite like this. Any and all thoughts about this are welcome as well.

推荐答案

编译器无法区分您提供的三个方法调用,因为它们看上去都像Method(key);

The compiler has no way to distinguish between the three method calls you've provided, because they all look like Method(key);

一种选择是返回object,然后期望使用代码将其强制转换为所需的内容:

One option is to return an object and then expect the consuming code to cast it to what they want:

public object Method(string key)
{
    if(dictionary.ContainsKey(key))
    {
        var temp = dictionary[key];

        switch (temp.Type)
        {
            case "bool":
                return Convert.ToBoolean(temp.Value);

            case "int"
                return Convert.ToInt(temp.Value);

            case "string"
                return temp.Value;
        }
    }

    return "NULL"; 
}

...

int x = (int) Method(key);
string word = (string) Method(key);
bool isTrue = (bool) Method(key);

您还可以使用dynamic关键字来隐含强制转换:

You could also use the dynamic keyword to make the cast implicit:

public dynamic Method(string key)
{
    if(dictionary.ContainsKey(key))
    {
        var temp = dictionary[key];

        switch (temp.Type)
        {
            case "bool":
                return Convert.ToBoolean(temp.Value);

            case "int"
                return Convert.ToInt(temp.Value);

            case "string"
                return temp.Value;
        }
    }

    return "NULL"; 
}

...

int x = Method(key);
string word = Method(key);
bool isTrue = Method(key);

但是,dynamic是一个非常强大的概念,它很容易失控,因此您必须非常小心.

However, dynamic is a very powerful concept, and it's easy for it to get out of hand, so you have to be really careful with that.

在我看来,您期望您的调用代码知道每个键期望获得的对象类型.似乎最好的方法似乎就是让用户提供该信息:

It seems to me that you're expecting your calling code to know which type of object it's expecting to get for each key. It seems like maybe the best approach is to just let the user supply that information:

public T Method<T>(string key)
{
    if(dictionary.ContainsKey(key))
        return (T) Convert.ChangeType(dictionary[key].Value, typeof(T));
    return default(T);
}

...

int x = Method<int>(key);
string word = Method<string>(key);
bool isTrue = Method<bool>(key);

这样,就无需首先在字典对象中跟踪Type值.

That way, there's no need to track the Type value in your dictionary objects in the first place.

这篇关于具有多种返回类型的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆