public static T ToStatic <T>(object dynamicObject) { //Create new Instance of the Static var entity = Activator.CreateInstance <T>(); //Convert the dynamicObject as an implemented dictionary var properties = dynamicObject as IDictionary <string, object>; if (properties == null) { return(entity); } //Create Lookup Dictionary to hold static destination class set of FacebookMapping decorated properties List <propertycontainer> PropertyLookup = new List <propertycontainer>(); //Get list of FacebookMapping custom attribute properties decorated within the destination static class var destFBMappingProperties = (from PropertyInfo property in entity.GetType().GetProperties() where property.GetCustomAttributes(typeof(FacebookMapping), true).Length > 0 select property).ToList(); //Loop through the list of Facebook properties to build a dictionary lookup list foreach (PropertyInfo pi in destFBMappingProperties) { foreach (object attribute in pi.GetCustomAttributes(typeof(FacebookMapping))) { FacebookMapping facebookMapAttribute = attribute as FacebookMapping; if (facebookMapAttribute != null) { //string facebookLookupKey = string.Empty; //if (string.IsNullOrEmpty(facebookMapAttribute.parent)) // facebookLookupKey = facebookMapAttribute.GetName(); //else // facebookLookupKey = facebookMapAttribute.parent; //Add new property container with Facebook mapping and property info details from destination class PropertyLookup.Add(new propertycontainer { facebookfield = facebookMapAttribute.GetName(), facebookparent = facebookMapAttribute.parent, facebookMappedProperty = pi }); } } } //Iterate through the dynamic Object's list of properties, looking for match from the facebook mapping lookup foreach (var entry in properties) { var MatchedResults = PropertyLookup.Where(x => x.facebookparent == entry.Key || x.facebookfield == entry.Key); if (MatchedResults != null) { foreach (propertycontainer DestinationPropertyInfo in MatchedResults) { object mappedValue = null; if (entry.Value.GetType().Name == "JsonObject") { //drill down on level to obtain a list of properties from the child set of properties //of the dynamic Object //var childproperties = entry.Value as IDictionary<string, object>; //mappedValue = (from KeyValuePair<string, object> item in childproperties // where item.Key == DestinationPropertyInfo.facebookfield // select item.Value).FirstOrDefault(); mappedValue = FindMatchingChildPropertiesRecursively(entry, DestinationPropertyInfo); //child properity was not matched so apply the parent FacebookJson object as the entry value if (mappedValue == null && DestinationPropertyInfo.facebookfield == entry.Key) { mappedValue = entry.Value; } } else { if (String.IsNullOrEmpty(DestinationPropertyInfo.facebookparent) && DestinationPropertyInfo.facebookfield == entry.Key) { mappedValue = entry.Value; } } //copy mapped value into destination class property if (mappedValue != null) { if (DestinationPropertyInfo.facebookMappedProperty.PropertyType.Name == "DateTime") { DestinationPropertyInfo.facebookMappedProperty.SetValue(entity, System.DateTime.Parse(mappedValue.ToString()), null); } else { DestinationPropertyInfo.facebookMappedProperty.SetValue(entity, mappedValue, null); } } } } } return(entity); }
public static T ToStatic <T>(object dynamicObject) { //create new instance of the static var entity = Activator.CreateInstance <T>(); //convert dynamic object as an implemented directory var properties = dynamicObject as IDictionary <string, object>; if (properties == null) { return(entity); } Dictionary <string, propertycontainer> PropertyLookup = new Dictionary <string, propertycontainer>(); var destFBMapplingProperties = (from PropertyInfo property in entity.GetType().GetProperties() where property.GetCustomAttributes(typeof(FacebookMapping), true).Length > 0 select property).ToList(); foreach (PropertyInfo pi in destFBMapplingProperties) { foreach (object attribute in pi.GetCustomAttributes(typeof(FacebookMapping))) { FacebookMapping facebookMappingAttribute = attribute as FacebookMapping; if (facebookMappingAttribute != null) { string facebookLookupKey = string.Empty; if (string.IsNullOrEmpty(facebookMappingAttribute.parent)) { facebookLookupKey = facebookMappingAttribute.GetName(); } else { facebookLookupKey = facebookMappingAttribute.parent; } try { PropertyLookup.Add(facebookLookupKey, new propertycontainer { facebookfield = facebookMappingAttribute.GetName(), facebookparent = facebookMappingAttribute.parent, facebookMappedProperty = pi }); } catch (Exception) { } } } } foreach (var entry in properties) { if (PropertyLookup.ContainsKey(entry.Key)) { var DestinationPropertyInfo = PropertyLookup[entry.Key]; if (DestinationPropertyInfo != null) { object mappedValue; if (entry.Value.GetType().Name == "JsonObject") { //var childProperties = entry.Value as IDictionary<string, object>; //mappedValue = (from KeyValuePair<string, object> item in childProperties // where item.Key == DestinationPropertyInfo.facebookfield // select item.Value).FirstOrDefault(); mappedValue = FindMatchingChildProperitesRecursively(entry, DestinationPropertyInfo); if (mappedValue == null) { mappedValue = entry.Value; } } else { mappedValue = entry.Value; } if (DestinationPropertyInfo.facebookMappedProperty.PropertyType.Name == "DateTime") { DestinationPropertyInfo.facebookMappedProperty.SetValue(entity, System.DateTime.Parse(mappedValue.ToString()), null); } else { DestinationPropertyInfo.facebookMappedProperty.SetValue(entity, mappedValue.ToString(), null); } } } } return(entity); }