将IDictionary <string,object>转换为class

use*_*160 -3 .net c# asp.net-mvc

我有个问题.如何转换IDictionary<string, object>为课程?以及创建用户帐户.

WebSecurity.CreateUserAndAccount(UserName, Password, new { eny = 0 , eny2 = "sas"});
Run Code Online (Sandbox Code Playgroud)

我有

void Create(String name, IDictionary<string, object> values)) {...}
Run Code Online (Sandbox Code Playgroud)

和班级

 class test {
    String name,
    int eny,
    int eny2
  }
Run Code Online (Sandbox Code Playgroud)

我想用字典中的数据进行对象测试.

L.B*_*L.B 32

我猜猜想

Dictionary<string, object> values = new Dictionary<string, object>()
{
    {"Name","Joe"},{"Id",123}
};

var test = GetObject<TestClass>(values);
Run Code Online (Sandbox Code Playgroud)
class TestClass
{
    public string Name { get; set; }
    public int Id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)