Example #1
0
        private MemberInfo FindMatchingMember(
            ParameterInfo par, BindingFlags flags, Mode mode)
        {
            var attr = par.GetSingleAttribute<CtorFieldMapToAttribute>();
            bool isExact = attr != null;
            var name = isExact ? attr.FieldOrPropertyName : par.Name;
            IEnumerable<string> names;

            var inclFields = (mode & Mode.Fields) != 0;
            var inclProperties = (mode & Mode.Properties) != 0;

            if (!isExact)
            {
                var nrmName = name;

                if (inclFields)
                {
                    if (nrmName.StartsWith("m_", StringComparison.OrdinalIgnoreCase))
                        nrmName = nrmName.Substring(2);
                    else if (nrmName.StartsWith("_", StringComparison.Ordinal))
                        nrmName = nrmName.Substring(1);
                }

                bool empty = nrmName.Length == 0;
                if (!empty && char.IsUpper(nrmName, 0))
                    nrmName = char.ToLowerInvariant(nrmName[0]) + nrmName.Substring(1);

                var nms = new List<string>();

                if (inclFields)
                {
                    nms.Add("_" + nrmName);
                    nms.Add("m_" + nrmName);
                }

                if (!empty)
                {
                    nms.Add(nrmName);
                    nrmName = char.ToUpperInvariant(nrmName[0]) + nrmName.Substring(1);
                    nms.Add(nrmName);

                    if (inclFields)
                    {
                        nms.Add("_" + nrmName);
                        nms.Add("m_" + nrmName);
                    }
                }

                names = nms;
            }
            else
            {
                names = new[] {name};
            }

            MemberInfo result = null;

            if (inclProperties)
                foreach (var n in names)
                    FindMatchingMember(ref result, par, false, n, names, flags);
            if (result != null)
                return result;

            if (inclFields)
                foreach (var n in names)
                    FindMatchingMember(ref result, par, true, n, names, flags);
            if (result != null)
                return result;

            throw new SerializerException(
                "Constructor {0}.{1}(...{2} {3}...) does not map to {4} {5} {6}{7} with type {8}",
                FieldType.FullName, FieldType.Name,
                par.ParameterType, par.Name,
                (flags & BindingFlags.NonPublic) != 0 ? "public and private" : "public",
                inclProperties && inclFields ? "fields or properties" : (inclFields ? "fields" : "properties"),
                isExact ? "" : "similar to ", name, par.ParameterType.ToDebugStr());
        }