本文整理汇总了C#中Type.IsEnumEx方法的典型用法代码示例。如果您正苦于以下问题:C# Type.IsEnumEx方法的具体用法?C# Type.IsEnumEx怎么用?C# Type.IsEnumEx使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Type
的用法示例。
在下文中一共展示了Type.IsEnumEx方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryGetConvertExpression
public override LambdaExpression TryGetConvertExpression(Type from, Type to)
{
if (to.IsEnumEx() && from == typeof(decimal))
{
var type = Converter.GetDefaultMappingFromEnumType(this, to);
if (type != null)
{
var fromDecimalToType = GetConvertExpression(from, type, false);
var fromTypeToEnum = GetConvertExpression(type, to, false);
return Expression.Lambda(
fromTypeToEnum.GetBody(fromDecimalToType.Body),
fromDecimalToType.Parameters);
}
}
return base.TryGetConvertExpression(from, to);
}
示例2: IsConvertible
static bool IsConvertible(Type type)
{
if (type.IsEnumEx())
return false;
switch (type.GetTypeCodeEx())
{
case TypeCode.Boolean :
case TypeCode.Byte :
case TypeCode.SByte :
case TypeCode.Int16 :
case TypeCode.Int32 :
case TypeCode.Int64 :
case TypeCode.UInt16 :
case TypeCode.UInt32 :
case TypeCode.UInt64 :
case TypeCode.Single :
case TypeCode.Double :
case TypeCode.Decimal :
case TypeCode.Char : return true;
default : return false;
}
}
示例3: GetToEnum
static Expression GetToEnum(Type @from, Type to, Expression expression, MappingSchema mappingSchema)
{
if (to.IsEnumEx())
{
var toFields = mappingSchema.GetMapValues(to);
if (toFields == null)
return null;
var fromTypeFields = toFields
.Select(f => new { f.OrigValue, attrs = f.MapValues.Where(a => a.Value == null || a.Value.GetType() == @from).ToList() })
.ToList();
if (fromTypeFields.All(f => f.attrs.Count != 0))
{
var cases = fromTypeFields
.Select(f => new
{
value = f.OrigValue,
attrs = f.attrs
.Where (a => a.Configuration == f.attrs[0].Configuration)
.Select(a => a.Value ?? mappingSchema.GetDefaultValue(@from))
.ToList()
})
.ToList();
var ambiguityMappings =
from c in cases
from a in c.attrs
group c by a into g
where g.Count() > 1
select g;
var ambiguityMapping = ambiguityMappings.FirstOrDefault();
if (ambiguityMapping != null)
{
var enums = ambiguityMapping.ToArray();
return Expression.Convert(
Expression.Call(
_throwLinqToDBConvertException,
Expression.Constant(
"Mapping ambiguity. MapValue({0}) attribute is defined for both '{1}.{2}' and '{1}.{3}'."
.Args(ambiguityMapping.Key, to.FullName, enums[0].value, enums[1].value))),
to);
}
var expr = Expression.Switch(
expression,
Expression.Convert(
Expression.Call(_defaultConverter,
Expression.Convert(expression, typeof(object)),
Expression.Constant(to)),
to),
cases
.Select(f =>
Expression.SwitchCase(
Expression.Constant(f.value),
(IEnumerable<Expression>)f.attrs.Select(a => Expression.Constant(a, @from))))
.ToArray());
return expr;
}
if (fromTypeFields.Any(f => f.attrs.Count(a => a.Value != null) != 0))
{
var field = fromTypeFields.First(f => f.attrs.Count == 0);
return Expression.Convert(
Expression.Call(
_throwLinqToDBConvertException,
Expression.Constant(
"Inconsistent mapping. '{0}.{1}' does not have MapValue(<{2}>) attribute."
.Args(to.FullName, field.OrigValue, from.FullName))),
to);
}
}
return null;
}
示例4: GetParseEnum
static Expression GetParseEnum(Type from, Type to, Expression p)
{
if (from == typeof(string) && to.IsEnumEx())
{
#if SL4
return
Expression.Call(
MemberHelper.MethodOf(() => Enum.Parse(to, "", true)),
Expression.Constant(to),
p,
Expression.Constant(true));
#else
var values = Enum.GetValues(to);
var names = Enum.GetNames (to);
var dic = new Dictionary<string,object>();
for (var i = 0; i < values.Length; i++)
{
var val = values.GetValue(i);
var lv = (long)Convert.ChangeType(val, typeof(long)
#if !NETFX_CORE
, Thread.CurrentThread.CurrentCulture
#endif
);
dic[lv.ToString()] = val;
if (lv > 0)
dic["+" + lv.ToString()] = val;
}
for (var i = 0; i < values.Length; i++)
dic[names[i].ToLowerInvariant()] = values.GetValue(i);
for (var i = 0; i < values.Length; i++)
dic[names[i]] = values.GetValue(i);
var cases =
from v in dic
group v.Key by v.Value
into g
select Expression.SwitchCase(Expression.Constant(g.Key), g.Select(Expression.Constant));
var expr = Expression.Switch(
p,
Expression.Convert(
Expression.Call(_defaultConverter,
Expression.Convert(p, typeof(string)),
Expression.Constant(to)),
to),
cases.ToArray());
return expr;
#endif
}
return null;
}
示例5: 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 })
//.........这里部分代码省略.........