Ejemplo n.º 1
0
 /// <summary>
 /// 解析出一个类的分析参数以及泛型约束
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public static Dictionary <string, IEnumerable <GenericeConstraintInfo> > GetGenericeParam(this Type type)
 {
     return(GenericeAnalysis.GetConstrains(type));
 }
Ejemplo n.º 2
0
        private static IEnumerable <GenericeConstraintInfo> GetOneConstrain(Type paramter, IEnumerable <Type> constraintType, GenericParameterAttributes attributes)
        {
            // struct unmanaged
            var only = IsStructOrUnmanaged(constraintType.ToArray(), attributes);

            if (only.IsThis)
            {
                return new GenericeConstraintInfo[] { new GenericeConstraintInfo(only.Keyword, only.Location, only.Value) }
            }
            ;

            List <GenericeConstraintInfo> list = new List <GenericeConstraintInfo>();
            var types = constraintType.GetEnumerator();

            #region 第一位置

            // classs
            if ((attributes | GenericParameterAttributes.ReferenceTypeConstraint) == attributes)
            {
                list.Add(new GenericeConstraintInfo(GenericKeyword.Class, ConstraintLocation.First, "class"));

                attributes = attributes ^ GenericParameterAttributes.ReferenceTypeConstraint;
            }

            // notnull
            else if (!constraintType.Any() && attributes == GenericParameterAttributes.None)
            {
                // notnull
                // System.Runtime.CompilerServices.NullableAttribute
                if (paramter.GetCustomAttributes().Any(x => x.GetType().Name.CompareTo("NullableAttribute") == 0))
                {
                    list.Add(new GenericeConstraintInfo(GenericKeyword.Notnull, ConstraintLocation.First, "notnull"));
                }
                else
                {
                    return new GenericeConstraintInfo[] { new GenericeConstraintInfo(GenericKeyword.NoConstrant, ConstraintLocation.None, string.Empty) }
                };
            }

            // <baseclass>
            else if (constraintType.FirstOrDefault(x => !x.IsInterface && x != typeof(object)) is var baseClassType && baseClassType != null)
            {
                list.Add(new GenericeConstraintInfo(GenericKeyword.BaseClass, ConstraintLocation.First, GenericeAnalysis.GetGenriceName(baseClassType)));
                types.MoveNext();
            }

            #endregion

            #region 中间位置

            if (constraintType.Any())
            {
                while (types.MoveNext())
                {
                    var item = types.Current;

                    list.Add(
                        new GenericeConstraintInfo(item.IsInterface ? GenericKeyword.Interface : GenericKeyword.TU,
                                                   ConstraintLocation.Any,
                                                   GenericeAnalysis.GetGenriceName(item))
                        );
                }
            }

            #endregion

            // new
            if ((attributes | GenericParameterAttributes.DefaultConstructorConstraint) == attributes)
            {
                list.Add(new GenericeConstraintInfo(GenericKeyword.New, ConstraintLocation.End, "new()"));
            }

            return(list);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 获取泛型类的名称
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public static string GetGenericeName(this Type type)
 {
     return(GenericeAnalysis.GetGenriceName(type));
 }