2016-01-14 121 views
6

我已经浏览了许多类似于这个问题的问题,但没有一个真正触及我想要做的事情。我所试图做的是从外部源读取的变量还包括它们的数据类型为字符串数组列表:带多种返回类型的方法

例子:

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

我然后存储这些对象到词典中包含多个字符串的对象,ID也用作关键字。

我想要做的是现在创建一个方法,该方法使用switch语句将字符串转换为正确的数据类型,并返回它,而无需在方法调用中指定任何内容。该函数应该是这个样子:

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); 
boolean isTrue = Method(string key); 

也许我错过了一些东西,但我还没有找到的东西,确实相当的东西喜欢这个。对此的任何和所有想法也是受欢迎的。

+2

对不起,没有这样的事情在C#中。一种方法有一个固定的返回类型。你可以使用'object'作为你的返回类型,但你必须施放。 – adv12

+1

你必须使该方法通用才能工作。当你使用它时,它最终会看起来像'string word =方法(key)'。你也必须做'bool?'和'int?'因为当你没有找到密钥时你想返回'null'。 – juharr

+0

如果你的值被存储为'Object',你可以做'(T)值'。什么阻止你从'string word =(string)方法(string key);',给定,方法返回'object' –

回答

6

编译器有没有办法三个方法来区分来电您提供,因为他们看起来都像Method(key);

一种选择是返回object,然后期望消费代码丢给他们想:

​​

您也可以使用关键字dynamic,使投隐:

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是一个非常强大的概念,它很容易失控,所以你必须非常小心。

在我看来,你期待你的调用代码知道它期望为每个键获取哪种类型的对象。看起来也许是最好的办法是只让用户供应信息:

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); 

这样的话,就没有必要跟踪在第一时间在你的字典对象的类型值。

+1

我意识到OP使用'boolean',但在C#中它是'bool'或'Boolean'。 – juharr

+0

@juharr:对,谢谢。 – StriplingWarrior

+0

同样来自OP,'Var'在C#中无效,应该是'var'。 –

0

函数的返回类型必须键入。与任何其他变量或操作一样,从指定类型继承的任何类型都是有效的返回值(这就是为什么对象允许任何值作为值)。

我个人不认为这是有益的,使一个方法有多种返回类型,但如果你真的想有一个方法有多种返回类型,你可以用.NET 4.0的dynamic类型:

private static void Main(string[] args) 
    { 
     int x = Method("varName3"); 
     string word = Method("varName2"); 
     bool isTrue = Method("varName1"); 
    } 

    private static dynamic Method(string key) 
    { 
     var dictionary = new Dictionary<string, KeyValuePair<Type, object>>() 
     { 
      { "varName1", new KeyValuePair<Type, object>(typeof(bool), false) }, 
      { "varName2", new KeyValuePair<Type, object>(typeof(string), "str") }, 
      { "varName3", new KeyValuePair<Type, object>(typeof(int), 5) }, 
     }; 

     if (dictionary.ContainsKey(key)) 
     { 
      return dictionary[key].Value; 
     } 

     return null; 
    } 

希望它有帮助

+0

非常奇妙,但不幸的是我仅限于3.5框架。背景:这是一个使用Unity的人的工具。 –

+0

如果您仅限于.NET 3.5,我会尝试使该方法具有通用性: int x =方法(“varName3”); 请看看StriplingWarrior的回答。 – Sneijder