1. 程式人生 > >根據類型自動生成隨機數

根據類型自動生成隨機數

imu ict 引用類型 stat end info static reg att

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using System.Text;

namespace Qka.UsersApi.Test
{
    public class ParaUtil
    {
        #region 隨機獲取常用類型值


        static Random rd = new Random();

        /// <summary>
        ///
隨機int /// </summary> /// <returns></returns> public static int GetInt(int max) { return rd.Next(0, max); } /// <summary> /// 隨機int /// </summary> /// <returns></returns> public static int
GetInt(int min = int.MinValue, int max = int.MaxValue) { return rd.Next(min, max); } /// <summary> /// 隨機long /// </summary> /// <returns></returns> public static long GetLong(long min = long.MinValue, long max = long.MaxValue) {
if (long.TryParse(GetDouble((double)min, (double)max).ToString(), out long longValue)) { ///成功 return longValue; } //失敗 return GetInt(); } /// <summary> /// 隨機float /// </summary> /// <returns></returns> public static float GeFloat(float min = float.MinValue, float max = float.MaxValue) { if (float.TryParse(GetDouble((double)min, (double)max).ToString(), out float outValue)) { ///成功 return outValue; } //失敗 return GetInt(); } /// <summary> /// 隨機Double /// </summary> /// <returns></returns> public static double GetDouble(double min = double.MinValue, double max = double.MaxValue) { if (rd != null) { return rd.NextDouble() * (max - min) + min; } else { return 0.0d; } } /// <summary> /// 隨機string /// </summary> /// <returns></returns> public static string GetString() { return GetString(GetInt(0, 1000), GetBoolean(), GetInt()); } /// <summary> /// 隨機string /// </summary> /// <returns></returns> public static string GetString(int maxSize) { var size = GetInt(0, maxSize); return GetString(size, GetBoolean(), GetInt()); } /// <summary> /// 隨機string /// </summary> /// <returns></returns> public static string GetString(int size, bool isNum = false, int isLower = -1) { StringBuilder builder = new StringBuilder(); char ch = 0; for (int i = 0; i < size; i++) { if (isNum) { ch = Convert.ToChar(Convert.ToInt32(9 * rd.NextDouble() + 48)); } else { if (isLower < 0) { int index = Convert.ToInt32(size * rd.NextDouble()) % 2 == 0 ? 65 : 97; ch = Convert.ToChar(Convert.ToInt32(25 * rd.NextDouble() + index)); } else if (isLower == 0) { ch = Convert.ToChar(Convert.ToInt32(25 * rd.NextDouble() + 65)); } else if (isLower > 0) { ch = Convert.ToChar(Convert.ToInt32(25 * rd.NextDouble() + 97)); } } builder.Append(ch); } return builder.ToString(); } /// <summary> /// 隨機datetime /// </summary> /// <returns></returns> public static DateTime GetDateTime() { return GetDate(DateTime.MinValue, DateTime.MaxValue); } /// <summary> /// 隨機datetime /// </summary> /// <returns></returns> public static DateTime GetDate(DateTime minDate, DateTime maxDate) { int totalDays = (int)((TimeSpan)maxDate.Subtract(minDate)).TotalDays; int randomDays = rd.Next(0, totalDays); return minDate.AddDays(randomDays); } /// <summary> /// 隨機bool /// </summary> /// <returns></returns> public static bool GetBoolean() { return (GetInt(0, 2) == 0); } /// <summary> /// 隨機char /// </summary> /// <returns></returns> public static char GetChar() { return Convert.ToChar(Convert.ToInt32(26 * rd.NextDouble() + 64)); } /// <summary> /// 隨機byte /// </summary> /// <returns></returns> public static byte GetByte() { return GetByte(0, byte.MaxValue); } /// <summary> /// 隨機byte /// </summary> /// <returns></returns> public static byte GetByte(byte min, byte max) { return (byte)GetInt((int)min, (int)max); } /// <summary> /// 隨機shrot /// </summary> /// <returns></returns> public static short GetShort() { return GetShort(0, short.MaxValue); } /// <summary> /// 隨機short /// </summary> /// <returns></returns> public static short GetShort(short min, short max) { return (short)GetInt((int)min, (int)max); } #endregion #region 如果是實體類型 public static T GetModelData<T>(bool isGetOkData = false) where T : new() { var model = new T(); var t = typeof(T); PropertyInfo[] properties = t.GetProperties(); foreach (PropertyInfo property in properties) { Type oneType = property.PropertyType; if (IsNullableType(oneType)) { if (!GetBoolean()) { //對於可空類型,隨機給空值 continue; } oneType = property.PropertyType.GetGenericArguments()[0]; } if (isGetOkData) { var paraValue = GetValueBySLAttribute(property); if (paraValue != null) { property.SetValue(model, paraValue); continue; } } if (!oneType.IsValueType) { if (!GetBoolean()) { //如果是引用類型,又不要求一定要合理的值,則可以為空 continue; } } property.SetValue(model, GetValueByType(oneType, 0)); } return model; } /// <summary> /// 根據有標識長度限制的特性來生成對應的值 /// </summary> /// <param name="property"></param> /// <returns></returns> private static string GetValueBySLAttribute(PropertyInfo property) { var allAttribute = property.GetCustomAttributes(true); if (allAttribute == null) { return null; } foreach (var item in allAttribute) { if (item is StringLengthAttribute) { var slAttribute = item as StringLengthAttribute; var paraVal = GetString(slAttribute.MaximumLength); bool isok = false; while (!isok) { paraVal = GetString(slAttribute.MaximumLength); isok = ValidValue(slAttribute, paraVal); } return paraVal; } } return null; } /// <summary> /// 驗證將要給的值是否合格 /// </summary> /// <param name="slAttribute"></param> /// <param name="paraVal"></param> /// <returns></returns> private static bool ValidValue(StringLengthAttribute slAttribute, string paraVal) { if (slAttribute.IsValid(paraVal)) { return true; } return false; } private static bool IsNullableType(Type theType) { return (theType.IsGenericType && theType. GetGenericTypeDefinition().Equals (typeof(Nullable<>))); } private static object GetValueByType(Type paraType, int maxSize = 0) { if (paraType == typeof(string)) { if (maxSize > 0) { return GetString(maxSize); } return GetString(); } if (paraType == typeof(Int16)) { return GetInt(Int16.MinValue, Int16.MaxValue); } if (paraType == typeof(int)) { return GetInt(); } if (paraType == typeof(Int32)) { return GetInt(); } if (paraType == typeof(Int64)) { return GetLong(); } if (paraType == typeof(long)) { return GetLong(); } if (paraType == typeof(float)) { return GeFloat(); } if (paraType == typeof(double)) { return GetDouble(); } if (paraType == typeof(bool)) { return GetBoolean(); } if (paraType == typeof(DateTime)) { return GetDateTime(); } return null; } /// <summary> /// 驗證實體中的StringLength特性能否夠 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="model"></param> /// <returns>false為不通過,true為通過</returns> public static bool IsValidate<T>(T model) { var t = typeof(T); PropertyInfo[] properties = t.GetProperties(); foreach (PropertyInfo property in properties) { //object[] Attribute2 = p.GetCustomAttributes(typeof(MarkAttribute), false); var allAttribute = property.GetCustomAttributes(true); if (allAttribute == null) { continue; } foreach (var item in allAttribute) { if (item is StringLengthAttribute) { var slAttribute = item as StringLengthAttribute; var validateVal = property.GetValue(model, null); if (!slAttribute.IsValid(validateVal)) { return false; } } } } return true; } #endregion } }

根據類型自動生成隨機數