private CodeTypeReference GetTypeForField(string clrFormatter, bool isGeneric) { var result = TypeMappingService.TypeRef(typeof(object)); if (isGeneric) { result = new CodeTypeReference(new CodeTypeParameter("T")); } else if (!string.IsNullOrEmpty(clrFormatter)) { var type = Type.GetType(clrFormatter, false); if (type != null) { result = TypeMappingService.TypeRef(type); } else { var array = clrFormatter.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (array.Length != 0) { result = new CodeTypeReference(array[0]); } } } return(result); }
private async Task <CodeTypeReference> BuildCodeTypeReferenceForPartyListAsync(IServiceProvider services) { var filterService = (ICodeWriterFilterService)services?.GetService(typeof(ICodeWriterFilterService)); var namingService = (INamingService)services?.GetService(typeof(INamingService)); var entityMetadata = (await((IMetadataProviderService)services?.GetService(typeof(IMetadataProviderService)))?.LoadMetadataAsync()).Entities.FirstOrDefault((EntityMetadata entity) => string.Equals(entity.LogicalName, "activityparty", StringComparison.Ordinal) && filterService?.GenerateEntityAsync(entity, services).GetAwaiter().GetResult() == true); if (entityMetadata == null) { return(TypeMappingService.TypeRef(typeof(IEnumerable <>), TypeMappingService.TypeRef(typeof(Entity)))); } return(TypeMappingService.TypeRef(typeof(IEnumerable <>), this.TypeRef(await namingService?.GetNameForEntityAsync(entityMetadata, services)))); }
async Task <CodeTypeReference> ITypeMappingService.GetTypeForAttributeTypeAsync(EntityMetadata entityMetadata, AttributeMetadata attributeMetadata, IServiceProvider services) { var type = typeof(object); if (attributeMetadata.AttributeType != null) { var value = attributeMetadata.AttributeType.Value; if (this._attributeTypeMapping.ContainsKey(value)) { type = this._attributeTypeMapping[value]; } else { if (value == AttributeTypeCode.PartyList) { return(await this.BuildCodeTypeReferenceForPartyListAsync(services)); } if (attributeMetadata is ImageAttributeMetadata) { type = typeof(byte[]); } else { var attributeOptionSet = TypeMappingService.GetAttributeOptionSet(attributeMetadata); if (attributeOptionSet != null) { var codeTypeReference = await this.BuildCodeTypeReferenceForOptionSetAsync(attributeMetadata.LogicalName, entityMetadata, attributeOptionSet, services); if (!codeTypeReference.BaseType.Equals("System.Object")) { return(codeTypeReference); } if (value.Equals(AttributeTypeCode.Picklist) || value.Equals(AttributeTypeCode.Status)) { type = typeof(OptionSetValue); if (type.IsValueType) { type = typeof(Nullable <>).MakeGenericType(type); } } } } } if (type.IsValueType) { type = typeof(Nullable <>).MakeGenericType(type); } } return(TypeMappingService.TypeRef(type)); }
private async Task <CodeTypeReference> BuildCodeTypeReferenceForOptionSetAsync(string attributeName, EntityMetadata entityMetadata, OptionSetMetadataBase attributeOptionSet, IServiceProvider services) { var codeWriterFilterService = (ICodeWriterFilterService)services?.GetService(typeof(ICodeWriterFilterService)); var namingService = (INamingService)services?.GetService(typeof(INamingService)); var codeGenerationService = (ICodeGenerationService)services?.GetService(typeof(ICodeGenerationService)); if (codeWriterFilterService != null && await codeWriterFilterService.GenerateOptionSetAsync(attributeOptionSet, services)) { var nameForOptionSet = await namingService.GetNameForOptionSetAsync(entityMetadata, attributeOptionSet, services); var typeForOptionSet = await codeGenerationService.GetTypeForOptionSetAsync(entityMetadata, attributeOptionSet, services); if (typeForOptionSet == CodeGenerationType.Class) { return(this.TypeRef(nameForOptionSet)); } if (typeForOptionSet == CodeGenerationType.Enum || typeForOptionSet == CodeGenerationType.Struct) { return(TypeMappingService.TypeRef(typeof(Nullable <>), this.TypeRef(nameForOptionSet))); } await CrmSvcUtil.CrmSvcUtilLogger.TraceWarningAsync("Cannot map type for atttribute {0} with OptionSet type {1} which has CodeGenerationType {2}", attributeName, attributeOptionSet.Name, typeForOptionSet); } return(TypeMappingService.TypeRef(typeof(object))); }