eXpand icon indicating copy to clipboard operation
eXpand copied to clipboard

ImportWizard map Enum value using EnumDescriptor(prop.MemberType).ParseCaption(value)

Open FernandoARamos opened this issue 3 years ago • 1 comments

Hi,

I am using xpand importwizardwin in a XAF win application and I would like to override StringValueMapper to use Caption of enums instead of value. Like this.

How should I proceed this? Creating a controller ?

`public class MyImportWizardWinMapper : Xpand.ExpressApp.ImportWizard.Win.Wizard.StringValueMapper {

    protected override object MapStringToValueType(XPMemberInfo prop, string value, NumberFormatInfo numberFormatInfo = null)
    {
        object result = null;

        if (prop.MemberType.IsEnum)
            //result = Enum.Parse(prop.MemberType, value);
            result = new EnumDescriptor(prop.MemberType).ParseCaption(value);

        else if (prop.MemberType == typeof(char))
            result = Convert.ChangeType(ImportUtils.GetQString(value), prop.MemberType);

        else if (prop.StorageType == typeof(int))
        {
            int number;
            if (value != String.Empty && Int32.TryParse(value, out number))
                result = number;
            else
                result = 0;
        }
        else if (prop.MemberType == typeof(Guid))
            result = new Guid(ImportUtils.GetQString(value));
        else if (prop.StorageType == typeof(DateTime))
        {
            if (value != string.Empty)
            {
                //Include validate
                DateTime dt = DateTime.FromOADate(Convert.ToDouble(value));
                result = dt;
            }
        }
        else if (prop.MemberType == typeof(double))
        {
            double number;

            if (Double.TryParse(value, NumberStyles.Number,
                numberFormatInfo ?? new NumberFormatInfo { NumberDecimalSeparator = "." }, out number))
                result = number;
        }
        else if (prop.MemberType == typeof(bool))
        {
            if (value != string.Empty &&
                (value.Length == 1 || value.ToLower() == @"true" || value.ToLower() == @"false"))
            {
                bool truefalse;
                if (value.ToLower() == @"true" || value.ToLower() == @"false")
                    truefalse = Convert.ToBoolean(value);
                else
                    truefalse = Convert.ToBoolean(Convert.ToInt32(value));
                result = truefalse;
            }
        }
        else
            result = Convert.ChangeType(value, prop.MemberType, CultureInfo.InvariantCulture);
        return result;

    }

}`

Thanks in advance!

Fernando

FernandoARamos avatar Feb 07 '22 03:02 FernandoARamos

this package is a contribution and not authored by me hence I cannot support it.

apobekiaris avatar Feb 07 '22 04:02 apobekiaris

Closing issue for age. Feel free to reopen it at any time.

.Thank you for your contribution.

expand avatar Dec 08 '23 21:12 expand