コード例 #1
0
        public void Initialize <TModel>(IModelBindingDefinition <TModel> init)
        {
            var modelAttribute            = typeof(TModel).GetCustomAttributes(true).OfType <ArgsModelAttribute>().SingleOrDefault() ?? ArgsModelAttribute.Default;
            var modelDescriptionAttribute = typeof(TModel).GetCustomAttributes(true).OfType <DescriptionAttribute>().SingleOrDefault();

            init.SwitchDelimiter = modelAttribute.SwitchDelimiter;
            init.StringComparer  = modelAttribute.StringComparer;
            SortedDictionary <int, MemberInfo> ordinalArguments = new SortedDictionary <int, MemberInfo>();

            if (modelDescriptionAttribute != null)
            {
                init.CommandModelDescription = modelDescriptionAttribute.Description;
            }

            var members = GetMembers(typeof(TModel));

            foreach (MemberInfo member in members)
            {
                var memberBinding = init.GetOrCreateMemberBindingDefinition(member);

                var memberAttributes = member.GetCustomAttributes(true).ToArray();

                var switchAttribute = memberAttributes.OfType <ArgsMemberSwitchAttribute>().SingleOrDefault();
                if (switchAttribute != null)
                {
                    if (switchAttribute.SwitchValues != null)
                    {
                        memberBinding.SwitchValues.AddRange(switchAttribute.SwitchValues);
                    }
                    if (switchAttribute.ArgumentIndex.HasValue)
                    {
                        ordinalArguments.Add(switchAttribute.ArgumentIndex.Value, member);
                    }
                }
                else
                {
                    memberBinding.SwitchValues.AddRange(new[] { member.Name, DeriveShortName(members, member) }.Distinct(init.StringComparer));
                }

                var defaultValueAttribute = memberAttributes.OfType <DefaultValueAttribute>().SingleOrDefault();
                if (defaultValueAttribute != null)
                {
                    memberBinding.DefaultValue = defaultValueAttribute.Value;
                }

                var typeConverterAttribute = memberAttributes.OfType <TypeConverterAttribute>().SingleOrDefault();

                if (typeConverterAttribute != null)
                {
                    memberBinding.TypeConverter = (TypeConverter)ArgsTypeResolver.Current.GetService(Type.GetType(typeConverterAttribute.ConverterTypeName));
                }
            }

            init.SetOrdinalArguments(ordinalArguments.Select(a => a.Value));
        }
コード例 #2
0
        public FluentMemberBindingConfiguration <TModel> ForMember <TResult>(Expression <Func <TModel, TResult> > member)
        {
            var memberBindingDefinition = Source.GetOrCreateMemberBindingDefinition(member.GetMemberInfoFromExpression());

            return(new FluentMemberBindingConfiguration <TModel>(memberBindingDefinition));
        }