private void FillMember(object target, MemberInfo member) { Type memberType = UniversalGenerator_Functions.GetTypeOfMemberInfo(member); if (memberType == null) { return; } object[] customAttributes = member.GetCustomAttributes(false); if (fillFunctions.ContainsKey(memberType)) { if (memberType != null) { GetRandomValue piaction = fillFunctions[memberType]; FillMemberValue(target, member, GetRandomValueForType(memberType, customAttributes)); } } else if (memberType.IsEnum) { GetRandomValue piaction = fillFunctions[typeof(Enum)]; FillMemberValue(target, member, GetRandomValueForType(memberType, customAttributes)); } else { object instance = CreateInstance(memberType); FillMemberValue(target, member, instance); } }
/// <summary> /// Assign a function that will generate a random value for a given type. /// </summary> /// <param name="type">Type for which the function will be used.</param> /// <param name="action">Function that will generate a random value for a given type.</param> public void AddFillFunction(Type type, GetRandomValue action) { if (fillFunctions.ContainsKey(type)) { fillFunctions[type] = action; } else { fillFunctions.Add(type, action); } }
private object GetRandomValueFromFunctionForType(Type type, object[] customAttributes) { if (fillFunctions.ContainsKey(type)) { if (type != null) { GetRandomValue piaction = fillFunctions[type]; return(piaction(customAttributes)); } } else if (type.IsEnum) { GetRandomValue piaction = fillFunctions[typeof(Enum)]; List <object> attrs = new List <object>(0); attrs.AddRange(customAttributes); attrs.Add(type); return(piaction(customAttributes)); } return(null); }
new Weapon(GetRandomValue(_setting.MinValueOfWeapons, _setting.MaxValueOfWeapons)));