让asp.net mvc的Action支持jQuery直接提交的javascript对象(6)
来源:未知 责任编辑:责任编辑 发表时间:2014-05-26 11:00 点击:次
return array;
}
return null;
}
}
public class ObjectValueProviderResult
: ValueProviderResult
{
private IDictionary<string, ValueProviderResult> _Properties;
public ObjectValueProviderResult(IDictionary<string, ValueProviderResult> properties)
{
_Properties = properties;
base.RawValue = properties.ToDictionary(v => v.Key, v => v.Value.RawValue);
base.AttemptedValue = "{" + string.Join(", ", properties.Select(v => string.Format("{0}: {1}", v.Key, v.Value.AttemptedValue ))) + "}";
}
public override object ConvertTo(Type type, CultureInfo culture)
{
if (!type.IsPrimitive && !type.IsArray)
{
var constructor = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance).OrderBy(v => v.GetParameters().Length).FirstOrDefault();
if (constructor != null)
{
var args = constructor.GetParameters()
.Where(v => !v.IsOptional)
.Join(_Properties.DefaultIfEmpty(), v => v.Name, v => v.Key, (l, r) => r.Value).ToArray();
var obj = Activator.CreateInstance(type, args);
foreach( var property in type.GetProperties( BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty ))
{
if (property.GetIndexParameters().Length > 0) continue;
ValueProviderResult propertyValue;
if (_Properties.TryGetValue(property.Name, out propertyValue) && propertyValue != null )
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>