void Member_New(Member member, NewEventArgs e) { Type typeMemberType = MemberTypeManager.GetMemberTypeType(member.ContentType.Alias); foreach (PropertyInfo propInfo in typeMemberType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { MemberTypePropertyAttribute propAttr = Util.GetAttribute <MemberTypePropertyAttribute>(propInfo); if (propAttr == null) { continue; // skip this property - not part of a Member type } string propertyName; string propertyAlias; MemberTypeManager.ReadPropertyNameAndAlias(propInfo, propAttr, out propertyName, out propertyAlias); if (propAttr.DefaultValue != null) { try { umbraco.cms.businesslogic.property.Property property = member.getProperty(propertyAlias); property.Value = Convert.ChangeType(propAttr.DefaultValue, propInfo.PropertyType); } catch (Exception exc) { throw new Exception(string.Format("Cannot set default value ('{0}') for property {1}.{2}. Error: {3}", propAttr.DefaultValue, typeMemberType.Name, propInfo.Name, exc.Message), exc); } } } }
internal static bool PopuplateInstance <T>(Member member, Type typeMemberType, T typedMember) where T : MemberTypeBase { if (member == null) { return(false); } if (member.ContentType.Alias == null) { throw new Exception(string.Format("Member has no associated member type. Member: id: {0}, login name: {1}", member.Id, member.LoginName)); } typedMember.Id = member.Id; typedMember.Email = member.Email; typedMember.UniqueId = member.UniqueId; typedMember.CreateDate = member.CreateDateTime; typedMember.Source = member; foreach (PropertyInfo propInfo in typeMemberType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { MemberTypePropertyAttribute propAttr = Util.GetAttribute <MemberTypePropertyAttribute>(propInfo); if (propAttr == null || (propInfo.GetGetMethod() != null && propInfo.GetGetMethod().IsVirtual)) { continue; // skip this property - not part of a Document Type or is virtual in which case value will be intercepted } object value = null; try { value = GetPropertyValue(member, propInfo, propAttr); propInfo.SetValue(typedMember, value, null); } catch (Exception exc) { throw new Exception(string.Format("Cannot set the value of a Member type property {0}.{1} (member type: {2}) to value: '{3}' (value type: {4}). Error: {5}", typeMemberType.Name, propInfo.Name, propInfo.PropertyType.FullName, value, value != null ? value.GetType().FullName : "null", exc.Message)); } } return(true); }
internal static object GetPropertyValue(Member member, PropertyInfo propInfo, MemberTypePropertyAttribute propAttr) { object value = null; string propertyName; string propertyAlias; if (propAttr == null) { propAttr = Util.GetAttribute <MemberTypePropertyAttribute>(propInfo); } MemberTypeManager.ReadPropertyNameAndAlias(propInfo, propAttr, out propertyName, out propertyAlias); umbraco.cms.businesslogic.property.Property property = member.getProperty(propertyAlias); if (property == null) { value = null; } else if (propInfo.PropertyType.Equals(typeof(System.Boolean))) { if (property.Value == null || String.IsNullOrEmpty(Convert.ToString(property.Value)) || Convert.ToString(property.Value) == "0") { value = false; } else { value = true; } } else if (ContentHelper.PropertyConvertors.ContainsKey(propInfo.PropertyType)) { // will be transformed later. TODO: move transformation here value = property.Value; } else if (propInfo.PropertyType.IsGenericType && propInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { if (String.IsNullOrEmpty(Convert.ToString(property.Value))) { value = null; } else { value = Convert.ChangeType(property.Value, Nullable.GetUnderlyingType(propInfo.PropertyType)); } // TODO: If data type is DateTime and is nullable and is less than 1.1.1000 than set it to NULL } else { value = Convert.ChangeType(property.Value, propInfo.PropertyType); } if (ContentHelper.PropertyConvertors.ContainsKey(propInfo.PropertyType)) { value = ContentHelper.PropertyConvertors[propInfo.PropertyType].ConvertValueWhenRead(value); } return(value); }