使用泛型返回一个字符串或者字典<字符串对象> [英] Using Generics to return a literal string or from Dictionary<string, object>

查看:132
本文介绍了使用泛型返回一个字符串或者字典<字符串对象>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我自己扑空这一次。随意编辑标题我也想不出一个好一个。



我从文件中读取,然后在该文件将是一个字符串,因为它像一个xml文件。但是,该文件将是文字值或命令,从workContainer



所以



获得的价值

 <电子邮件和GT; me@company.com< /电子邮件与GT; 

 <电子邮件和GT;? MyEmail<?/电子邮件与GT; 



我想要做的写作IFS,而不是全把它放在一个泛型函数<的地方/ p>

这样的逻辑是

 如果从容器别人抢串集装箱指挥抢并转换为需要的类型
及其到用户,以保证该文件是确定和类型是正确

所以另一个例子是



所以

 <答案→3&下; /答案> 

 <应答和GT;? NumberOfSales<?/应答和GT; 

这是我一开始就



工作程序

 公共类WorkContainer:字典<字符串对象> 
{
公共牛逼GetKeyValue< T>(字符串参数)
{
如果(Parameter.StartsWith([?))
{
串键= Parameter.Replace([?,).Replace(?],);

如果(this.ContainsKey(密钥))
{
回报率(T)本[关键]
}
,否则
{
//可能会抛出值类型
返回默认值(T)的错误;
}

}
,否则
{
//不能编译
如果(typeof运算(T)为字符串)
{
返回参数
}
//或返回(T)参数

}
}
}

召唤就会

  mail.To = container.GetKeyValue<串GT(me@company.com); 

  mail.To = container.GetKeyValue<串GT;([MyEmail?]); 

INT答案= container.GetKeyValue&所述; INT>(3);

 答案= container.GetKeyValue<&诠释GT;([?NumberOfSales]); 



不过,这并不编译?


解决方案

因此,这里是我想出了,我对装箱和拆箱有点担心的答案,但它适用于现在。

 公共类WorkContainer:字典<字符串对象> 
{
公共牛逼GetKeyValue< T>(字符串参数)
{
如果(Parameter.StartsWith([?))
{
串键= Parameter.Replace([?,).Replace(?],);

如果(this.ContainsKey(密钥))
{
如果(typeof运算(T)== typeof运算(字符串))
{
//特例字符串,特别是如果对象是类
//拿ToString方法没有隐式转换
回报率(T)Convert.ChangeType(本[关键]的ToString()的typeof(T) );
}
,否则
{
回报率(T)本[关键]
}
}
,否则
{
返回默认值(T);
}

}
,否则
{
回报率(T)Convert.ChangeType(参数中的typeof(T));
}
}
}


I think I outsmarted myself this time. Feel free to edit the title also I could not think of a good one.

I am reading from a file and then in that file will be a string because its like an xml file. But in the file will be a literal value or a "command" to get the value from the workContainer

so

<Email>me@company.com</Email>

or

<Email>[? MyEmail ?]</Email>

What I wanted to do instead of writing ifs all over the place to put it in a generic function

so logic is

If Container command grab from container else grab string and convert to desired type 
Its up to the user to ensure the file is ok and the type is correct 

so another example is

so

<Answer>3</Answer>

or

<Answer>[? NumberOfSales ?]</Answer>

This is the procedure I started to work on

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

        }
    }
}

The Call would be

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

or

  mail.To = container.GetKeyValue<string>("[? MyEmail ?]");

  int answer = container.GetKeyValue<int>("3");

or

  answer = container.GetKeyValue<int>("[? NumberOfSales ?]");

But it does not compile?

解决方案

So here is the answer I came up with, I am a little worried about boxing and unboxing but it works for now

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

这篇关于使用泛型返回一个字符串或者字典&LT;字符串对象&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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