private void TransformOptionSets(CodeMemberProperty member, EntityCacheItem entity, AttributeCacheItem attribute) { AttributeMetadata attributeMetadata = attribute.Metadata; if (entity != null && attributeMetadata != null) { if (attributeMetadata is EnumAttributeMetadata) { string typeName; EnumAttributeMetadata enumMetadata = attributeMetadata as EnumAttributeMetadata; OptionSetCacheItem optionSet = DynamicsMetadataCache.OptionSets.GetBy(entity.LogicalName, enumMetadata.OptionSet.Name); if (optionSet == null) { optionSet = DynamicsMetadataCache.OptionSets.GetBy("*", enumMetadata.OptionSet.Name); } if (optionSet != null) { typeName = optionSet.GeneratedTypeName; } else { var namingService = (INamingService)serviceProvider.GetService(typeof(INamingService)); typeName = namingService.GetNameForOptionSet(entity.Metadata, enumMetadata.OptionSet, serviceProvider); } if (!this.addedEnums.ContainsKey(typeName)) { this.addedEnums.Add(typeName, enumMetadata); } FixEnums(member, enumMetadata, typeName); } else { member.Type = new CodeTypeReference("int?"); } } }
private static void TransformOptionSets(CodeTypeMember member, EntityCacheItem entity, AttributeCacheItem attribute) { var codeProperty = (CodeMemberProperty)member; if (member.Name.ToLower() == "statecode" || codeProperty.Type.BaseType != "Microsoft.Xrm.Sdk.OptionSetValue") { return; } OptionSetCacheItem optionSet = null; AttributeMetadata attributeMetadata = attribute.Metadata; if (entity != null && attributeMetadata != null) { if (attributeMetadata is EnumAttributeMetadata) { FixEnums(codeProperty, (EnumAttributeMetadata)attributeMetadata, optionSet); } else { codeProperty.Type = new CodeTypeReference("int?"); } } }
private static void FixEnums(CodeMemberProperty codeProperty, EnumAttributeMetadata listAttribute, OptionSetCacheItem optionSet) { //TODO: refator this method to also work in VB or F# codeProperty.Type = new CodeTypeReference(optionSet.GeneratedTypeName + "?"); if (codeProperty.HasSet) { if (codeProperty.SetStatements[1].GetType() == typeof(CodeConditionStatement)) { ((CodeConditionStatement)codeProperty.SetStatements[1]).FalseStatements[0] = new CodeSnippetStatement { Value = string.Format( "\t\t\t\tthis.SetAttributeValue(\"{0}\", (value.HasValue ? new Microsoft.Xrm.Sdk.OptionSetValue((int)value.Value) : null));", listAttribute.LogicalName) }; } else { codeProperty.SetStatements[1] = new CodeSnippetStatement( string.Format( "\t\t\t\t\tthis.SetAttributeValue(\"{0}\", (value.HasValue ? new Microsoft.Xrm.Sdk.OptionSetValue((int)value.Value) : null));", listAttribute.LogicalName)); } } if (codeProperty.HasGet) { codeProperty.GetStatements.Clear(); codeProperty.GetStatements.Add(new CodeSnippetExpression( string.Format( "var ret = this.GetAttributeValue<Microsoft.Xrm.Sdk.OptionSetValue>(\"{1}\");" + Environment.NewLine + "\t\t\t\treturn (ret!=null ? ({0}?)ret.Value : ({0}?)null);", optionSet.GeneratedTypeName, listAttribute.LogicalName) )); } }