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
            };
        }
        private EnumPropertyInfo GetOptionSetEnumInfo(CodeMemberProperty prop, string entityLogicalName)
        {
            var data = CodeWriterFilterService.EntityMetadata[entityLogicalName];
            var propertyLogicalName = (from CodeAttributeDeclaration att in prop.CustomAttributes
                                       where att.AttributeType.BaseType == "Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute"
                                       select((CodePrimitiveExpression)att.Arguments[0].Value).Value.ToString()).FirstOrDefault();

            if (propertyLogicalName == null)
            {
                throw new Exception("Unable to determine property Logical Name");
            }

            var    attribute = data.Attributes.FirstOrDefault(a => a.LogicalName == propertyLogicalName);
            var    picklist  = attribute as EnumAttributeMetadata;
            string specifiedEnum;

            if (picklist == null)
            {
                return(null);
            }

            var enumName = NamingService.GetNameForOptionSet(data, picklist.OptionSet, Services);

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

            return(new EnumPropertyInfo
            {
                EnumTypeName = enumName,
                PropertyName = prop.Name + "Enum",
                LogicalName = propertyLogicalName
            });
        }
        //private IEnumerable<CodeMemberProperty> GetProperties(CodeTypeDeclaration type){
        //    foreach(var member in type.Members){
        //        var property = member as CodeMemberProperty;
        //        if(property != null){
        //            yield return property;
        //        }
        //    }
        //    yield break;
        //}

        private CodeMemberProperty GetOptionSetEnumType(CodeMemberProperty prop, string entityLogicalName)
        {
            var    data = CodeWriterFilterService.EntityMetadata[entityLogicalName];
            string propertyLogicalName = (from CodeAttributeDeclaration att in prop.CustomAttributes
                                          where att.AttributeType.BaseType == "Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute"
                                          select((CodePrimitiveExpression)att.Arguments[0].Value).Value.ToString()).FirstOrDefault();

            if (propertyLogicalName == null)
            {
                throw new Exception("Unable to determine property Logical Name");
            }

            var    attribute = data.Attributes.FirstOrDefault(a => a.LogicalName == propertyLogicalName);
            var    picklist  = attribute as EnumAttributeMetadata;
            string specifiedEnum;

            if (picklist == null)
            {
                return(null);
            }

            var enumName = picklist.OptionSet.Name;

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

            enumName += "?";

            var property = new CodeMemberProperty
            {
                Name       = prop.Name + "Enum",
                Type       = new CodeTypeReference(enumName),
                Attributes = System.CodeDom.MemberAttributes.Public
            };

            // [Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("AttributeLogicalName")]
            property.CustomAttributes.Add(new CodeAttributeDeclaration("Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute", new CodeAttributeArgument(new CodePrimitiveExpression(propertyLogicalName))));

            property.GetStatements.Add(
                new CodeMethodReturnStatement(
                    new CodeCastExpression(
                        enumName,
                        new CodeMethodInvokeExpression(
                            new CodeTypeReferenceExpression("EntityOptionSetEnum"),
                            "GetEnum",
                            new CodeThisReferenceExpression(),
                            new CodePrimitiveExpression(propertyLogicalName)))));

            if (prop.HasSet)
            {
                var setSnippet = IsNullableIntPropery(prop) ? "(int?)value" : "value.HasValue ? new Microsoft.Xrm.Sdk.OptionSetValue((int)value) : null";

                property.SetStatements.Add(
                    new CodeAssignStatement(
                        new CodeVariableReferenceExpression(prop.Name),
                        new CodeSnippetExpression(setSnippet)));
            }
            return(property);
        }