コード例 #1
0
ファイル: Processor.cs プロジェクト: avensia-oss/tstypegen
        private void CreateTypeBuilderConfig()
        {
            var typeMappings = new Dictionary <string, TsTypeReference>();

            void AddTypeMappingsFromAssembly(Assembly asm)
            {
                foreach (var attribute in TypeUtils.GetAssemblyCustomAttributesData(asm).Where(a =>
                                                                                               a.AttributeType.Name == Constants.DefineTypeScriptTypeForExternalTypeAttributeName && a.ConstructorArguments.Count == 2 && a.ConstructorArguments[1].Value is string))
                {
                    string key;
                    if (attribute.ConstructorArguments[0].Value is Type type)
                    {
                        key = TypeUtils.GetFullNameWithGenericArguments(type);
                    }
                    else if (attribute.ConstructorArguments[0].Value is string s)
                    {
                        key = s;
                    }
                    else
                    {
                        continue;
                    }

                    typeMappings[key] = TsTypeReference.Simple((string)attribute.ConstructorArguments[1].Value);
                }
            }

            foreach (var assembly in _generatorContext.Assemblies)
            {
                AddTypeMappingsFromAssembly(assembly);
            }

            // override mappings from config
            foreach (var mapping in _config.TypeMappings)
            {
                var reference = TsTypeReference.Simple(mapping.Value);
                typeMappings[mapping.Key] = reference;
            }

            var mappings = ImmutableDictionary.CreateRange(typeMappings);

            _typeBuilderConfig = new TypeBuilderConfig(mappings, _config.CustomTypeScriptIgnoreAttributeFullName, _config.WrapConstEnumsInTemplateStrings);
        }
コード例 #2
0
ファイル: TypeBuilder.cs プロジェクト: avensia-oss/tstypegen
        internal static bool ShouldGenerateDotNetTypeNamesAsJsDocComment(Type type)
        {
            bool DoShouldGenerateDotNetTypeNamesAsJsDocComment(object typeOrAssembly)
            {
                var customAttributes = typeOrAssembly is Type type?TypeUtils.GetCustomAttributesData(type) : TypeUtils.GetAssemblyCustomAttributesData((Assembly)typeOrAssembly);

                var attr = customAttributes.FirstOrDefault(a => a.AttributeType.Name == Constants.GenerateDotNetTypeNamesAsJsDocCommentAttributeName);

                return(attr != null);
            }

            if (DoShouldGenerateDotNetTypeNamesAsJsDocComment(type.Assembly))
            {
                return(true);
            }

            for (var currentType = type; currentType != null; currentType = currentType.DeclaringType)
            {
                var shouldGenerate = DoShouldGenerateDotNetTypeNamesAsJsDocComment(currentType);
                if (shouldGenerate)
                {
                    return(true);
                }
            }

            return(false);
        }
コード例 #3
0
ファイル: TypeBuilder.cs プロジェクト: avensia-oss/tstypegen
        internal static string GetTypescriptNamespace(Type type)
        {
            var parentToAugument = TypeBuilder.GetParentTypeToAugument(type);

            if (parentToAugument != null)
            {
                type = parentToAugument;
            }

            string DoGetTypescriptNamespace(object typeOrAssembly)
            {
                var customAttributes = typeOrAssembly is Type type?TypeUtils.GetCustomAttributesData(type) : TypeUtils.GetAssemblyCustomAttributesData((Assembly)typeOrAssembly);

                var attr = customAttributes.FirstOrDefault(a => a.AttributeType.Name == Constants.GenerateTypeScriptNamespaceAttributeName && a.ConstructorArguments.Count == 1 && a.ConstructorArguments[0].Value is string);

                return((string)attr?.ConstructorArguments[0].Value);
            }

            for (var currentType = type; currentType != null; currentType = currentType.DeclaringType)
            {
                var ns = DoGetTypescriptNamespace(currentType);
                if (ns != null)
                {
                    return(ns);
                }
            }

            return(DoGetTypescriptNamespace(type.Assembly));
        }