private CodeTypeReference BuildCodeTypeReferenceForOptionSet(
            string attributeName
            , EntityMetadata entityMetadata
            , AttributeMetadata attributeMetadata
            , OptionSetMetadata attributeOptionSet
            , ICodeGenerationServiceProvider iCodeGenerationServiceProvider
            )
        {
            if (iCodeGenerationServiceProvider.CodeWriterFilterService.GenerateOptionSet(attributeOptionSet, attributeMetadata, iCodeGenerationServiceProvider))
            {
                string nameForOptionSet = iCodeGenerationServiceProvider.NamingService.GetNameForOptionSet(entityMetadata, attributeOptionSet, iCodeGenerationServiceProvider);

                CodeGenerationType typeForOptionSet = iCodeGenerationServiceProvider.CodeGenerationService.GetTypeForOptionSet(entityMetadata, attributeOptionSet, iCodeGenerationServiceProvider);

                switch (typeForOptionSet)
                {
                case CodeGenerationType.Class:
                    return(this.TypeRef(nameForOptionSet));

                case CodeGenerationType.Enum:
                case CodeGenerationType.Struct:
                    return(TypeMappingService.TypeRef(typeof(Nullable <>), this.TypeRef(nameForOptionSet)));
                }
            }
            return(TypeMappingService.TypeRef(typeof(object)));
        }
        private CodeTypeReference GetTypeForField(string clrFormatter, bool isGeneric)
        {
            CodeTypeReference codeTypeReference = TypeMappingService.TypeRef(typeof(object));

            if (isGeneric)
            {
                codeTypeReference = new CodeTypeReference(new CodeTypeParameter("T"));
            }
            else if (!string.IsNullOrEmpty(clrFormatter))
            {
                Type type = Type.GetType(clrFormatter, false);
                if (type != null)
                {
                    codeTypeReference = TypeMappingService.TypeRef(type);
                }
                else
                {
                    string[] strArray = clrFormatter.Split(new char[1] {
                        ','
                    }, StringSplitOptions.RemoveEmptyEntries);

                    if (strArray != null && strArray.Length > 0)
                    {
                        codeTypeReference = new CodeTypeReference(strArray[0]);
                    }
                }
            }
            return(codeTypeReference);
        }
        public CodeTypeReference GetTypeForAttributeType(
            EntityMetadata entityMetadata
            , AttributeMetadata attributeMetadata
            , ICodeGenerationServiceProvider iCodeGenerationServiceProvider
            )
        {
            Type type = typeof(object);

            if (attributeMetadata.AttributeType.HasValue)
            {
                AttributeTypeCode key = attributeMetadata.AttributeType.Value;

                if (key == AttributeTypeCode.PartyList)
                {
                    return(this.BuildCodeTypeReferenceForPartyList(iCodeGenerationServiceProvider));
                }

                if (_attributeTypeMapping.ContainsKey(key))
                {
                    type = _attributeTypeMapping[key];
                }
                else if (attributeMetadata is ImageAttributeMetadata)
                {
                    type = typeof(byte[]);
                }
                else if (attributeMetadata is MultiSelectPicklistAttributeMetadata)
                {
                    type = typeof(OptionSetValueCollection);
                }
                //else
                //{
                //    OptionSetMetadataBase attributeOptionSet = TypeMappingService.GetAttributeOptionSet(attributeMetadata);
                //    if (attributeOptionSet != null)
                //    {
                //        CodeTypeReference codeTypeReference = this.BuildCodeTypeReferenceForOptionSet(attributeMetadata.LogicalName, entityMetadata, attributeMetadata, attributeOptionSet, iCodeGenerationServiceProvider);
                //        if (!codeTypeReference.BaseType.Equals("System.Object"))
                //        {
                //            return codeTypeReference;
                //        }

                //        if (key.Equals(AttributeTypeCode.Picklist) || key.Equals(AttributeTypeCode.Status))
                //        {
                //            type = typeof(OptionSetValue);
                //        }
                //    }
                //}
            }

            if (type.IsValueType)
            {
                type = typeof(Nullable <>).MakeGenericType(type);
            }

            return(TypeMappingService.TypeRef(type));
        }
        private CodeTypeReference BuildCodeTypeReferenceForPartyList(ICodeGenerationServiceProvider iCodeGenerationServiceProvider)
        {
            EntityMetadata entityMetadata = iCodeGenerationServiceProvider.MetadataProviderService.GetEntityMetadata("activityparty");

            if (entityMetadata != null)
            {
                if (!iCodeGenerationServiceProvider.CodeWriterFilterService.GenerateEntity(entityMetadata, iCodeGenerationServiceProvider))
                {
                    entityMetadata = null;
                }
            }

            if (entityMetadata == null)
            {
                return(TypeMappingService.TypeRef(typeof(IEnumerable <>), TypeMappingService.TypeRef(typeof(Entity))));
            }

            return(TypeMappingService.TypeRef(typeof(IEnumerable <>), this.TypeRef(iCodeGenerationServiceProvider.NamingService.GetNameForEntity(entityMetadata, iCodeGenerationServiceProvider))));
        }