- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在一页上加密我的整个查询字符串,然后在另一页上解密。我正在使用 HttpUtility.ParseQueryString 获取所有值的 NameValueCollection。
现在,我有一个类,其属性与查询字符串变量名相匹配。我正在努力如何从查询字符串中设置属性的值。
这是我的代码:
NameValueCollection col = HttpUtility.ParseQueryString(decodedString);
ConfirmationPage cp = new ConfirmationPage();
for(int i = 0; i < col.Count; i++)
{
Type type = typeof(ConfirmationPage);
FieldInfo fi = type.GetField(col.GetKey(i));
}
我看到了通过反射检索值的示例 - 但我想获得对 ConfirmationPage 类的属性的引用,并在循环中使用它的值设置它 - col.Get(i)。
最佳答案
我可能会走另一条路并找到属性(或使用 GetFields() 的字段)并在查询参数中查找它们而不是遍历查询参数。然后,您可以在 PropertyInfo 对象上使用 SetValue 方法来设置 ConfirmationPage 上的属性值。
var col = HttpUtility.ParseQueryString(decodedString);
var cp = new ConfirmationPage();
foreach (var prop in typeof(ConfirmationPage).GetProperties())
{
var queryParam = col[prop.Name];
if (queryParam != null)
{
prop.SetValue(cp,queryParam,null);
}
}
关于c# - 从 NameValueCollection 设置类的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5303762/
在我尝试用内部 NameValueCollection 初始化 NameValueCollection 后,c# 编译器说必须是 NameValueCollection(string,string)。
有没有办法从 NameValueCollection 中提取与特定模式/命名约定相关的键,而不必遍历集合中的每个值? 最佳答案 名称值集合的设计并不是特别有效地进行这样的搜索。无论您使用哪种方法,它都
我必须将通用对象转换为 NameValueCollection。我正在尝试使用反射。虽然我可以获得父属性,但我无法获取作为对象的父属性的属性。 Class One public string name
有人可以阐明我的 NameValueCollection 返回 Length 属性而不是 Name 和 Value 可以告诉我我在这里做错了什么。我无法为下拉列表设置 DataTextField 或
如何将动态转换为 NameValueCollection? 我尝试将它转换为字典,然后转换为 NameValueCollection,但它会针对不同类型抛出异常。 [...] NameValueC
我读过 NameValueCollection 允许添加重复的键,但是当我尝试使用它时,情况似乎并非如此。 我的代码 using (var wb = new WebClient()) { v
我有两个 NameValueCollection: NameValueCollection customTag = (NameValueCollection)System.Configuration.
假设我有一个匿名类实例 var foo = new { A = 1, B = 2}; 有没有快速生成 NameValueCollection 的方法?我想在事先不知道匿名类型的属性的情况下获得与下面的
我有一个 NameValueCollection,我想遍历这些值。目前,我正在这样做,但似乎应该有一种更简洁的方法来做到这一点: NameValueCollection nvc = new NameV
在 automapper 中,如何将名称值集合映射到强类型集合? Mapper.Map>(); public class MetaModel { public string Name;
例如,如果我们有 {"Parameters":["name" : "test"]}它将被序列化为 {"Parameters":["name"]} . (使用 System.Text.Json) 这是为
有没有办法使 NameValueCollection 或派生类可序列化? 最佳答案 NameObjectCollectionBase ( NameValueCollection 的基类)已经实现了 I
有谁知道断言 NameValueCollection 是否等效的好方法?目前我正在使用 NUnit,但 CollectionAssert.AreEquivalent() 似乎只声明键。不是键和值。 我
我有枚举, public enum ClearAttributes { S_DURATION, S_TYPE } 我正在使用枚举中的描述将项目添加到 NameValueCollecti
NameValueCollection x = new NameValueCollection(Request.QueryString); string x1 = x.ToString(); Name
我有一个 NameValueCollection 变量 queryDict。 有些键没有值。但是我想遍历所有键,即使它们没有关联的值。 使用这个,如果值为空,我得到 null 而不是键名: forea
如果不是,确定两个 NameValueCollections 对象是否相等的最佳方法是什么。 我用它来确定解析到名称值集合中的两个查询字符串是否具有相同的键和值,而不管顺序如何。 最佳答案 Equal
我有一种情况,我不能使用字典来收集字符串的名称值。我的名称值对包含重复键,因为我需要支持遗留系统。所以请不要让我停止支持重复键。我尝试使用 NameValueCollection 但这不能与 Data
在我的 web.config 中,我将配置部分分解为单独的文件: 然后我使用 ConfigurationManager 检索设置: NameValueCollection appSettings
在 .NET(反)序列化的幕后发生了什么,使它的行为方式与它在以下场景中的行为方式相同? (这是一个测试应用程序,仅用于说明我的设置。) 我有一个带有 NameValueCollection 属性的类
我是一名优秀的程序员,十分优秀!