private EnumPropertyInfo GetOptionSetEnumInfo(CodeMemberProperty prop, string entityLogicalName)
        {
            var propertyLogicalName = prop.GetLogicalName();
            if (propertyLogicalName == null) { throw new Exception("Unable to determine property Logical Name"); }

            var data = CodeWriterFilterService.EntityMetadata[entityLogicalName];
            var attribute = data.Attributes.FirstOrDefault(a => a.LogicalName == propertyLogicalName);
            var picklist = attribute as EnumAttributeMetadata;
            if (picklist == null) { return null; }

            var enumName = NamingService.GetNameForOptionSet(data, picklist.OptionSet, Services);
            if (SpecifiedMappings.TryGetValue(entityLogicalName.ToLower() + "." + prop.Name.ToLower(), out var specifiedEnum))
            {
                enumName = specifiedEnum;
            }
            else if (CodeWriterFilterService.EntityMetadata.ContainsKey(enumName) && CodeWriterFilterService.EntityMetadata[enumName].SchemaName == enumName)
            {
                enumName += "Enum";
            }

            return new EnumPropertyInfo
            {
                OptionSetType = enumName,
                IsMultSelect = picklist is MultiSelectPicklistAttributeMetadata,
                PropertyName = prop.Name + "Enum",
                LogicalName = propertyLogicalName
            };
        }
예제 #2
0
        private void AddEnumSet(CodeMemberProperty prop, EnumPropertyInfo info, CodeMemberProperty property)
        {
            if (!prop.HasSet)
            {
                return;
            }

            // this.OnPropertyChanging("PropName");
            property.SetStatements.Add(new CodeMethodInvokeExpression(
                                           new CodeThisReferenceExpression(), "OnPropertyChanging", new CodePrimitiveExpression(prop.Name)
                                           ));

            CodeExpression getValueToSetExpression;

            if (info.IsMultSelect)
            {
                //EntityOptionSetEnum.GetMultiEnum(this, info.LogicalName, value)
                getValueToSetExpression =
                    new CodeMethodInvokeExpression(
                        CreateBaseClasses
                            ? new CodeTypeReferenceExpression(EntityBaseClassGenerator.BaseEntityName)
                            : new CodeTypeReferenceExpression("EntityOptionSetEnum"),
                        "GetMultiEnum",
                        new CodeThisReferenceExpression(),
                        new CodePrimitiveExpression(info.LogicalName),
                        new CodePropertySetValueReferenceExpression());
            }
            else
            {
                getValueToSetExpression = new CodeSnippetExpression(
                    IsNullableIntProperty(prop)
                        ? "(int?)value"
                        : "value.HasValue ? new Microsoft.Xrm.Sdk.OptionSetValue((int)value) : null");
            }

            // this.SetAttributeValue("logicalName", getValueExpression);
            property.SetStatements.Add(
                new CodeMethodInvokeExpression(
                    new CodeThisReferenceExpression(),
                    "SetAttributeValue",
                    new CodePrimitiveExpression(prop.GetLogicalName()),
                    getValueToSetExpression));

            // this.OnPropertyChanged("PropName");
            property.SetStatements.Add(new CodeMethodInvokeExpression(
                                           new CodeThisReferenceExpression(), "OnPropertyChanged", new CodePrimitiveExpression(prop.Name)
                                           ));
        }