Ejemplo n.º 1
0
        private IList <IPropertyData> SortConstructorParameters(IList <IPropertyData> parameters, bool exactMatch)
        {
            StringComparison comparison = exactMatch ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

            foreach (ConstructorInfo constructor in ForType.GetConstructors())
            {
                ParameterInfo[] ctorParms = constructor.GetParameters();
                if (ctorParms.Length != parameters.Count)
                {
                    continue;
                }

                IPropertyData[] result     = new IPropertyData[parameters.Count];
                int             matchCount = 0;
                foreach (IPropertyData property in parameters)
                {
                    if (property.Position >= 0)
                    {
                        if (exactMatch && ctorParms[property.Position].ParameterType != property.PropertyType)
                        {
                            break;
                        }

                        //if (!exactMatch && !ctorParms[property.Position].ParameterType.IsAssignableFrom(property.PropertyType))
                        //    break;

                        if (result[property.Position] == null)
                        {
                            result[property.Position] = property;
                            matchCount++;
                        }
                        else
                        {
                            // something else occupies this spot already, so there's a conflict with this constructor so try another
                            // most likely none of them will work in this situation, but we'll let the outer method determine that
                            break;
                        }
                    }
                    else
                    {
                        // named argument, search the list
                        int i;
                        for (i = 0; i < ctorParms.Length; i++)
                        {
                            string parmName = ctorParms[i].Name;
                            if (ctorParms[i].IsDefined(typeof(ConstructorParameterAttribute), false))
                            {
                                parmName = ReflectionUtils.GetAttribute <ConstructorParameterAttribute>(ctorParms[i], false).Name;
                            }

                            if (parmName.Equals(property.ConstructorParameterName, comparison) &&
                                ((exactMatch && ctorParms[i].ParameterType == property.PropertyType) ||
                                 (!exactMatch /* && ctorParms[i].ParameterType.IsAssignableFrom(property.PropertyType)*/)))
                            {
                                break;
                            }
                        }
                        if (i < ctorParms.Length && result[i] == null)
                        {
                            result[i] = property;
                            matchCount++;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                if (matchCount == result.Length)
                {
                    // found a match, return it
                    if (exactMatch)
                    {
                        return(result);
                    }

                    if (IsLooseMatch(constructor, result))
                    {
                        return(result);
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
        private IList <IPropertyData> SortConstructorParameters(IList <IPropertyData> parameters, bool exactMatch)
        {
            StringComparison comparison = exactMatch ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

            foreach (ConstructorInfo constructor in ForType.GetConstructors())
            {
                ParameterInfo[] ctorParms = constructor.GetParameters();
                if (ctorParms.Length != parameters.Count)
                {
                    continue;
                }

                IPropertyData[] result     = new IPropertyData[parameters.Count];
                int             matchCount = 0;
                foreach (IPropertyData property in parameters)
                {
                    // named argument, search the list
                    int i;
                    for (i = 0; i < ctorParms.Length; i++)
                    {
                        string parmName = ctorParms[i].Name;
                        if (ctorParms[i].IsDefined(typeof(ConstructorParameterAttribute), false))
                        {
                            parmName = ReflectionUtils.GetAttribute <ConstructorParameterAttribute>(ctorParms[i], false).Name ?? parmName;
                        }

                        if (string.IsNullOrEmpty(parmName))
                        {
                            parmName = ctorParms[i].Name;
                        }

                        if (parmName.Equals(property.ConstructorParameterName, comparison) &&
                            ((exactMatch && ctorParms[i].ParameterType == property.PropertyType) ||
                             (!exactMatch /* && ctorParms[i].ParameterType.IsAssignableFrom(property.PropertyType)*/)))
                        {
                            break;
                        }
                    }
                    if (i < ctorParms.Length && result[i] == null)
                    {
                        result[i] = property;
                        matchCount++;
                    }
                    else
                    {
                        break;
                    }
                }
                if (matchCount == result.Length)
                {
                    // found a match, return it
                    if (exactMatch)
                    {
                        return(result);
                    }

                    if (IsLooseMatch(constructor, result))
                    {
                        return(result);
                    }
                }
            }
            return(null);
        }