转换IDictionary< string,object>上课 [英] Convert IDictionary<string, object> to class
本文介绍了转换IDictionary< string,object>上课的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题.如何将IDictionary<string, object>
转换为class?以及创建用户帐户.
I have a question. How to convert IDictionary<string, object>
to class? As well as the create user account.
WebSecurity.CreateUserAndAccount(UserName, Password, new { eny = 0 , eny2 = "sas"});
我有
void Create(String name, IDictionary<string, object> values)) {...}
和班级
class test {
String name,
int eny,
int eny2
}
我想用字典中的数据进行对象测试.
I want to have a object test with data from dictionary.
推荐答案
我会猜测
Dictionary<string, object> values = new Dictionary<string, object>()
{
{"Name","Joe"},{"Id",123}
};
var test = GetObject<TestClass>(values);
class TestClass
{
public string Name { get; set; }
public int Id { get; set; }
}
T GetObject<T>(Dictionary<string,object> dict)
{
Type type = typeof(T);
var obj = Activator.CreateInstance(type);
foreach (var kv in dict)
{
type.GetProperty(kv.Key).SetValue(obj, kv.Value);
}
return (T)obj;
}
这篇关于转换IDictionary< string,object>上课的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文