当前位置: 首页>>代码示例>>C#>>正文


C# MappingSchema.GetAttributes方法代码示例

本文整理汇总了C#中MappingSchema.GetAttributes方法的典型用法代码示例。如果您正苦于以下问题:C# MappingSchema.GetAttributes方法的具体用法?C# MappingSchema.GetAttributes怎么用?C# MappingSchema.GetAttributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MappingSchema的用法示例。


在下文中一共展示了MappingSchema.GetAttributes方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetDefaultMappingFromEnumType

        public static Type GetDefaultMappingFromEnumType(MappingSchema mappingSchema, Type enumType)
        {
            var type = enumType.ToNullableUnderlying();

            if (!type.IsEnumEx())
                return null;

            var fields =
            (
                from f in type.GetFieldsEx()
                where (f.Attributes & EnumField) == EnumField
                let attrs = mappingSchema.GetAttributes<MapValueAttribute>(f, a => a.Configuration)
                select
                (
                    from a in attrs
                    where a.Configuration == attrs[0].Configuration
                    orderby !a.IsDefault
                    select a
                ).ToList()
            ).ToList();

            Type defaultType = null;

            if (fields.All(attrs => attrs.Count != 0))
            {
                var attr = fields.FirstOrDefault(attrs => attrs[0].Value != null);

                if (attr != null)
                {
                    var valueType = attr[0].Value.GetType();

                    if (fields.All(attrs => attrs[0].Value == null || attrs[0].Value.GetType() == valueType))
                        defaultType = valueType;
                }
            }

            if (defaultType == null)
                defaultType = Enum.GetUnderlyingType(type);

            if (enumType.IsNullable() && !defaultType.IsClassEx() && !defaultType.IsNullable())
                defaultType = typeof(Nullable<>).MakeGenericType(defaultType);

            return defaultType;
        }
开发者ID:Convey-Compliance,项目名称:linq2db,代码行数:44,代码来源:ConvertBuilder.cs

