按类型嵌套到 class 的字典
Nested dictionary to class by type
我正在尝试根据其类型将嵌套字典转换为嵌套 class。
例如。我有这本字典:
{
"owner": {
"name": {
"first": "Linh",
"last": "Nguyen"
},
"age": "Nguyen"
},
"power": {
"index": "1",
"range": "5"
}
}
我有这个嵌套 class :
public class AccountRegistrationViewModel
{
/// <summary>
/// Account owner.
/// </summary>
[Required]
public Owner Owner { get; set; }
/// <summary>
/// User age.
/// </summary>
[Required]
public int Age { get; set; }
/// <summary>
/// Account photo.
/// </summary>
[Required]
public List<HttpFile> Photos { get; set; }
}
public class Owner
{
public Name Name { get; set; }
}
public class Name
{
public string First { get; set; }
public string Last { get; set; }
}
我有这个功能:
T Convert<T>(IDictionary<string, object> input, Type classType)
{
// Do something.
return T;
}
在javascript中,完成这个任务很容易,但是目前,我不知道如何使用C#来完成这个任务。
有人可以帮我吗?
谢谢,
对于那些想要将嵌套字典转换为 class 的人。
实际上,我的问题是为 Web multipart/form-data 制作一个新的格式化程序 API 2。我已经尝试了一些为 Web API 2 制作的格式化程序,但它无法处理上传文件时嵌套 class。
https://gist.github.com/Danielku15/bfc568a19b9e58fd9e80
How create a MultipartFormFormatter for ASP.NET 4.5 Web API
Here 是我的替代 multipart/form-data 格式化程序。
我的实现可能不是最好的,但至少我们以后可以改进它。
感谢奇迹来自反射。
我正在尝试根据其类型将嵌套字典转换为嵌套 class。 例如。我有这本字典:
{
"owner": {
"name": {
"first": "Linh",
"last": "Nguyen"
},
"age": "Nguyen"
},
"power": {
"index": "1",
"range": "5"
}
}
我有这个嵌套 class :
public class AccountRegistrationViewModel
{
/// <summary>
/// Account owner.
/// </summary>
[Required]
public Owner Owner { get; set; }
/// <summary>
/// User age.
/// </summary>
[Required]
public int Age { get; set; }
/// <summary>
/// Account photo.
/// </summary>
[Required]
public List<HttpFile> Photos { get; set; }
}
public class Owner
{
public Name Name { get; set; }
}
public class Name
{
public string First { get; set; }
public string Last { get; set; }
}
我有这个功能:
T Convert<T>(IDictionary<string, object> input, Type classType)
{
// Do something.
return T;
}
在javascript中,完成这个任务很容易,但是目前,我不知道如何使用C#来完成这个任务。
有人可以帮我吗?
谢谢,
对于那些想要将嵌套字典转换为 class 的人。
实际上,我的问题是为 Web multipart/form-data 制作一个新的格式化程序 API 2。我已经尝试了一些为 Web API 2 制作的格式化程序,但它无法处理上传文件时嵌套 class。
https://gist.github.com/Danielku15/bfc568a19b9e58fd9e80 How create a MultipartFormFormatter for ASP.NET 4.5 Web API
Here 是我的替代 multipart/form-data 格式化程序。
我的实现可能不是最好的,但至少我们以后可以改进它。 感谢奇迹来自反射。