static PropertyGenInfo ToPropertyGenInfo(FieldInfo fieldInfo) { var result = new PropertyGenInfo() { Name = fieldInfo.Name, TypeName = fieldInfo.FieldType.GetFriendlyName(), IsStatic = fieldInfo.IsStatic, HasGetter = true, HasSetter = !fieldInfo.IsInitOnly && !fieldInfo.IsLiteral, }; FillEnumInfo(result, fieldInfo.FieldType); return result; }
static PropertyGenInfo ToPropertyGenInfo(PropertyInfo propertyInfo) { var getMethod = propertyInfo.GetGetMethod(); var setMethod = propertyInfo.GetSetMethod(); bool isStatic = getMethod == null ? setMethod.IsStatic : getMethod.IsStatic; var result = new PropertyGenInfo() { Name = propertyInfo.Name, TypeName = propertyInfo.PropertyType.GetFriendlyName(), IsStatic = isStatic, HasGetter = getMethod != null && getMethod.IsPublic, HasSetter = setMethod != null && setMethod.IsPublic, }; FillEnumInfo(result, propertyInfo.PropertyType); return result; }
public static TypeGenInfo FromType(Type type, List <Type> genTypes) { // 关于懒绑定的成员函数:先全部丢进lazy收集器中。尔后如果发现有同名方法是不lazy的,那么它也要变成不lazy // 做这个事情的原因是目前还没法做到重载级别的lazy。 LazyMemberCollector lazyCollector = new LazyMemberCollector(); var methodLists = Puerts.Utils.GetMethodAndOverrideMethod(type, Utils.Flags) .Where(m => !Utils.IsNotSupportedMember(m)) .Where(m => !m.IsSpecialName && Puerts.Utils.IsNotGenericOrValidGeneric(m)) .Where(m => { BindingMode mode = Utils.getBindingMode(m); if (mode == BindingMode.DontBinding) { return(false); } if (mode == BindingMode.LazyBinding) { lazyCollector.Add(m); } return(true); }) .ToArray(); var extensionMethodsList = Puerts.Editor.Generator.Utils.GetExtensionMethods(type, new HashSet <Type>(genTypes)); if (extensionMethodsList != null) { extensionMethodsList = new List <MethodInfo>(extensionMethodsList) .Where(m => !Utils.IsNotSupportedMember(m)) .Where(m => !m.IsGenericMethodDefinition || Puerts.Utils.IsNotGenericOrValidGeneric(m)).ToArray(); if (genTypes != null) { extensionMethodsList = extensionMethodsList.Where(m => genTypes.Contains(m.DeclaringType)).ToArray(); } extensionMethodsList .Where(m => { BindingMode mode = Utils.getBindingMode(m); if (mode == BindingMode.DontBinding) { return(false); } if (mode == BindingMode.LazyBinding) { lazyCollector.Add(m); } return(true); }); } foreach (var m in methodLists) { if (lazyCollector.Contains(m.Name) && Utils.getBindingMode(m) != BindingMode.LazyBinding) { lazyCollector.Remove(m.Name); } } if (extensionMethodsList != null) { foreach (var m in extensionMethodsList) { if (lazyCollector.Contains(m.Name) && Utils.getBindingMode(m) != BindingMode.LazyBinding) { lazyCollector.Remove(m.Name); } } } var methodGroups = methodLists .Where(m => !lazyCollector.Contains(m.Name)) .GroupBy(m => new MethodKey { Name = m.Name, IsStatic = m.IsStatic }) .ToDictionary(i => i.Key, i => i.Cast <MethodBase>().ToList()); var extensionMethodGroup = extensionMethodsList != null?extensionMethodsList .Where(m => !lazyCollector.Contains(m.Name)) .GroupBy(m => new MethodKey { Name = m.Name, IsStatic = false }) .ToDictionary(i => i.Key, i => i.Cast <MethodBase>().ToList()) : new Dictionary <MethodKey, List <MethodBase> >(); var indexs = type.GetProperties(Utils.Flags) .Where(m => !Utils.IsNotSupportedMember(m)) .Where(p => p.GetIndexParameters().GetLength(0) == 1) .Select(p => IndexGenInfo.FromPropertyInfo(p)) .ToArray(); var operatorGroups = type.GetMethods(Utils.Flags) .Where(m => !Utils.IsNotSupportedMember(m) && m.IsSpecialName && m.Name.StartsWith("op_") && m.IsStatic) .Where(m => { if (m.Name == "op_Explicit" || m.Name == "op_Implicit") { lazyCollector.Add(m); return(false); } BindingMode mode = Utils.getBindingMode(m); if (mode == BindingMode.DontBinding) { return(false); } if (mode == BindingMode.LazyBinding) { lazyCollector.Add(m); return(false); } return(true); }) .GroupBy(m => new MethodKey { Name = m.Name, IsStatic = m.IsStatic }) .Select(i => i.Cast <MethodBase>().ToList()); var constructors = type.GetConstructors(Utils.Flags) .Where(m => !Utils.IsNotSupportedMember(m)) .Where(m => { BindingMode mode = Utils.getBindingMode(m); if (mode == BindingMode.DontBinding) { return(false); } if (mode == BindingMode.LazyBinding) { lazyCollector.Add(m); return(false); } return(true); }) .Cast <MethodBase>() .ToList(); return(new TypeGenInfo { WrapClassName = Utils.GetWrapTypeName(type), Namespaces = (extensionMethodsList != null ? extensionMethodsList .Select(m => m.DeclaringType.Namespace) .Where(name => !string.IsNullOrEmpty(name)) : new string[0]) .Concat(new[] { "System" }) .Distinct() .ToArray(), Name = type.GetFriendlyName(), IsValueType = type.IsValueType, Methods = methodGroups .Select(kv => { List <MethodBase> exOverloads = null; extensionMethodGroup.TryGetValue(kv.Key, out exOverloads); extensionMethodGroup.Remove(kv.Key); return MethodGenInfo.FromType(type, false, kv.Value, exOverloads); }) .Concat( extensionMethodGroup.Select(kv => MethodGenInfo.FromType(type, false, null, kv.Value)) ) .ToArray(), Constructor = !type.IsAbstract ? MethodGenInfo.FromType(type, true, constructors) : null, Properties = type.GetProperties(Utils.Flags) .Where(p => !Utils.IsNotSupportedMember(p)) .Where(p => !p.IsSpecialName && p.GetIndexParameters().GetLength(0) == 0) .Where(p => { BindingMode mode = Utils.getBindingMode(p); if (mode == BindingMode.DontBinding) { return false; } if (mode == BindingMode.LazyBinding) { lazyCollector.Add(p); return false; } return true; }) .Select(p => PropertyGenInfo.FromPropertyInfo(p)) .Concat( type.GetFields(Utils.Flags) .Where(f => !Utils.IsNotSupportedMember(f)) .Where(f => { BindingMode mode = Utils.getBindingMode(f); if (mode == BindingMode.DontBinding) { return false; } if (mode == BindingMode.LazyBinding) { lazyCollector.Add(f); return false; } return true; }) .Select(f => PropertyGenInfo.FromFieldInfo(f)) ) .ToArray(), GetIndexs = indexs.Where(i => i.HasGetter).ToArray(), SetIndexs = indexs.Where(i => i.HasSetter).ToArray(), Operators = operatorGroups.Select(o => MethodGenInfo.FromType(type, false, o)).ToArray(), Events = type.GetEvents(Utils.Flags) .Where(m => !Utils.IsNotSupportedMember(m)) .Where(e => { BindingMode mode = Utils.getBindingMode(e); if (mode == BindingMode.DontBinding) { return false; } if (mode == BindingMode.LazyBinding) { var adder = e.GetAddMethod(); var remover = e.GetRemoveMethod(); if (adder != null && adder.IsPublic) { lazyCollector.Add(adder); } if (remover != null && remover.IsPublic) { lazyCollector.Add(remover); } return false; } return true; }) .Select(e => EventGenInfo.FromEventInfo(e)) .ToArray(), LazyMembers = lazyCollector.ToArray() }); }