Javascript对象映射

时间:2014-07-03 06:57:08

标签: javascript jquery

我有一个c#方法,可以像这样从源代码转换为目标对象

public static object ToMapping(this object source, object destination)
        {
            //loop through the properties of the object you want to covert:          
            foreach (PropertyInfo pi in source.GetType().GetProperties())
            {
                try
                {
                    //get the value of property and try 
                    //to assign it to the property of destination object:
                    if (pi != null && pi.CanRead && pi.CanWrite)
                    {
                        var val = pi.GetValue(source, null);
                        if (val == null)
                            if (pi.PropertyType != typeof(Nullable))
                                continue;
                        destination.GetType().GetProperty(pi.Name).SetValue(destination, pi.GetValue(source, null), null);
                    }
                }
                catch { }
            }
            //return the T type object:         
            return destination;
        }

有没有办法将此方法从c#转换为java脚本?感谢。

1 个答案:

答案 0 :(得分:0)

var objA = {a: 1, b: 2}; // your source JS object
var objB = {};           // your destination JS object
for(var key in objA) {
    objB[key] = objA[key];
}
console.log(objA);
console.log(objB);