示例#1
0
        private string GetTableName <T>()
            where T : ModelBase
        {
            var tableCfgInfo = AttributeHelper.Get <T, TableAttribute>();

            return(tableCfgInfo != null?tableCfgInfo.Name.Trim() : typeof(T).Name);
        }
示例#2
0
        public static bool TryObtainBindByBinder <T>(this MemberInfo member, bool wrap, out ISafeSettingsBinder <T> settingsBinder)
        {
            settingsBinder = null;

            var attribute = AttributeHelper.Get <BindByAttribute>(member);

            if (attribute == null)
            {
                return(false);
            }

            var memberType = GetMemberType(member);

            var desiredBinderType = typeof(ISettingsBinder <>).MakeGenericType(memberType);

            if (!desiredBinderType.IsAssignableFrom(attribute.BinderType))
            {
                throw new InvalidOperationException($"The type specified in {nameof(BindByAttribute)} must implement {desiredBinderType.ToFriendlyName()}.");
            }

            var binder = Activator.CreateInstance(attribute.BinderType);

            binder = Activator.CreateInstance(typeof(SafeBinderWrapper <>).MakeGenericType(memberType), binder);

            if (wrap)
            {
                binder = Activator.CreateInstance(typeof(BinderWrapper <>).MakeGenericType(memberType), binder);
            }

            settingsBinder = (ISafeSettingsBinder <T>)binder;
            return(true);
        }
 public void Helper_should_not_filter_inherited_by_default()
 {
     AttributeHelper
     .Get <ExampleAttribute>(typeof(WithAttributesChild))
     .Should().NotBeNull()
     .And.BeOfType <ExampleAttribute>();
 }
 public void Helper_should_give_one_on_get_for_type_if_attributes_were_set()
 {
     AttributeHelper
     .Get <ExampleAttribute>(typeof(WithAttributes))
     .Should().NotBeNull()
     .And.BeOfType <ExampleAttribute>();
 }
        public void Helper_should_give_null_on_get_for_member_if_none_attributes_were_set()
        {
            var propertyInfo = typeof(WithoutAttributes).GetInstanceProperties().Single(p => p.Name == nameof(WithoutAttributes.Property));

            AttributeHelper
            .Get <ExampleAttribute>(propertyInfo)
            .Should().BeNull();
        }
示例#6
0
        private static IEnumerable <string> Validate(Type type, object value, HashSet <Type> visitedTypes)
        {
            if (!visitedTypes.Add(type))
            {
                yield break;
            }

            var attribute = AttributeHelper.Get <ValidateByAttribute>(type);

            if (attribute != null)
            {
                var validator      = Activator.CreateInstance(attribute.ValidatorType);
                var validateMethod = validator.GetType().GetMethod(nameof(ISettingsValidator <object> .Validate), new[] { type });
                if (validateMethod == null)
                {
                    throw new SettingsValidationException($"Type '{validator.GetType()}' specified as validator for settings of type '{type}' does not contain a suitable {nameof(ISettingsValidator<object>.Validate)} method.");
                }

                foreach (var error in (IEnumerable <string>)validateMethod.Invoke(validator, new[] { value }))
                {
                    yield return(error);
                }
            }

            if (value != null)
            {
                foreach (var field in type.GetInstanceFields())
                {
                    foreach (var error in Validate(field.FieldType, field.GetValue(value), visitedTypes))
                    {
                        yield return(FormatError(field.Name, error));
                    }
                }

                var properties = type.GetInstanceProperties();

                if (type.IsInterface)
                {
                    properties = properties.Concat(type.GetInterfaces().SelectMany(iface => iface.GetInstanceProperties()));
                }

                foreach (var prop in properties)
                {
                    object propertyValue;

                    try
                    {
                        propertyValue = prop.GetValue(value);
                    }
                    catch
                    {
                        continue;
                    }

                    foreach (var error in Validate(prop.PropertyType, propertyValue, visitedTypes))
                    {
                        yield return(FormatError(prop.Name, error));
                    }
                }
            }

            visitedTypes.Remove(type);
        }
示例#7
0
 public static bool TryGetBindByAttribute(this Type type, out BindByAttribute attribute) =>
 (attribute = AttributeHelper.Get <BindByAttribute>(type, false)) != null;
示例#8
0
 public DapperRepository(IDbContext dbContext)
 {
     _dapperDbContext = (DapperDbContextBase)dbContext;
     System.Data.Linq.Mapping.TableAttribute tableCfgInfo = AttributeHelper.Get <T, System.Data.Linq.Mapping.TableAttribute>();
     _tableName = tableCfgInfo != null?tableCfgInfo.Name.Trim() : typeof(T).Name;
 }
        public IEnumerable<IConfigurationOptions> ParseXml(string contentsOfConfigFile)
        {
            if (String.IsNullOrEmpty(contentsOfConfigFile))
                throw new ArgumentException("contentsOfConfigFile is null or empty.", "contentsOfConfigFile");

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(contentsOfConfigFile);
            XmlNodeList configNodes = doc.DocumentElement.SelectNodes("//config");
            var result = new List<IConfigurationOptions>();
            foreach (XmlNode configNode in configNodes)
            {
                IConfigurationOptions options = new DefaultConfigurationOptions();
                var attrib = new AttributeHelper(".//add[@key='{0}']/@value", configNode);
                options.AssemblyDirectory = attrib.Get("assemblydirectory");
                options.AbstractBaseName = attrib.Get("abstractbasename");
                options.BaseTypeName = attrib.Get("basetypename");
                options.ConnectionString = attrib.Get("connectionstring");
                options.DataNamespace = attrib.Get("datanamespace");
                options.IocVerboseLogging = attrib.GetBool("iocverboselogging", false);
                options.GenerateColumnList = attrib.GetBool("generatecolumnlist", true);
                options.GenerateComments = attrib.GetBool("generatecomments", true);
                options.UseMicrosoftsHeader = attrib.GetBool("usemicrosoftsheader", false);
                options.Namespace = attrib.Get("namespace");
                options.OutputPath = attrib.Get("outputpath");
                options.EnumOutputPath = attrib.Get("enumoutputpath");
                options.EnumNamespace = attrib.Get("enumnamespace");
                options.StaticPrimaryKeyName = attrib.Get("staticprimarykeyname");
                options.OnlyTablesWithPrefix.AddRange(attrib.Get("onlytableswithprefix").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
                options.SkipTablesWithPrefix.AddRange(attrib.Get("skiptableswithprefix").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
                options.SkipTables.AddRange(attrib.Get("skiptables").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
                options.Enums = ParseEnums(configNode);
                LoadEnumReplacements(configNode, options);
                options.Components = ParseComponents(configNode);
                result.Add(options);
            }
            return result;
        }
示例#10
0
 public static bool CanBeUsedFor(Type type) => AttributeHelper.Get <OmitConstructorsAttribute>(type) != null;
 public void Helper_should_filter_inherited_if_specified()
 {
     AttributeHelper
     .Get <ExampleAttribute>(typeof(WithAttributesChild), false)
     .Should().BeNull();
 }
 public void Helper_should_give_null_on_get_for_type_if_none_attributes_were_set()
 {
     AttributeHelper
     .Get <ExampleAttribute>(typeof(WithoutAttributes))
     .Should().BeNull();
 }
示例#13
0
 public static string GetDisplayName(this Enum val)
 {
     return(AttributeHelper.Get <DisplayAttribute>(val)?.Name
            ?? val.ToString());
 }