From 6bf77a07b776bdd6eabec0d94941d370c831b461 Mon Sep 17 00:00:00 2001 From: XperiAndri Date: Tue, 10 May 2022 23:49:16 +0300 Subject: [PATCH] Added constructor parameters filling --- src/GenFu/FillerManager.cs | 37 ++++++++-------- src/GenFu/GenFu.cs | 91 +++++++++++--------------------------- 2 files changed, 44 insertions(+), 84 deletions(-) diff --git a/src/GenFu/FillerManager.cs b/src/GenFu/FillerManager.cs index fc74a23..ad22d95 100644 --- a/src/GenFu/FillerManager.cs +++ b/src/GenFu/FillerManager.cs @@ -17,11 +17,7 @@ public class FillerManager static ReaderWriterLockSlim rwl = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); - - public FillerManager() - { - ResetFillers(); - } + public FillerManager() => ResetFillers(); public void ResetFillers() { @@ -94,7 +90,6 @@ public void ResetFillers() _propertyFillers.Add(new MedicalProcedureFiller()); _propertyFillers.Add(new StringFiller()); - } _specificPropertyFillersByObjectType = new Dictionary>(); @@ -149,7 +144,6 @@ public void RegisterFiller(IPropertyFiller filler) { typeFillers[key] = filler; } - } } finally @@ -159,13 +153,19 @@ public void RegisterFiller(IPropertyFiller filler) } public IPropertyFiller GetFiller(PropertyInfo propertyInfo) + => GetFiller(propertyInfo.DeclaringType, propertyInfo.Name, propertyInfo.PropertyType); + + public IPropertyFiller GetFiller(ParameterInfo parameterInfo) + => GetFiller(parameterInfo.Member.DeclaringType, parameterInfo.Name, parameterInfo.ParameterType); + + public IPropertyFiller GetFiller(Type declaringType, string name, Type type) { rwl.EnterReadLock(); var newRegistrations = new Dictionary(); IPropertyFiller result = null; try { - Type objectType = propertyInfo.DeclaringType; + Type objectType = declaringType; while (objectType != null && result == null) { //First try to get a specific filler based on a full type name (including namespace) @@ -174,7 +174,7 @@ public IPropertyFiller GetFiller(PropertyInfo propertyInfo) if (_specificPropertyFillersByObjectType.ContainsKey(fullTypeName)) { IDictionary propertyFillers = _specificPropertyFillersByObjectType[fullTypeName]; - result = GetMatchingPropertyFiller(propertyInfo, propertyFillers); + result = GetMatchingPropertyFiller(name, type, propertyFillers); } //Second try to get a more generic filler based on only the class name (no namespace) @@ -184,7 +184,7 @@ public IPropertyFiller GetFiller(PropertyInfo propertyInfo) if (_specificPropertyFillersByObjectType.ContainsKey(classTypeName)) { IDictionary propertyFillers = _specificPropertyFillersByObjectType[classTypeName]; - result = GetMatchingPropertyFiller(propertyInfo, propertyFillers); + result = GetMatchingPropertyFiller(name, type, propertyFillers); } } @@ -194,19 +194,19 @@ public IPropertyFiller GetFiller(PropertyInfo propertyInfo) if (result == null) { //Finally, grab a generic property filler for that property type - if (_genericPropertyFillersByPropertyType.ContainsKey(propertyInfo.PropertyType)) + if (_genericPropertyFillersByPropertyType.ContainsKey(type)) { - result = _genericPropertyFillersByPropertyType[propertyInfo.PropertyType]; + result = _genericPropertyFillersByPropertyType[type]; } - else if (propertyInfo.PropertyType.GetTypeInfo().BaseType == typeof(System.Enum)) + else if (type.GetTypeInfo().BaseType == typeof(System.Enum)) { - result = new EnumFiller(propertyInfo.PropertyType); + result = new EnumFiller(type); } else { //TODO: Can we build a custom filler here for other value types that we have not explicitly implemented (eg. long, decimal, etc.) result = new CustomFiller("*", typeof(object), true, () => null); - newRegistrations[propertyInfo.PropertyType] = result; + newRegistrations[type] = result; } } } @@ -287,13 +287,13 @@ public IPropertyFiller GetMethodFiller(MethodInfo methodInfo) } } - private static IPropertyFiller GetMatchingPropertyFiller(PropertyInfo propertyInfo, IDictionary propertyFillers) + private static IPropertyFiller GetMatchingPropertyFiller(string name, Type type, IDictionary propertyFillers) { IPropertyFiller result = null; foreach (IPropertyFiller propertyFiller in propertyFillers.Values) { - if (propertyFiller.PropertyType == propertyInfo.PropertyType && - propertyFiller.PropertyNames.Any(s => propertyInfo.Name.ToLowerInvariant().Equals(s.ToLowerInvariant()))) + if (propertyFiller.PropertyType == type && + propertyFiller.PropertyNames.Any(s => name.ToLowerInvariant().Equals(s.ToLowerInvariant()))) { result = propertyFiller; break; @@ -302,7 +302,6 @@ private static IPropertyFiller GetMatchingPropertyFiller(PropertyInfo propertyIn return result; } - private static IPropertyFiller GetMatchingMethodFiller(MethodInfo methodInfo, IDictionary propertyFillers) { const string setPattern = @"^Set([A-Z].*|_.*)"; diff --git a/src/GenFu/GenFu.cs b/src/GenFu/GenFu.cs index 1110143..8fa34d2 100644 --- a/src/GenFu/GenFu.cs +++ b/src/GenFu/GenFu.cs @@ -24,26 +24,28 @@ static GenFu() Random = new Random(); } - public static T New() where T : new() + public static T New() { return (T)New(typeof(T)); } public static object New(Type type) { - object instance = Activator.CreateInstance(type); + var ctor = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance) + .OrderByDescending(c => c.GetParameters().Length).First(); + var parameters = + ctor.GetParameters() + .Select(p => _fillerManager.GetFiller(p).GetValue(null)) + .ToArray(); + object instance = Activator.CreateInstance(type, parameters); return New(instance); } - public static T New(T instance) - { - return (T)New((object)instance); - - } + public static T New(T instance) => (T)New((object)instance); public static object New(object instance) { - if (instance != null) + if (instance is not null) { var type = instance.GetType(); if (type.FullName == "System.Guid") @@ -71,30 +73,21 @@ public static object New(object instance) } - public static List ListOf() where T : new() - { - return ListOf(typeof(T)).Cast().ToList(); - } + public static List ListOf() => ListOf(typeof(T)).Cast().ToList(); + + public static List ListOf(Type type) => BuildList(type, _listCount); - public static List ListOf(Type type) - { - return BuildList(type, _listCount); - } /// /// Creates a new list of /// /// /// Number of items to add /// - public static List ListOf(int itemCount) where T : new() - { - return ListOf(typeof(T), itemCount).Cast().ToList(); - } + public static List ListOf(int itemCount) + => ListOf(typeof(T), itemCount).Cast().ToList(); public static List ListOf(Type type, int itemCount) - { - return BuildList(type, itemCount); - } + => BuildList(type, itemCount); private static List BuildList(Type type, int itemCount) { @@ -108,15 +101,9 @@ private static List BuildList(Type type, int itemCount) return result; } - public static IEnumerable LazyOf() where T : new() - { - return LazyOf(typeof(T)).Cast(); - } + public static IEnumerable LazyOf() => LazyOf(typeof(T)).Cast(); - public static IEnumerable LazyOf(Type type) - { - return BuildLazy(type, _listCount); - } + public static IEnumerable LazyOf(Type type) => BuildLazy(type, _listCount); /// /// Creates a new lazy collection of @@ -124,15 +111,9 @@ public static IEnumerable LazyOf(Type type) /// /// Number of items to add /// - public static IEnumerable LazyOf(int itemCount) where T : new() - { - return LazyOf(typeof(T), itemCount).Cast(); - } + public static IEnumerable LazyOf(int itemCount) => LazyOf(typeof(T), itemCount).Cast(); - private static IEnumerable LazyOf(Type type, int itemCount) - { - return BuildLazy(type, itemCount); - } + private static IEnumerable LazyOf(Type type, int itemCount) => BuildLazy(type, itemCount); private static IEnumerable BuildLazy(Type type, int itemCount) @@ -143,15 +124,9 @@ private static IEnumerable BuildLazy(Type type, int itemCount) } } - public static IEnumerable ForeverOf() where T : new() - { - return ForeverOf(typeof(T)).Cast(); - } + public static IEnumerable ForeverOf() => ForeverOf(typeof(T)).Cast(); - private static IEnumerable ForeverOf(Type type) - { - return BuildForever(type); - } + private static IEnumerable ForeverOf(Type type) => BuildForever(type); private static IEnumerable BuildForever(Type type) { @@ -174,25 +149,16 @@ private static void CallSetterMethod(object instance, MethodInfo method) method.Invoke(instance, new[] { filler.GetValue(instance) }); } - - public static DateTime MinDateTime { - get { return new GenericFillerDefaults(_fillerManager).GetMinDateTime(); } - - set - { - new GenericFillerDefaults(_fillerManager).SetMinDateTime(value); - } + get => new GenericFillerDefaults(_fillerManager).GetMinDateTime(); + set => new GenericFillerDefaults(_fillerManager).SetMinDateTime(value); } public static DateTime MaxDateTime { - get { return new GenericFillerDefaults(_fillerManager).GetMaxDateTime(); } - set - { - new GenericFillerDefaults(_fillerManager).SetMaxDateTime(value); - } + get => new GenericFillerDefaults(_fillerManager).GetMaxDateTime(); + set => new GenericFillerDefaults(_fillerManager).SetMaxDateTime(value); } public static Random Random { get; private set; } @@ -250,9 +216,4 @@ public class Defaults public const string STRING_LOADFAIL = "The resource list for {0} failed to load."; } - - } - - -