示例2: GetFromEnum

        static Expression GetFromEnum(Type @from, Type to, Expression expression, MappingSchema mappingSchema)
        {
            if (from.IsEnumEx())
            {
                var fromFields = @from.GetFieldsEx()
                    .Where (f => (f.Attributes & EnumField) == EnumField)
                    .Select(f => new EnumValues { Field = f, Attrs = mappingSchema.GetAttributes<MapValueAttribute>(f, a => a.Configuration) })
                    .ToList();

                {
                    var toTypeFields = fromFields
                        .Select(f => new { f.Field, Attrs = f.Attrs
                            .OrderBy(a =>
                            {
                                var idx = a.Configuration == null ?
                                    int.MaxValue :
                                    Array.IndexOf(mappingSchema.ConfigurationList, a.Configuration);
                                return idx < 0 ? int.MaxValue : idx;
                            })
                            .ThenBy(a => !a.IsDefault)
                            .ThenBy(a => a.Value == null)
                            .FirstOrDefault(a => a.Value == null || a.Value.GetType() == to) })
                        .ToList();

                    if (toTypeFields.All(f => f.Attrs != null))
                    {
                        var cases = toTypeFields.Select(f => Expression.SwitchCase(
                            Expression.Constant(f.Attrs.Value ?? mappingSchema.GetDefaultValue(to), to),
                            Expression.Constant(Enum.Parse(@from, f.Field.Name, false))));

                        var expr = Expression.Switch(
                            expression,
                            Expression.Convert(
                                Expression.Call(_defaultConverter,
                                    Expression.Convert(expression, typeof(object)),
                                    Expression.Constant(to)),
                                to),
                            cases.ToArray());

                        return expr;
                    }

                    if (toTypeFields.Any(f => f.Attrs != null))
                    {
                        var field = toTypeFields.First(f => f.Attrs == null);

                        return Expression.Convert(
                            Expression.Call(
                                _throwLinqToDBConvertException,
                                Expression.Constant(
                                    "Inconsistent mapping. '{0}.{1}' does not have MapValue(<{2}>) attribute."
                                        .Args(from.FullName, field.Field.Name, to.FullName))),
                                to);
                    }
                }

                if (to.IsEnumEx())
                {
                    var toFields = to.GetFieldsEx()
                        .Where (f => (f.Attributes & EnumField) == EnumField)
                        .Select(f => new EnumValues { Field = f, Attrs = mappingSchema.GetAttributes<MapValueAttribute>(f, a => a.Configuration) })
                        .ToList();

                    var dic = new Dictionary<EnumValues,EnumValues>();
                    var cl  = mappingSchema.ConfigurationList.Concat(new[] { "", null }).Select((c,i) => new { c, i }).ToArray();

                    foreach (var toField in toFields)
                    {
                        if (toField.Attrs == null || toField.Attrs.Length == 0)
                            return null;

                        var toAttr = toField.Attrs.First();

                        toAttr = toField.Attrs.FirstOrDefault(a => a.Configuration == toAttr.Configuration && a.IsDefault) ?? toAttr;

                        var fromAttrs = fromFields.Where(f => f.Attrs.Any(a =>
                            a.Value == null ? toAttr.Value == null : a.Value.Equals(toAttr.Value))).ToList();

                        if (fromAttrs.Count == 0)
                            return null;

                        if (fromAttrs.Count > 1)
                        {
                            var fattrs =
                                from f in fromAttrs
                                select new {
                                    f,
                                    a = f.Attrs.First(a => a.Value == null ? toAttr.Value == null : a.Value.Equals(toAttr.Value))
                                } into fa
                                from c in cl
                                where fa.a.Configuration == c.c
                                orderby c.i
                                select fa.f;

                            fromAttrs = fattrs.Take(1).ToList();
                        }

                        var prev = dic
                            .Where (a => a.Value.Field == fromAttrs[0].Field)
                            .Select(pair => new { To = pair.Key, From = pair.Value })
//.........这里部分代码省略.........
开发者ID:Convey-Compliance,项目名称:linq2db,代码行数:101,代码来源:ConvertBuilder.cs

示例3: AttributeTest3

		public void AttributeTest3()
		{
			var ms = new MappingSchema("3", new MappingSchema("2"));

			var attrs = ms.GetAttributes<MapValueAttribute>(
				MemberHelper.FieldOf<AttrTest>(a => a.Field1),
				a => a.Configuration);

			Assert.That(attrs.Length,   Is.EqualTo(3));
			Assert.That(attrs[0].Value, Is.EqualTo(3));
			Assert.That(attrs[1].Value, Is.EqualTo(2));
			Assert.That(attrs[2].Value, Is.EqualTo(1));
		}
开发者ID:jkshan,项目名称:linq2db,代码行数:13,代码来源:MappingSchemaTest.cs

示例4: AttributeTest8

		public void AttributeTest8()
		{
			var ms = new MappingSchema("3", new MappingSchema("2"))
			{
				MetadataReader = new XmlAttributeReader(new MemoryStream(Encoding.UTF8.GetBytes(Data)))
			};

			var attrs = ms.GetAttributes<MapValueAttribute>(
				MemberHelper.FieldOf<AttrTest>(a => a.Field1),
				a => a.Configuration);

			Assert.That(attrs.Length,   Is.EqualTo(4));
			Assert.That(attrs[0].Value, Is.EqualTo(30));
			Assert.That(attrs[1].Value, Is.EqualTo(3));
			Assert.That(attrs[2].Value, Is.EqualTo(2));
			Assert.That(attrs[3].Value, Is.EqualTo(1));
		}
开发者ID:jkshan,项目名称:linq2db,代码行数:17,代码来源:MappingSchemaTest.cs

示例5: GetTable

        private TableName GetTable(MappingSchema mappingSchema, Type dataObjectType)
        {
            var attribute = mappingSchema
                .GetAttributes<TableAttribute>(dataObjectType)
                .FirstOrDefault();

            var tableName = attribute?.Name ?? dataObjectType.Name;
            var schemaName = attribute?.Schema;
            return new TableName(tableName, schemaName);
        }
开发者ID:2gis,项目名称:nuclear-river,代码行数:10,代码来源:BulkReplicationActor.cs


注:本文中的MappingSchema.GetAttributes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。