首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的Enum属性在http-response中被更改?

为什么我的Enum属性在http-response中被更改?
EN

Stack Overflow用户
提问于 2018-10-01 16:45:21
回答 1查看 74关注 0票数 0

我有一个web-service,并有以下代码:

代码语言:javascript
复制
roomStay = new RoomStay
            {
                PlaceType = stay.hotelInfo.propertyType,
                GeneralDescription = GenerateGeneralDescription(stay.hotelInfo).Result,
                IsRefundableRoom = !stay.nonRefundable,
                ProviderType = Supplier.BetterBooking,
                BasicPropertyInfo = new BasicPropertyInfo
                {
                    SupplierCode = stay.hotelInfo?.hotelName + stay.hotelInfo?.areaName
                                                          ,
                    Images = imageList,
                    NewImages = newImageList.ToArray()
                                                          ,
                    Address = $"{area + address + city + countryName  }" //$"{hotelStaticDataResult.hotelInfo?.address?.countryName} {hotelStaticDataResult.hotelInfo?.address?.cityName} {string.Join(" ", hotelStaticDataResult.hotelInfo?.address?.addressLines.ToArray())}"
                                                          ,
                    City = ""//cityName
                                                          ,
                    Name = stay.hotelInfo.hotelName
                                                          ,
                    Facilities = new List<FacilityCategory>()//facilitieList
                                                            ,
                    Longitude = longitude//hotelStaticDataResult.hotelInfo?.position.longitude
                                                            ,
                    Latitude = latitude// hotelStaticDataResult.hotelInfo?.position.latitude
                                                            ,
                    Telephone = string.Empty//hotelStaticDataResult?.hotelInfo?.contactNumbers?.Where(a => a.phoneType == "Phone").FirstOrDefault()?.phoneNumber
                                                            ,
                    Description = string.Empty,
                    TextItems = stay.hotelInfo?.textItems.Select(a => new TextItemsModelOTA()
                    {
                        Title = a.title == null || string.IsNullOrEmpty(a.title) ? string.Empty : a.title,
                        Description = a.description == null || string.IsNullOrEmpty(a.description) ? string.Empty : a.description,
                        TitleEnumType = (TitleEnumType)Utility.GetEnum<TitleEnumType>(a.title).Result
                    })//hotelStaticDataResult.hotelInfo?.textItems.ToJson()
                                                            ,
                    Code = hotelKey// searchrs.Result.transactionId + "$" + searchrs.Result.searchId + "$" + stay.stayId + "$" + stay.hotelInfo.hotelName
                                                            ,
                    RateProviderKeyValues = new List<RateProviderKeyValue>() { new RateProviderKeyValue() { Key = decimal.Parse(stay.hotelInfo.starRating.ToStringSafe()), Value = RateProvider.AAA } }.ToArray()

                },
                RoomRates = roomRate
                                                ,
            };

TextItem属性中,我具有此类型类似于枚举的TextEnumType属性:

代码语言:javascript
复制
public enum TitleEnumType
    {
        Facilities = 0,
        Rooms = 1,
        Meals = 2,
        Payment = 3,
        PropertyDescription = 4,
        NumberOfRooms = 5,
        CheckInTime = 6,
        CheckOutTime = 7,
        HotelRemarks = 8,
        None = 9
    }

在下面的两个方法中,我使用GetStringForEnum(string model)方法获得字符串,并使用GetEnum<T>(string model)转换为Enum,如下所示:

代码语言:javascript
复制
public static int GetEnum<T>(string model)
    {
        var newModel = GetStringForEnum(model);

        if (!Enum.IsDefined(typeof(T), newModel))
        {
            return (int)Enum.Parse(typeof(T), "None", true);

        }

        return (int)Enum.Parse(typeof(T), newModel, true);
    }

    private static string GetStringForEnum(string model)
    {
        Regex rgx = new Regex("[^a-zA-Z0-9 -]");
        var nonAlphanumericData = rgx.Matches(model);
        if (nonAlphanumericData.Count < 0)
        {
            return model;
        }
        foreach (var item in nonAlphanumericData)
        {
            model = model.Replace((string)item, "");
        }
        return model;
    }

注释:-- GetEnum<T>(string model)方法将枚举生成为true。

在生成roomStay模型之后,在发送此roomStay http-response之前,我的所有数据都是正确的。但是当我在我的网站和controller中收到这个roomStay时,这个roomStay是不正确的。

我的问题是什么?

EN

回答 1

Stack Overflow用户

发布于 2018-10-01 16:54:01

这是因为调用获取枚举的方法是异步的。因此,在返回RoomStay对象时,TextItems.TitleEnumType是默认的0。尝试:

代码语言:javascript
复制
public static GetEnum<T>(string model)
    {
            var newModel = GetStringForEnum(model);

            if (!Enum.IsDefined(typeof(T), newModel.Result))
            {
                var test1 = (int)Enum.Parse(typeof(T), "None", true);
                return test1;
            }

            var test = (int)Enum.Parse(typeof(T), newModel.Result, true);
            return test;
    }

    private static GetStringForEnum(string model)
    {
            Regex rgx = new Regex("[^a-zA-Z0-9 -]");
            var nonAlphanumericData = rgx.Matches(model);
            if (nonAlphanumericData.Count < 0)
            {
                return model;
            }
            foreach (var item in nonAlphanumericData)
            {
                model = model.Replace((string)item, "");
            }
            return model;
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52595439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档