C# 使用泛型返回类型创建函数

C# 使用泛型返回类型创建函数,c#,asp.net,generics,types,C#,Asp.net,Generics,Types,目前我有以下功能 public int GetVariableValue(TaxReturn taxReturnObj, string identityKey) { int returnTypeOut = 0; if (taxReturnObj.Identity.ContainsKey(identityKey)) { int.TryParse(taxReturnObj.Identity[identityKey],

目前我有以下功能

  public int GetVariableValue(TaxReturn taxReturnObj, string identityKey)
    {
        int returnTypeOut = 0;
        if (taxReturnObj.Identity.ContainsKey(identityKey))
        {
            int.TryParse(taxReturnObj.Identity[identityKey], out returnTypeOut);
        }
        return returnTypeOut;
    }
要检索我们正在使用的值,请使用以下代码

e、 g

由于所有的Identity值都是整数,所以它一直工作正常,但最近我们添加了带有字符串和布尔类型的新identies。
所以我想把上面的函数设置为通用的…但我不知道怎么做,我试着在谷歌上搜索,但什么也找不到。

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

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);
        }
公共T GetVariableValue(TaxReturn taxReturnObj,字符串标识键) { if(taxReturnObj.Identity.ContainsKey(identityKey)) { if(类型(T)==Int32) { int returnTypeOut=0; int.TryParse(taxReturnObj.Identity[identityKey],out returnTypeOut); 返回打印输出; } else if(typeof(T)==系统字符串) { //代码在这里 } } 返回默认值(T); } 你可以这样称呼它

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


string valPayStatusStr = GetVariableValue<string>(objTaxretrun, TaxReturnIdentity.aadata_identity_paystatus)
int-valPayStatus=GetVariableValue(objTaxretrun,TaxReturnIdentity.aadata\u identity\u paystatus)
字符串valPayStatusStr=GetVariableValue(objTaxretrun,TaxReturnIdentity.aadata\u identity\u paystatus)
公共T GetVariableValue(TaxReturn taxReturnObj,字符串标识键)
{
if(taxReturnObj.Identity.ContainsKey(identityKey))
{
return(T)taxReturnObj.Identity[identityKey];
}
返回默认值(T);
}
像这样的?
这并不意味着如果你传递的执行选项类型错误,它就不会抛出执行选项。

你可以尝试以下方法:

public T GetVariableValue<T>(TaxReturn taxReturnObj, string identityKey)
{
    if (taxReturnObj.Identity.ContainsKey(identityKey))
    {
        return taxReturnObj.Identity[identityKey];   
    }
    return default(T);
}
公共T GetVariableValue(TaxReturn taxReturnObj,字符串标识键) { if(taxReturnObj.Identity.ContainsKey(identityKey)) { 返回税返回对象标识[identityKey]; } 返回默认值(T); } 如果在字典中找到
标识,则返回该特定值,而无需对其进行解析,或者可以返回
默认值
公共T GetVariableValue(TaxReturn taxReturnObj,string identityKey)
    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);
        }
    } 
{ if(taxReturnObj.Identity.ContainsKey(identityKey)) { return(T)Convert.ChangeType(taxReturnObj.Identity[identityKey],typeof(T)); } 其他的 { 返回默认值(T); } }
我试图在谷歌上搜索,但什么也找不到
谷歌死了吗?!。。检查…啊,一切正常,它可以找到像我会用不同的方法:GetVariableValueAsInt,GetVariableValueAsString。。。这里的问题是,您将对每种类型(int.parse、bool.parse、simple cast to string…)进行特定处理,因此某些类型将被忽略。通过调用一个泛型方法GetVariableValue,我无法知道T接受哪些类型以及哪些类型将引发异常。我会这样做,但可能会尝试一下,非常感谢,我遇到了编译时错误,比如无法将字符串类型的表达式转换为TI类型。我想知道如果需要解析值,int是否实际上是字符串,请使用renes代码。感谢帮助…但问题是我无法直接返回,因为我使用的是int.parse方法,它需要输出参数…以及该输出参数需要先初始化..这就是我得到编译错误的地方..为什么会这样?我认为不可能隐式地将object转换为t。我猜Identity属性是一个对象列表。从
ContainsKey
,Identity看起来像一个字典,可以定义为
public dictionary Identity{get;set;}
,也可以是
public dictionary Identity{get;set;}
非常感谢您……只做了一些小改动……效果非常好:)
public T GetVariableValue<T>(TaxReturn taxReturnObj, string identityKey)
{
    if (taxReturnObj.Identity.ContainsKey(identityKey))
    {
        return taxReturnObj.Identity[identityKey];   
    }
    return default(T);
}
    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);
        }
    }