예제 #1
0
        public IPropertyFiller GetMethodFiller(MethodInfo methodInfo)
        {
            try
            {
                rwl.EnterReadLock();

                IPropertyFiller result     = null;
                Type            objectType = methodInfo.DeclaringType;
                while (objectType != null && result == null)
                {
                    //First try to get a specific filler based on a full type name (including namespace)
                    string fullTypeName = objectType.FullName.ToLowerInvariant();
                    if (_specificPropertyFillersByObjectType.ContainsKey(fullTypeName))
                    {
                        IDictionary <string, IPropertyFiller> propertyFillers = _specificPropertyFillersByObjectType[fullTypeName];
                        result = GetMatchingMethodFiller(methodInfo, propertyFillers);
                    }


                    //Second try to get a more generic filler based on only the class name (no namespace)
                    if (result == null)
                    {
                        string classTypeName = objectType.Name.ToLowerInvariant();
                        if (_specificPropertyFillersByObjectType.ContainsKey(classTypeName))
                        {
                            IDictionary <string, IPropertyFiller> propertyFillers = _specificPropertyFillersByObjectType[classTypeName];
                            result = GetMatchingMethodFiller(methodInfo, propertyFillers);
                        }
                    }

                    objectType = objectType.GetTypeInfo().BaseType;
                }

                //            // TODO: Would like to exclude methods for fill unless we explicity specify to include
                //            if (result == null)
                //            {
                //                var paramType = methodInfo.GetParameters()[0].ParameterType;
                //
                //                //Finally, grab a generic property filler for that property type
                //                if (_genericPropertyFillersByPropertyType.ContainsKey(paramType))
                //                {
                //                    result = _genericPropertyFillersByPropertyType[paramType];
                //                }
                //                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<object>("*", typeof(object), true, () => null);
                //
                //                    _genericPropertyFillersByPropertyType.Add(paramType, result);
                //                }
                //            }

                return(result);
            }
            finally
            {
                rwl.ExitReadLock();
            }
        }
예제 #2
0
        private static void CallSetterMethod(object instance, MethodInfo method)
        {
            IPropertyFiller filler = _fillerManager.GetMethodFiller(method);

            if (filler != null)
            {
                method.Invoke(instance, new[] { filler.GetValue(instance) });
            }
        }
예제 #3
0
 public void RegisterFiller(IPropertyFiller filler)
 {
     foreach (string objectTypeName in filler.ObjectTypeNames.Select(s => s.ToLowerInvariant()))
     {
         if (!_specificPropertyFillersByObjectType.ContainsKey(objectTypeName))
         {
             _specificPropertyFillersByObjectType[objectTypeName] = new List <IPropertyFiller>();
         }
         IList <IPropertyFiller> typeFillers = _specificPropertyFillersByObjectType[objectTypeName];
         typeFillers.Add(filler);
     }
 }
예제 #4
0
        public IPropertyFiller GetFiller(PropertyInfo propertyInfo)
        {
            IPropertyFiller result     = null;
            Type            objectType = propertyInfo.DeclaringType;

            while (objectType != null && result == null)
            {
                //First try to get a specific filler based on a full type name (including namespace)
                string fullTypeName = objectType.FullName.ToLowerInvariant();

                if (_specificPropertyFillersByObjectType.ContainsKey(fullTypeName))
                {
                    IDictionary <string, IPropertyFiller> propertyFillers = _specificPropertyFillersByObjectType[fullTypeName];
                    result = GetMatchingPropertyFiller(propertyInfo, propertyFillers);
                }

                //Second try to get a more generic filler based on only the class name (no namespace)
                if (result == null)
                {
                    string classTypeName = objectType.Name.ToLowerInvariant();
                    if (_specificPropertyFillersByObjectType.ContainsKey(classTypeName))
                    {
                        IDictionary <string, IPropertyFiller> propertyFillers = _specificPropertyFillersByObjectType[classTypeName];
                        result = GetMatchingPropertyFiller(propertyInfo, propertyFillers);
                    }
                }

                objectType = objectType.GetTypeInfo().BaseType;
            }

            if (result == null)
            {
                //Finally, grab a generic property filler for that property type
                if (_genericPropertyFillersByPropertyType.ContainsKey(propertyInfo.PropertyType))
                {
                    result = _genericPropertyFillersByPropertyType[propertyInfo.PropertyType];
                }
                else if (propertyInfo.PropertyType.GetTypeInfo().BaseType == typeof(System.Enum))
                {
                    result = new EnumFiller(propertyInfo.PropertyType);
                }
                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 <object>("*", typeof(object), true, () => null);

                    _genericPropertyFillersByPropertyType.Add(propertyInfo.PropertyType, result);
                }
            }

            return(result);
        }
예제 #5
0
        private static IPropertyFiller GetMatchingPropertyFiller(PropertyInfo propertyInfo, IList <IPropertyFiller> propertyFillers)
        {
            IPropertyFiller result = null;

            foreach (IPropertyFiller propertyFiller in propertyFillers)
            {
                if (propertyFiller.PropertyType == propertyInfo.PropertyType &&
                    propertyFiller.PropertyNames.Any(s => propertyInfo.Name.ToLowerInvariant().Contains(s.ToLowerInvariant())))
                {
                    result = propertyFiller;
                    break;
                }
            }
            return(result);
        }
