Please explain the code
Im trying this sample (http://a33ik.blogspot.in/2012/02/integrating-crm-2011-using-sql.html)Please explain below piece of code in detail thanks a lot in advanceusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace CrmProxy.Crm{ partial class Entity{public Entity(){this.FormattedValuesField =new FormattedValueCollection();this.RelatedEntitiesField =new RelatedEntityCollection();}public T GetAttributeValue<T>(string attributeLogicalName){if(null==this.Attributes){this.Attributes =new AttributeCollection();};object value;if(this.Attributes.TryGetValue(attributeLogicalName,out value)){return(T)value;}returndefault(T);}publicobjectthis[string attributeName]{ get{if(null==this.Attributes){this.Attributes =new AttributeCollection();};returnthis.Attributes.GetItem(attributeName);} set{if(null==this.Attributes){this.Attributes =new AttributeCollection();};this.Attributes.SetItem(attributeName, value);}}}publicstaticclass CollectionExtensions{publicstatic TValue GetItem<TKey, TValue>(this IList<KeyValuePair<TKey, TValue>> collection, TKey key){ TValue value;if(TryGetValue(collection, key,out value)){return value;}thrownew System.Collections.Generic.KeyNotFoundException("Key = "+ key);}publicstaticvoid SetItem<TKey, TValue>(this IList<KeyValuePair<TKey, TValue>> collection, TKey key, TValue value){int index;if(TryGetIndex<TKey, TValue>(collection, key,out index)){ collection.RemoveAt(index);}//If the value is an array, it needs to be converted into a List. This is due to how Silverlight serializes//Arrays and IList<T> objects (they are both serialized with the same namespace). Any collection objects will//already add the KnownType for IList<T>, which means that any parameters that are arrays cannot be added//as a KnownType (or it will throw an exception). Array array = value as Array;if(null!= array){ Type listType =typeof(List<>).GetGenericTypeDefinition().MakeGenericType(array.GetType().GetElementType());object list = Activator.CreateInstance(listType, array);try{ value =(TValue)list;}catch(InvalidCastException){//Don't do the conversion because the types are not compatible}} collection.Add(new KeyValuePair<TKey, TValue>(key, value));}publicstaticbool ContainsKey<TKey, TValue>(this IList<KeyValuePair<TKey, TValue>> collection, TKey key){int index;return TryGetIndex<TKey, TValue>(collection, key,out index);}publicstaticbool TryGetValue<TKey, TValue>(this IList<KeyValuePair<TKey, TValue>> collection, TKey key,out TValue value){int index;if(TryGetIndex<TKey, TValue>(collection, key,out index)){ value = collection[index].Value;returntrue;} value =default(TValue);returnfalse;}privatestaticbool TryGetIndex<TKey, TValue>(IList<KeyValuePair<TKey, TValue>> collection, TKey key,outint index){if(null== collection || null== key){ index =-1;returnfalse;} index =-1;for(int i =0; i < collection.Count; i++){if(key.Equals(collection[i].Key)){ index = i;returntrue;}}returnfalse;}}}
August 18th, 2012 10:53pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics