【问题标题】:Creating Function with Generic Return Type创建具有通用返回类型的函数
【发布时间】:2012-08-18 21:53:30
【问题描述】:

目前我有以下功能

  public int GetVariableValue(TaxReturn taxReturnObj, string identityKey)
    {
        int returnTypeOut = 0;
        if (taxReturnObj.Identity.ContainsKey(identityKey))
        {
            int.TryParse(taxReturnObj.Identity[identityKey], out returnTypeOut);
        }
        return returnTypeOut;
    }

要检索我们使用以下代码的值,

例如

int valPayStatus = GetVariableValue(objTaxretrun, TaxReturnIdentity.aadata_identity_paystatus)

到目前为止,它工作正常,因为所有 Identity 值都是整数,但最近我们添加了具有 String 和 Boolean 类型的新 Identites。 所以我想将上述功能设为Generic...但我不知道该怎么做,我试图在谷歌上搜索但找不到任何东西。

【问题讨论】:

  • I tried to search on google but couldn't find anything google 死了吗?!..checking...aah 一切正常,它可以找到this 之类的东西
  • 我会使用不同的方法:GetVariableValueAsInt, GetVariableValueAsString, ...这里的问题是您要对每种类型进行特定处理(int.parse、bool.parse、简单转换to string...),所以有些类型会被忽略。通过调用泛型方法 GetVariableValue,我无法知道 T 接受哪些类型以及哪些类型会引发异常。

标签: c# asp.net generics types


【解决方案1】:

我会这样做,也许有更好的方法;

public T GetVariableValue<T>(TaxReturn taxReturnObj, string identityKey)
        {
            if (taxReturnObj.Identity.ContainsKey(identityKey))
            {
                if(typeof(T) == Int32)
                {     
                    int returnTypeOut = 0;
                    int.TryParse(taxReturnObj.Identity[identityKey], out returnTypeOut);
                    return returnTypeOut;
                }
                else if (typeof(T) == System.String)
                {
                //code here
                }
            }
            return default(T);
        }

你可以这样称呼它

int valPayStatus = GetVariableValue<int>(objTaxretrun, TaxReturnIdentity.aadata_identity_paystatus)


string valPayStatusStr = GetVariableValue<string>(objTaxretrun, TaxReturnIdentity.aadata_identity_paystatus)

【讨论】:

  • 感谢您的帮助...但是这里的问题是我无法直接返回,因为我正在使用需要输出参数的 int.parse 方法...并且需要先初始化输出参数...仅此而已我得到编译错误的地方......
【解决方案2】:
    public T GetVariableValue<T>(TaxReturn taxReturnObj, string identityKey) 
    { 
        if (taxReturnObj.Identity.ContainsKey(identityKey)) 
        { 
            return (T) Convert.ChangeType(taxReturnObj.Identity[identityKey], typeof(T));
        }
        else
        {
           return default(T);
        }
    } 

【讨论】:

  • 非常感谢...改动很小..完美运行..:)
【解决方案3】:

你可以试试这样的:

public T GetVariableValue<T>(TaxReturn taxReturnObj, string identityKey)
{
    if (taxReturnObj.Identity.ContainsKey(identityKey))
    {
        return taxReturnObj.Identity[identityKey];   
    }
    return default(T);
}

如果您在字典 Identity 中找到 key,则返回该特定值,而不对其进行解析,或者您可以返回 default

【讨论】:

  • 为什么会这样?我认为不可能将对象隐式转换为 T。我猜 Identity-property 是一个对象列表。
  • 好吧来自ContainsKey,Identity看起来像一本字典,可以定义为public Dictionary&lt;string, dynamic&gt; Identity { get; set; }
  • 可能是public Dictionary&lt;string, object&gt; Identity { get; set; }
【解决方案4】:
public T GetVariableValue<T>(TaxReturn taxReturnObj, string identityKey)
{
    if (taxReturnObj.Identity.ContainsKey(identityKey))
    {
        return (T)taxReturnObj.Identity[identityKey];
    }
    return default(T);
}

像这样的东西? 并不意味着如果你传递了错误的类型,它就不会抛出异常。

【讨论】:

  • 我会这样做,但可能会用 try-catch 包围它
  • 非常感谢,我收到编译时错误,例如无法将类型字符串的表达式转换为类型 T
  • 我想知道如果您需要使用 renes 代码之类的方式解析值,您的 int 是否实际上是字符串。
猜你喜欢
  • 1970-01-01
  • 2015-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-13
  • 2020-05-09
  • 2017-11-02
  • 2020-05-27
相关资源
最近更新 更多