예제 #6
0
        private static IPropertyFiller GetMatchingPropertyFiller(PropertyInfo propertyInfo, IDictionary <string, IPropertyFiller> 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())))
                {
                    result = propertyFiller;
                    break;
                }
            }
            return(result);
        }
예제 #7
0
 public void RegisterFiller(IPropertyFiller filler)
 {
     foreach (string objectTypeName in filler.ObjectTypeNames.Select(s => s.ToLowerInvariant()))
     {
         if (!_specificPropertyFillersByObjectType.ContainsKey(objectTypeName))
         {
             _specificPropertyFillersByObjectType[objectTypeName] = new Dictionary <string, IPropertyFiller>();
         }
         IDictionary <string, IPropertyFiller> typeFillers = _specificPropertyFillersByObjectType[objectTypeName];
         foreach (var key in filler.PropertyNames)
         {
             typeFillers[key] = filler;
         }
     }
 }
예제 #8
0
        private static IPropertyFiller GetMatchingMethodFiller(MethodInfo methodInfo, IDictionary <string, IPropertyFiller> propertyFillers)
        {
            const string setPattern = @"^Set([A-Z].*|_.*)";
            string       cleanName  = null;

            if (Regex.IsMatch(methodInfo.Name, setPattern))
            {
                cleanName = Regex.Match(methodInfo.Name, setPattern).Groups[1].Value;
            }

            IPropertyFiller result = null;

            foreach (IPropertyFiller propertyFiller in propertyFillers.Values)
            {
                if (propertyFiller.PropertyType == methodInfo.GetParameters()[0].ParameterType &&
                    (propertyFiller.PropertyNames.Any(s => methodInfo.Name.ToLowerInvariant().Contains(s.ToLowerInvariant())) ||
                     (cleanName != null && propertyFiller.PropertyNames.Any(s => cleanName.ToLowerInvariant().Contains(s.ToLowerInvariant())))))
                {
                    result = propertyFiller;
                    break;
                }
            }
            return(result);
        }
예제 #9
0
        private static void SetPropertyValue(object instance, PropertyInfo property)
        {
            IPropertyFiller filler = _fillerManager.GetFiller(property);

            property.SetValue(instance, filler.GetValue(instance), null);
        }
예제 #10
0
        public IPropertyFiller GetFiller(PropertyInfo propertyInfo)
        {
            rwl.EnterReadLock();
            var             newRegistrations = new Dictionary <Type, IPropertyFiller>();
            IPropertyFiller result           = null;

            try
            {
                Type objectType = propertyInfo.DeclaringType;
                while (objectType != null && result == null)
                {
                    //First try to get a specific filler based on a full type name (including namespace)
                    string fullTypeName = objectType.FullName.ToLowerInvariant();

                    if (_specificPropertyFillersByObjectType.ContainsKey(fullTypeName))
                    {
                        IDictionary <string, IPropertyFiller> propertyFillers = _specificPropertyFillersByObjectType[fullTypeName];
                        result = GetMatchingPropertyFiller(propertyInfo, propertyFillers);
                    }

                    //Second try to get a more generic filler based on only the class name (no namespace)
                    if (result == null)
                    {
                        string classTypeName = objectType.Name.ToLowerInvariant();
                        if (_specificPropertyFillersByObjectType.ContainsKey(classTypeName))
                        {
                            IDictionary <string, IPropertyFiller> propertyFillers = _specificPropertyFillersByObjectType[classTypeName];
                            result = GetMatchingPropertyFiller(propertyInfo, propertyFillers);
                        }
                    }

                    objectType = objectType.GetTypeInfo().BaseType;
                }

                if (result == null)
                {
                    //Finally, grab a generic property filler for that property type
                    if (_genericPropertyFillersByPropertyType.ContainsKey(propertyInfo.PropertyType))
                    {
                        result = _genericPropertyFillersByPropertyType[propertyInfo.PropertyType];
                    }
                    else if (propertyInfo.PropertyType.GetTypeInfo().BaseType == typeof(System.Enum))
                    {
                        result = new EnumFiller(this.GenFu, propertyInfo.PropertyType);
                    }
                    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 <object>(GenFu, "*", typeof(object), true, () => null);
                        newRegistrations[propertyInfo.PropertyType] = result;
                    }
                }
            }
            finally
            {
                rwl.ExitReadLock();
            }

            if (newRegistrations.Any())
            {
                rwl.EnterWriteLock();
                foreach (var newRegistration in newRegistrations)
                {
                    _genericPropertyFillersByPropertyType[newRegistration.Key] = newRegistration.Value;
                }
                rwl.ExitWriteLock();
            }
            return(result);
        }
예제 #11
0
        private static void SetPropertyValue <T>(T instance, PropertyInfo property)
        {
            IPropertyFiller filler = _maggie.GetFiller(property);

            property.SetValue(instance, filler.GetValue(), null);
        }