Beispiel #1
0
        private static TsInterfaceMember BuildMember(MemberInfo member, IList <Type> interfaces, TypeBuilderConfig config, string currentTsNamespace)
        {
            var interfaceProperties = interfaces.SelectMany(i => TypeUtils.GetRelevantAndBaseProperties(i).Where(p => p.Name == member.Name));

            var allPropertiesToCheckForIgnore = new List <MemberInfo>();

            allPropertiesToCheckForIgnore.Add(member);
            allPropertiesToCheckForIgnore.AddRange(interfaceProperties);

            var currentType = member.DeclaringType?.BaseType;

            while (currentType != null)
            {
                var baseProperties = TypeUtils.GetRelevantAndBaseProperties(currentType).Where(p => p.Name == member.Name);
                allPropertiesToCheckForIgnore.AddRange(baseProperties);
                currentType = currentType.BaseType;
            }

            foreach (var propertyToCheckForIgnore in allPropertiesToCheckForIgnore)
            {
                var attributes = TypeUtils.GetCustomAttributesData(propertyToCheckForIgnore);
                if (attributes.All(a => a.AttributeType.Name != Constants.TypeScriptTypeAttributeName))
                {
                    if (attributes.Any(a => a.AttributeType.FullName == "Newtonsoft.Json.JsonIgnoreAttribute"))
                    {
                        return(null);
                    }

                    if (attributes.Any(a => a.AttributeType.FullName == config.CustomTypeScriptIgnoreAttributeFullName))
                    {
                        return(null);
                    }

                    if (attributes.Any(a => a.AttributeType.Name == Constants.TypeScriptIgnoreAttributeName))
                    {
                        return(null);
                    }
                }
            }

            string name = FindNameFromJsonPropertyAttribute(member);

            if (string.IsNullOrEmpty(name))
            {
                name = StringUtils.ToCamelCase(member.Name);
            }

            var membersToCheckForOptional = new List <MemberInfo> {
                member
            };

            membersToCheckForOptional.AddRange(member.DeclaringType.GetInterfaces().SelectMany(i => i.GetMember(member.Name)).Where(m => m != null));

            var isOptional = membersToCheckForOptional.SelectMany(m => TypeUtils.GetCustomAttributesData(m)).FirstOrDefault(a => a.AttributeType.Name == Constants.TypeScriptOptionalAttributeName) != null;

            return(new TsInterfaceMember(name, BuildTsTypeReferenceToPropertyType(member, config, currentTsNamespace, isOptional), member, isOptional));
        }
Beispiel #2
0
        private static string FindNameFromJsonPropertyAttribute(MemberInfo member)
        {
            string LookupSingle(MemberInfo m)
            {
                var jsonPropertyAttribute = TypeUtils.GetCustomAttributesData(m).FirstOrDefault(a => a.AttributeType.FullName == "Newtonsoft.Json.JsonPropertyAttribute");

                if (jsonPropertyAttribute != null)
                {
                    if (jsonPropertyAttribute.NamedArguments?.Any(x => x.MemberName == "PropertyName") == true)
                    {
                        return(jsonPropertyAttribute.NamedArguments.First(x => x.MemberName == "PropertyName").TypedValue.Value as string);
                    }
                    else if (jsonPropertyAttribute.ConstructorArguments.Count > 0)
                    {
                        return(jsonPropertyAttribute.ConstructorArguments[0].Value as string);
                    }
                }
                return(null);
            }

            if (LookupSingle(member) is string result)
            {
                return(result);
            }

            foreach (var interfaceProperty in member.DeclaringType?.GetInterfaces().SelectMany(i => TypeUtils.GetRelevantAndBaseProperties(i).Where(p => p.Name == member.Name)) ?? new List <PropertyInfo>())
            {
                if (LookupSingle(interfaceProperty) is string interfaceResult)
                {
                    return(interfaceResult);
                }
            }

            var currentType = member.DeclaringType?.BaseType;

            while (currentType != null)
            {
                var baseProperties = TypeUtils.GetRelevantAndBaseProperties(currentType).Where(p => p.Name == member.Name);
                foreach (var baseProperty in baseProperties)
                {
                    if (LookupSingle(baseProperty) is string baseResult)
                    {
                        return(baseResult);
                    }
                }

                currentType = currentType.BaseType;
            }

            return(null);
        }