当前位置: 首页>>代码示例>>C#>>正文


C# IKey.GetType方法代码示例

本文整理汇总了C#中IKey.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# IKey.GetType方法的具体用法?C# IKey.GetType怎么用?C# IKey.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IKey的用法示例。


在下文中一共展示了IKey.GetType方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ValidateDtoData

        protected static Collection<Dictionary<string, string>> ValidateDtoData(IKey dto, EntityBase entity)
        {
            var retVal = new Collection<Dictionary<string, string>>();
            var fieldName = string.Empty;
            foreach (var inputField in dto.GetType().GetProperties().Where(x => x.Name != "Key" && x.GetValue(dto) != null)) {
                if (inputField.Name.StartsWith("Start_")) {
                    fieldName = inputField.Name.Substring(6);
                } else if(inputField.Name.StartsWith("End_")){
                    fieldName = inputField.Name.Substring(4);
                } else if(inputField.Name.Contains("__")) {
                    //  using field from a different table
                    //      not currently handling this case
                } else {
                    fieldName = inputField.Name;
                }
                //  insure the entity contains the field represented by fieldName
                var entityFieldName = entity.GetType().GetProperty(fieldName);
                if (entityFieldName == null || entityFieldName.Name.Length == 0) {
                    //  entity does not contain this field
                    continue;
                }
                if (entity.GetDataType(fieldName) == typeof(string)) {
                    //  should be ok
                    continue;
                }
                if (entity.GetDataType(fieldName) == typeof(int) || entity.GetDataType(fieldName) == typeof(int?)) {
                    var nbr = 0;
                    if (!int.TryParse(inputField.GetValue(dto).ToString(), out nbr)) {

                        var dic = new Dictionary<string, string> {{inputField.Name, "Invalid data type cast"}};
                        retVal.Add(dic);
                    }
                    if (entity.GetDataType(fieldName) == typeof(bool) || entity.GetDataType(fieldName) == typeof(bool?)) {
                        if (inputField.GetValue(dto).ToString().ToLower() != "true" && inputField.GetValue(dto).ToString().ToLower() != "false") {
                            var dic = new Dictionary<string, string> { { inputField.Name, "Invalid data type cast" } };
                            retVal.Add(dic);
                        }
                    }
                    if (entity.GetDataType(fieldName) == typeof(DateTime) || entity.GetDataType(fieldName) == typeof(DateTime?)) {
                        DateTime dt;
                        if (!DateTime.TryParse(inputField.GetValue(dto).ToString(), out dt)) {
                            var dic = new Dictionary<string, string> { { inputField.Name, "Invalid data type cast" } };
                            retVal.Add(dic);
                        }
                    }
                    if (entity.GetDataType(fieldName) == typeof(decimal) || entity.GetDataType(fieldName) == typeof(decimal?)) {
                        decimal d;
                        if (!decimal.TryParse(inputField.GetValue(dto).ToString(), out d)) {
                            var dic = new Dictionary<string, string> { { inputField.Name, "Invalid data type cast" } };
                            retVal.Add(dic);
                        }
                    }
                }
            }
            return retVal;
        }
开发者ID:Stimulant-Software,项目名称:HarvestSelect,代码行数:56,代码来源:BaseApiController.cs

示例2: ProcessRecord

        /// <summary>
        /// conversion function to transform string values from parameter 
        /// object received by a Web API into an object that can be
        /// saved or updated in the database
        /// </summary>        
        public void ProcessRecord(IKey dto)
        {
            foreach (var inputField in dto.GetType().GetProperties().Where(x => x.Name != "Key" && x.GetValue(dto) != null)) {
                var fieldName = string.Empty;
                Type type;
                if (inputField.Name.StartsWith("Start_")) {
                    fieldName = inputField.Name.Substring(6);
                } else if (inputField.Name.StartsWith("End_")) {
                    fieldName = inputField.Name.Substring(4);
                } else if (inputField.Name.Contains("__")) {
                    //  this represents a field in another table, we aren't going
                    //      to attempt handling recursive searching at this time
                    continue;
                } else {
                    fieldName = inputField.Name;
                }

                if (fieldName != this.KeyName()) {
                    var matchingField = this.GetType().GetProperty(fieldName);
                    if (matchingField == null) {
                        //  represents a field in another object used in searching
                        continue;
                    }
                    type = GetDataType(fieldName);
                    if (type == typeof(string)) {
                        var value = inputField.GetValue(dto).ToString();
                        matchingField.SetValue(this, value);
                        continue;
                    }
                    if (type == typeof(int) || type == typeof(int?)) {
                        var value = int.Parse(inputField.GetValue(dto).ToString());
                        matchingField.SetValue(this, value);
                        continue;
                    }

                    if (type == typeof(bool) || type == typeof(bool?)) {
                        var value = bool.Parse(inputField.GetValue(dto).ToString());
                        matchingField.SetValue(this, value);
                        continue;
                    }

                    if (type == typeof(DateTime) || type == typeof(DateTime?)) {
                        var value = DateTime.Parse(inputField.GetValue(dto).ToString());
                        matchingField.SetValue(this, value);
                        continue;
                    }

                    if (type == typeof(decimal) || type == typeof(decimal?)) {
                        var value = decimal.Parse(inputField.GetValue(dto).ToString());
                        matchingField.SetValue(this, value);
                    }

                    if (type == typeof(byte[]))
                    {
                        var value = System.Text.Encoding.UTF8.GetBytes(inputField.GetValue(dto).ToString());
                        matchingField.SetValue(this, value);
                    }

                }
            }
        }
开发者ID:Stimulant-Software,项目名称:HarvestSelect,代码行数:66,代码来源:EntityBase.cs


注:本文中的IKey.GetType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。