如何在字典对象中获取动态数据类型 [英] How to take dynamic datatype in dictionary object

查看:128
本文介绍了如何在字典对象中获取动态数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我想出了我以querystring的形式从用户那里获取价值的情况。前两个参数的名称是修复的,因此查询字符串的名称是固定的。在这两个参数之后,用户可以输入或不输入任何参数。参数计数可以是1到多个,并且数据类型也不同。



例如:sitename / sample.aspx?username =''& userid =''& Date =

Ex:sitename / sample.aspx?username = ''& userid =''& Date =& amount =



这就是我创建字典对象并将动态查询字符串的值和值存储到其中的原因(不是用户名和用户ID)





Hello I came up with situation where I am taking value from user in the form of querystring. Name of first 2 parameter is fix,so name name of querystring is fix.After these 2 parameter,user may enter or may not enter any parameter.Parameter count can be from 1 to many and vary in datatype.

Ex: sitename/sample.aspx?username=''&userid=''&Date=
Ex: sitename/sample.aspx?username=''&userid=''&Date=&amount=

That's why I created dictionary object and storing key and value of dynamic querystring into it(not username and userid)


Dictionary<string, string> queryStringValues = new Dictionary<string, string>();

foreach (string key in Request.QueryString.AllKeys)
{
    queryStringValues.Add(key, Request.QueryString[key]);
}





这里我创建了.But字典我的密钥必须是字符串,值可能不是字符串。它可以是int ,date.How采取那个。最后我想检查数值的数据类型。怎么做



我尝试过:





Here I created dictionary of .But my key must be string and value may not string.It can be int,date.How to take that.At last I want to check datatype of value.How to do that

What I have tried:

Dictionary<string, string> queryStringValues = new Dictionary<string, string>();

foreach (string key in Request.QueryString.AllKeys)
{
    queryStringValues.Add(key, Request.QueryString[key]);
}

推荐答案

首先,查询字符串就是这样;一个字符串。



您将始终根据您打算如何使用它来解析每种值类型。



其次,动态类型是非常具体的,而不是你的意思,但我理解你的意思。



为了存储不同的单个变量中的类型类型然后该变量类型必须是要存储的所有类型共享的最低公共类型基本类型。这将是'对象'类型。



无论如何,您仍然需要将对象类型转换为您的知识类型,因此这一步似乎毫无意义。当你使用它时解析字符串。



我希望有帮助

Andy ^ _ ^
Firstly, a Query String is exactly that; a string.

You will always have to parse each value type according to how you intend to use it.

Secondly, A 'dynamic' type is something very specific and not what you mean to say, but I understand what you mean.

In order to store different 'Types' in a single variable Type then that variable Type must be the lowest common type base type shared by all types you want to store. That would be the 'object' type.

You will still have to cast out of the object type into your know type anyway so this step seems pointless. Just parse the string when you use it.

I hope that helps
Andy ^_^


这篇关于如何在字典对象中获取动态数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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