private static string GetPropertySetAttribute(PropertyInfo prop, List <UsingStatement> usings)
        {
            // Handle null values by resetting to default value
            var resetValueParameterExpression = BindablePropertyExistsForProp(prop)
                ? $"{prop.Name}DefaultValue"
                : string.Empty;

            var formattedValue = string.Empty;

            if (TypeToAttributeHelperSetter.TryGetValue(prop.PropertyType, out var propValueFormat))
            {
                var resetValueParameterExpressionAsExtraParameter = string.Empty;
                if (!string.IsNullOrEmpty(resetValueParameterExpression))
                {
                    resetValueParameterExpressionAsExtraParameter = ", " + resetValueParameterExpression;
                }
                formattedValue = string.Format(CultureInfo.InvariantCulture, propValueFormat, resetValueParameterExpressionAsExtraParameter);
            }
            else if (prop.PropertyType.IsEnum)
            {
                var resetValueParameterExpressionAsExtraParameter = string.Empty;
                if (!string.IsNullOrEmpty(resetValueParameterExpression))
                {
                    resetValueParameterExpressionAsExtraParameter = ", (int)" + resetValueParameterExpression;
                }
                var castTypeName = GetTypeNameAndAddNamespace(prop.PropertyType, usings);
                formattedValue = $"({castTypeName})AttributeHelper.GetInt(attributeValue{resetValueParameterExpressionAsExtraParameter})";
            }
            else if (prop.PropertyType == typeof(string))
            {
                formattedValue =
                    string.IsNullOrEmpty(resetValueParameterExpression)
                    ? "(string)attributeValue"
                    : string.Format(CultureInfo.InvariantCulture, "(string)attributeValue ?? {0}", resetValueParameterExpression);
            }
            else
            {
                // TODO: Error?
                Console.WriteLine($"WARNING: Couldn't generate property set for {prop.DeclaringType.Name}.{prop.Name}");
            }

            return($@"                case nameof({GetNamespacePrefix(prop.DeclaringType, usings)}{prop.DeclaringType.Name}.{GetIdentifierName(prop.Name)}):
                    {prop.DeclaringType.Name}Control.{GetIdentifierName(prop.Name)} = {formattedValue};
                    break;
");
        }
예제 #2
0
        private static string GetPropertySetAttribute(PropertyInfo prop, List <UsingStatement> usings)
        {
            // Handle null values by resetting to default value
            var bindablePropertyForProp = GetBindablePropertyForProp(prop);
            var declaredDefaultValue    = bindablePropertyForProp.DefaultValue;
            var defaultValueForType     = GetDefaultValueForType(prop.PropertyType);
            var needsCustomResetValue   = declaredDefaultValue == null ? false : !declaredDefaultValue.Equals(defaultValueForType);

            var resetValueParameterExpression = string.Empty;

            if (needsCustomResetValue)
            {
                resetValueParameterExpression = ", " + GetValueExpression(declaredDefaultValue);
            }

            var formattedValue = string.Empty;

            if (TypeToAttributeHelperSetter.TryGetValue(prop.PropertyType, out var propValueFormat))
            {
                formattedValue = string.Format(CultureInfo.InvariantCulture, propValueFormat, resetValueParameterExpression);
            }
            else if (prop.PropertyType.IsEnum)
            {
                var castTypeName = GetTypeNameAndAddNamespace(prop.PropertyType, usings);
                formattedValue = $"({castTypeName})AttributeHelper.GetInt(attributeValue{resetValueParameterExpression})";
            }
            else
            {
                // TODO: Error?
            }

            return($@"                case nameof(XF.{prop.DeclaringType.Name}.{prop.Name}):
                    {prop.DeclaringType.Name}Control.{prop.Name} = {formattedValue};
                    break;
");
        }
예제 #3
0
        private static string GetPropertySetAttribute(PropertyInfo prop, List <UsingStatement> usings)
        {
            // Handle null values by resetting to default value
            var resetValueParameterExpression = string.Empty;
            var bindablePropertyForProp       = GetBindablePropertyForProp(prop);

            if (bindablePropertyForProp != null)
            {
                var declaredDefaultValue  = bindablePropertyForProp.DefaultValue;
                var defaultValueForType   = GetDefaultValueForType(prop.PropertyType);
                var needsCustomResetValue = declaredDefaultValue == null ? false : !declaredDefaultValue.Equals(defaultValueForType);

                if (needsCustomResetValue)
                {
                    var valueExpression = GetValueExpression(declaredDefaultValue, usings);
                    if (string.IsNullOrEmpty(valueExpression))
                    {
                        Console.WriteLine($"WARNING: Couldn't get value expression for {prop.DeclaringType.Name}.{prop.Name} of type {prop.PropertyType.FullName}.");
                    }
                    resetValueParameterExpression = valueExpression;
                }
            }

            var formattedValue = string.Empty;

            if (TypeToAttributeHelperSetter.TryGetValue(prop.PropertyType, out var propValueFormat))
            {
                var resetValueParameterExpressionAsExtraParameter = string.Empty;
                if (!string.IsNullOrEmpty(resetValueParameterExpression))
                {
                    resetValueParameterExpressionAsExtraParameter = ", " + resetValueParameterExpression;
                }
                formattedValue = string.Format(CultureInfo.InvariantCulture, propValueFormat, resetValueParameterExpressionAsExtraParameter);
            }
            else if (prop.PropertyType.IsEnum)
            {
                var resetValueParameterExpressionAsExtraParameter = string.Empty;
                if (!string.IsNullOrEmpty(resetValueParameterExpression))
                {
                    resetValueParameterExpressionAsExtraParameter = ", (int)" + resetValueParameterExpression;
                }
                var castTypeName = GetTypeNameAndAddNamespace(prop.PropertyType, usings);
                formattedValue = $"({castTypeName})AttributeHelper.GetInt(attributeValue{resetValueParameterExpressionAsExtraParameter})";
            }
            else if (prop.PropertyType == typeof(string))
            {
                formattedValue =
                    string.IsNullOrEmpty(resetValueParameterExpression)
                    ? "(string)attributeValue"
                    : string.Format(CultureInfo.InvariantCulture, "(string)attributeValue ?? {0}", resetValueParameterExpression);
            }
            else
            {
                // TODO: Error?
                Console.WriteLine($"WARNING: Couldn't generate property set for {prop.DeclaringType.Name}.{prop.Name}");
            }

            return($@"                case nameof(XF.{prop.DeclaringType.Name}.{GetIdentifierName(prop.Name)}):
                    {prop.DeclaringType.Name}Control.{GetIdentifierName(prop.Name)} = {formattedValue};
                    break;
");
        }