Exemplo n.º 1
0
        /// <summary>
        /// Creation of object generation information.
        /// </summary>
        /// <param name="type">type.</param>
        /// <returns>ObjectCreateInfo.</returns>
        public static ObjectCreateInfo MakeObjectCreateInfo(Type type)
        {
            lock (_selectedTypeInfo)
            {
                ObjectCreateInfo info;
                if (_selectedTypeInfo.TryGetValue(type, out info))
                {
                    return(info);
                }

                var properties = type.GetPropertiesEx();
                var members    = new ObjectCreateMemberInfo[properties.Length];
                for (int i = 0; i < members.Length; i++)
                {
                    members[i] = new ObjectCreateMemberInfo(properties[i].Name, null);
                }

                info = new ObjectCreateInfo(members, null);
                _selectedTypeInfo[type] = info;
                return(info);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creation of object generation information.
        /// </summary>
        /// <param name="exp">expression.</param>
        /// <returns>ObjectCreateInfo.</returns>
        public static ObjectCreateInfo MakeObjectCreateInfo(Expression exp)
        {
            var select = new List <ObjectCreateMemberInfo>();
            var newExp = exp as NewExpression;

            if (newExp != null)
            {
                for (int i = 0; i < newExp.Arguments.Count; i++)
                {
                    var    propInfo = newExp.Members[i] as PropertyInfo;
                    string name     = null;
                    if (propInfo != null)
                    {
                        name = propInfo.Name;
                    }
                    else
                    {
                        //.net3.5
                        var method = newExp.Members[i] as MethodInfo;
                        name = method.GetPropertyName();
                    }
                    select.Add(new ObjectCreateMemberInfo(name, newExp.Arguments[i]));
                }
                return(new ObjectCreateInfo(select.ToArray(), exp));
            }

            var initExp = exp as MemberInitExpression;

            if (initExp != null)
            {
                var elements = new ObjectCreateMemberInfo[initExp.Bindings.Count];
                for (int i = 0; i < elements.Length; i++)
                {
                    var e = initExp.Bindings[i] as MemberAssignment;
                    elements[i] = new ObjectCreateMemberInfo(e.Member.Name, e.Expression);
                }
                return(new ObjectCreateInfo(elements, exp));
            }

            var member = exp as MemberExpression;

            if (member != null)
            {
                if (SupportedTypeSpec.IsSupported(exp.Type))
                {
                    return(new ObjectCreateInfo(new[] { new ObjectCreateMemberInfo(string.Empty, exp) }, exp));
                }
                Type type = null;
                var  prop = member.Member as PropertyInfo;
                if (prop != null)
                {
                    type = prop.PropertyType;
                }
                else
                {
                    type = ((FieldInfo)member.Member).FieldType;
                }
                return(MakeObjectCreateInfo(type));
            }

            return(new ObjectCreateInfo(new[] { new ObjectCreateMemberInfo(string.Empty, exp) }, exp));
        }