C# 使用泛型返回文字字符串或从字典返回<;字符串,对象>;

C# 使用泛型返回文字字符串或从字典返回<;字符串,对象>;,c#,generics,C#,Generics,我想这次我比自己聪明多了。请随意编辑标题,我想不出一个好的标题 我从一个文件中读取,然后在该文件中会出现一个字符串,因为它类似于xml文件。但文件中会有一个文本值或一个“命令”,用于从workContainer中获取该值 所以 另一个例子是 所以 3 或 [?NumberOfSales?] 这是我开始做的程序 public class WorkContainer:Dictionary<string, object> { public T GetKeyValue<T&

我想这次我比自己聪明多了。请随意编辑标题,我想不出一个好的标题

我从一个文件中读取,然后在该文件中会出现一个字符串,因为它类似于xml文件。但文件中会有一个文本值或一个“命令”,用于从workContainer中获取该值

所以

另一个例子是

所以

3

[?NumberOfSales?]
这是我开始做的程序

public class WorkContainer:Dictionary<string, object>
{
    public T GetKeyValue<T>(string Parameter) 
    {
        if (Parameter.StartsWith("[? "))
        {
            string key = Parameter.Replace("[? ", "").Replace(" ?]", "");

            if (this.ContainsKey(key))
            {
                return (T)this[key];
            }
            else
            {
                // may throw error for value types
                return default(T);
            }

        }
        else
        {
            // Does not Compile
            if (typeof(T) is string)
            {
                return Parameter
            }
            // OR return (T)Parameter

        }
    }
}
公共类工作容器:字典
{
公共T GetKeyValue(字符串参数)
{
if(参数.StartsWith(“[?”)
{
字符串键=参数。替换(“[?”,“”)。替换(“?]”,“”);
如果(本文件包含密钥))
{
返回(T)此[键];
}
其他的
{
//可能会引发值类型错误
返回默认值(T);
}
}
其他的
{
//不编译
if(typeof(T)是字符串)
{
返回参数
}
//或返回(T)参数
}
}
}
电话是

  mail.To = container.GetKeyValue<string>("me@company.com");
mail.To=container.GetKeyValue(“me@company.com");

mail.To=container.GetKeyValue(“[?MyEmail?]”);
int answer=container.GetKeyValue(“3”);

answer=container.GetKeyValue(“[?NumberOfSales?]”);
但它不编译行

if (typeof(T) is string)
当typeof运算符给出类型对象时,将始终返回false。应将其替换为

if (T is string)
此外,您还应该查看该方法。它在这里可能会有所帮助。

使用
typeof(T)==typeof(string)
更改:

if(typeof(T) == typeof(string))
{
    return (T)Parameter;
}
else
{
    // convert the value to the appropriate type
}
if (typeof(T) is string)
致:


is运算符仅在类实例上有效。T实际上不是一个实例,它是一个类型,因此在代码中使用is将不会编译,因为您需要比较两种类型。您可以在msdn上阅读更多有关它的信息。

因此,这是我提出的答案,我有点担心装箱和拆箱,但它现在可以工作了

public class WorkContainer:Dictionary<string, object>
{
    public T GetKeyValue<T>(string Parameter) 
    {
        if (Parameter.StartsWith("[? "))
        {
            string key = Parameter.Replace("[? ", "").Replace(" ?]", "");

            if (this.ContainsKey(key))
            {
                if (typeof(T) == typeof(string) )
                {
                    // Special Case for String, especially if the object is a class
                    // Take the ToString Method not implicit conversion
                    return (T)Convert.ChangeType(this[key].ToString(), typeof(T));
                }
                else
                {
                    return (T)this[key];
                }
            }
            else
            {
                return default(T);
            }

        }
        else
        {                
            return (T)Convert.ChangeType(Parameter, typeof(T));
        }
    }
}
公共类工作容器:字典
{
公共T GetKeyValue(字符串参数)
{
if(参数.StartsWith(“[?”)
{
字符串键=参数。替换(“[?”,“”)。替换(“?]”,“”);
如果(本文件包含密钥))
{
if(typeof(T)=typeof(string))
{
//字符串的特殊情况,特别是当对象是类时
//采用ToString方法,而不是隐式转换
返回(T)Convert.ChangeType(此[key].ToString(),typeof(T));
}
其他的
{
返回(T)此[键];
}
}
其他的
{
返回默认值(T);
}
}
其他的
{                
return(T)Convert.ChangeType(参数,typeof(T));
}
}
}

您遇到了什么编译错误?错误…否。您的解决方案将不会编译。
is
运算符用于确定值(变量、文字或常量)是一种特殊类型。它不能用于比较两种类型。我给你+1是因为即使你的T是字符串错误,Convert.ChangeType是关键。仍然错误不能隐式地将类型'string'转换为'T'。迈克:我的错误;即使有逻辑,仍然需要强制转换。我编辑了答案。即使(typeof(T)=typeof(String))工作返回仍然是一个问题仍然错误无法将类型“String”隐式转换为“T”
  mail.To = container.GetKeyValue<string>("[? MyEmail ?]");

  int answer = container.GetKeyValue<int>("3");
  answer = container.GetKeyValue<int>("[? NumberOfSales ?]");
if (typeof(T) is string)
if (T is string)
if(typeof(T) == typeof(string))
{
    return (T)Parameter;
}
else
{
    // convert the value to the appropriate type
}
if (typeof(T) is string)
if (typeof(T) == typeof(String))
public class WorkContainer:Dictionary<string, object>
{
    public T GetKeyValue<T>(string Parameter) 
    {
        if (Parameter.StartsWith("[? "))
        {
            string key = Parameter.Replace("[? ", "").Replace(" ?]", "");

            if (this.ContainsKey(key))
            {
                if (typeof(T) == typeof(string) )
                {
                    // Special Case for String, especially if the object is a class
                    // Take the ToString Method not implicit conversion
                    return (T)Convert.ChangeType(this[key].ToString(), typeof(T));
                }
                else
                {
                    return (T)this[key];
                }
            }
            else
            {
                return default(T);
            }

        }
        else
        {                
            return (T)Convert.ChangeType(Parameter, typeof(T));
        }
    }
}