public static void UpdateEntityProperties(object source, ref object destination) { PropertyInfo[] properties = source.GetType().GetProperties(); #region Get Key Property PropertyInfo keyProperty = null; object[] attributes = source.GetType().GetCustomAttributes(true); string keyPropertyName = ""; foreach (object attribute in attributes) { if (attribute.GetType().FullName.Contains("Key")) { Microsoft.OData.Client.KeyAttribute key = (Microsoft.OData.Client.KeyAttribute)attribute; keyPropertyName = key.KeyNames.Last(); break; } } keyProperty = source.GetType().GetProperty(keyPropertyName); #endregion Get Key Property var filteredProperties = from p in properties where !p.PropertyType.FullName.Contains("Collection") && !p.PropertyType.FullName.Contains("Byte") && !p.PropertyType.Namespace.Contains("northwindClient") && !p.PropertyType.Namespace.Contains("ArubaClient") && !p.Equals(keyProperty) select p; foreach (PropertyInfo prop in filteredProperties) { prop.SetValue(destination, prop.GetValue(source, null), null); } }
public static object GetSameEntity(object objToMatch, DataServiceContext ctx) { object matchedObject = null; PropertyInfo[] properties = objToMatch.GetType().GetProperties(); bool Ismatch = false; var sameTypeObjects = ctx.Entities.Where(t => t.Entity.GetType().FullName == objToMatch.GetType().FullName); #region Get Key Property PropertyInfo keyProperty = null; object[] attributes = objToMatch.GetType().GetCustomAttributes(true); string keyPropertyName = ""; foreach (object attribute in attributes) { if (attribute.GetType().FullName.Contains("Key")) { Microsoft.OData.Client.KeyAttribute key = (Microsoft.OData.Client.KeyAttribute)attribute; keyPropertyName = key.KeyNames.Last(); break; } } keyProperty = objToMatch.GetType().GetProperty(keyPropertyName); #endregion Get Key Property var filteredProperties = from p in properties where !p.PropertyType.FullName.Contains("Collection") && !p.PropertyType.FullName.Contains("Byte") && !p.PropertyType.Namespace.Contains("northwindClient") && !p.PropertyType.Namespace.Contains("ArubaClient") && !p.Equals(keyProperty) select p; string left, right; foreach (EntityDescriptor entityDescriptor in sameTypeObjects) { if (Ismatch) { break; } foreach (PropertyInfo prop in filteredProperties) { left = prop.GetValue(entityDescriptor.Entity, null) == null ? "" : prop.GetValue(entityDescriptor.Entity, null).ToString(); right = prop.GetValue(objToMatch, null) == null ? "$" : prop.GetValue(objToMatch, null).ToString(); Ismatch = left == right; } if (Ismatch) { matchedObject = entityDescriptor.Entity; } } return(matchedObject); }
public KeyAttWrapper() { this._KeyAttribute = new Microsoft.OData.Client.KeyAttribute(); }
/// <summary> /// Returns the KeyKind if <paramref name="propertyInfo"/> is declared as a key in <paramref name="dataServiceKeyAttribute"/> or it follows the key naming convension. /// </summary> /// <param name="propertyInfo">Property in question.</param> /// <param name="dataServiceKeyAttribute">DataServiceKeyAttribute instance.</param> /// <returns>Returns the KeyKind if <paramref name="propertyInfo"/> is declared as a key in <paramref name="dataServiceKeyAttribute"/> or it follows the key naming convension.</returns> private static KeyKind IsKeyProperty(PropertyInfo propertyInfo, KeyAttribute dataServiceKeyAttribute) { Debug.Assert(propertyInfo != null, "propertyInfo != null"); string propertyName = GetServerDefinedName(propertyInfo); KeyKind keyKind = KeyKind.NotKey; if (dataServiceKeyAttribute != null && dataServiceKeyAttribute.KeyNames.Contains(propertyName)) { keyKind = KeyKind.AttributedKey; } else if (propertyName.EndsWith("ID", StringComparison.Ordinal)) { string declaringTypeName = propertyInfo.DeclaringType.Name; if ((propertyName.Length == (declaringTypeName.Length + 2)) && propertyName.StartsWith(declaringTypeName, StringComparison.Ordinal)) { // matched "DeclaringType.Name+ID" pattern keyKind = KeyKind.TypeNameId; } else if (2 == propertyName.Length) { // matched "ID" pattern keyKind = KeyKind.Id; } } return keyKind; }
public static object GetSameObject(object objToMatch, DataServiceContext ctx, bool onlyKeyMatch) { object matchedObject = null; //Find Key property PropertyInfo[] properties = objToMatch.GetType().GetProperties(); string keyPropertyName = ""; PropertyInfo keyProperty = null; object[] attributes = objToMatch.GetType().GetCustomAttributes(true); foreach (object attribute in attributes) { if (attribute.GetType().FullName.Contains("Key")) { Microsoft.OData.Client.KeyAttribute key = (Microsoft.OData.Client.KeyAttribute)attribute; keyPropertyName = key.KeyNames.Last(); break; } } //foreach (PropertyInfo property in properties) //{ // if (property.Name.ToLower().Equals("id")) // { // } //} //string keyPropertyValue = keyProperty.GetValue(objToMatch, null).ToString(); keyProperty = objToMatch.GetType().GetProperty(keyPropertyName); bool skip = false; if (keyProperty != null) { if (keyProperty.GetValue(objToMatch, null) == null) { return(objToMatch); } string keyPropertyValue = keyProperty.GetValue(objToMatch, null).ToString(); var sameTypeObjects = ctx.Entities.Where(t => t.Entity.GetType().FullName == objToMatch.GetType().FullName); foreach (EntityDescriptor entityDescriptor in sameTypeObjects) { if (skip) { break; } if (keyProperty.GetValue(entityDescriptor.Entity, null).ToString() == keyPropertyValue) { if (onlyKeyMatch) { matchedObject = entityDescriptor.Entity; foreach (PropertyInfo nonKeyProperty in entityDescriptor.Entity.GetType().GetProperties(). Where(prop => prop != keyProperty)) { nonKeyProperty.SetValue(matchedObject, nonKeyProperty.GetValue(objToMatch, null), null); } skip = true; break; } else { var filteredProperties = from p in properties where !p.PropertyType.FullName.Contains("Collection") && !p.PropertyType.FullName.Contains("Byte") && !p.PropertyType.Namespace.Contains("northwindClient") && !p.PropertyType.Namespace.Contains("ArubaClient") && !p.Equals(keyProperty) select p; object left = matchedObject; object right = objToMatch; foreach (PropertyInfo prop in filteredProperties) { left = prop.GetValue(entityDescriptor.Entity, null) == null ? "" : prop.GetValue(entityDescriptor.Entity, null).ToString(); right = prop.GetValue(objToMatch, null) == null ? "$" : prop.GetValue(objToMatch, null).ToString(); if (left == right) { matchedObject = entityDescriptor.Entity; skip = true; break; } } } } } } return(matchedObject); }