private static bool IsSupportedDictionaryType(Type type, out ITypeInfo typeWrapper, out Type keyType, out Type valueType) { keyType = valueType = null; typeWrapper = null; // Type must implement both IDictionary<TKey,TValue> and IDictionary if (!(Utils.ImplementsInterface(type, typeof(IDictionary <,>)) && Utils.ImplementsInterface(type, typeof(IDictionary)))) { return(false); } typeWrapper = TypeFactory.GetTypeInfo(type); var genericArguments = typeWrapper.GetGenericArguments(); if (genericArguments.Length != 2) { return(false); } keyType = genericArguments[0]; valueType = genericArguments[1]; if (keyType != typeof(string) || valueType == typeof(object)) { return(false); } return(true); }
protected override TypeScriptType ReferenceFromInternal(ITypeInfo type, TypeScriptUnit targetUnit, ITypeGenerator typeGenerator) { var genericArgs = type.GetGenericArguments(); var keyType = typeGenerator.BuildAndImportType(targetUnit, genericArgs[0]); var valueType = typeGenerator.BuildAndImportType(targetUnit, genericArgs[1]); if (typeGenerator.Options.NullabilityMode != NullabilityMode.NullableReference) { keyType = keyType.NotNull(); valueType = valueType.NotNull(); } return(new TypeScriptTypeDefintion { Members = { new TypeScriptTypePropertyGetterDeclaration { Argument = new TypeScriptArgumentDeclaration { Name = "key", Type = keyType, }, ResultType = valueType, Optional = true, } } }); }
protected override TypeScriptType ReferenceFromInternal(ITypeInfo type, TypeScriptUnit targetUnit, ITypeGenerator typeGenerator) { var elementType = type.GetGenericArguments()[0]; var itemType = typeGenerator.BuildAndImportType(targetUnit, elementType); return(new TypeScriptArrayType(itemType)); }
protected override TypeScriptType ReferenceFromInternal(ITypeInfo type, TypeScriptUnit targetUnit, ITypeGenerator typeGenerator) { var itemType = type.IsGenericType ? type.GetGenericArguments()[0] : TypeInfo.From(typeof(void)); var itemTypeScriptType = typeGenerator.BuildAndImportType(targetUnit, itemType); return(new TypeScriptPromiseOfType(itemTypeScriptType)); }
public static bool IsContext(this ITypeInfo type) { return(!type.IsAbstract && !type.GetCustomAttributes(FullNames.BehaviorsAttribute, false).Any() && !type.GetGenericArguments().Any() && type.GetFields().Any(x => x.IsSpecification() || x.IsBehavior())); }
public static string ToSimpleString(this ITypeInfo type) { Guard.AgainstNullArgument("type", type); var baseTypeName = type.Name; var backTickIdx = baseTypeName.IndexOf('`'); if (backTickIdx >= 0) { baseTypeName = baseTypeName.Substring(0, backTickIdx); } var lastIndex = baseTypeName.LastIndexOf('.'); if (lastIndex >= 0) { baseTypeName = baseTypeName.Substring(lastIndex + 1); } if (!type.IsGenericType) { return(baseTypeName); } var genericTypes = type.GetGenericArguments().ToArray(); var simpleNames = new string[genericTypes.Length]; for (var idx = 0; idx < genericTypes.Length; idx++) { simpleNames[idx] = ToSimpleString(genericTypes[idx]); } return(string.Format(CultureInfo.InvariantCulture, "{0}<{1}>", baseTypeName, string.Join(", ", simpleNames))); }
static string ConvertToSimpleTypeName(ITypeInfo type) { var baseTypeName = type.Name; int backTickIdx = baseTypeName.IndexOf('`'); if (backTickIdx >= 0) { baseTypeName = baseTypeName.Substring(0, backTickIdx); } var lastIndex = baseTypeName.LastIndexOf('.'); if (lastIndex >= 0) { baseTypeName = baseTypeName.Substring(lastIndex + 1); } if (!type.IsGenericType) { return(baseTypeName); } ITypeInfo[] genericTypes = type.GetGenericArguments().ToArray(); string[] simpleNames = new string[genericTypes.Length]; for (int idx = 0; idx < genericTypes.Length; idx++) { simpleNames[idx] = ConvertToSimpleTypeName(genericTypes[idx]); } return(String.Format("{0}<{1}>", baseTypeName, String.Join(", ", simpleNames))); }
/// <summary> /// Converts a type into a name string. /// </summary> /// <param name="type">The type to convert.</param> /// <returns>Name string of type.</returns> public static string ConvertToSimpleTypeName(ITypeInfo type) { var baseTypeName = type.Name; var backTickIdx = baseTypeName.IndexOf('`'); if (backTickIdx >= 0) { baseTypeName = baseTypeName.Substring(0, backTickIdx); } var lastIndex = baseTypeName.LastIndexOf('.'); if (lastIndex >= 0) { baseTypeName = baseTypeName.Substring(lastIndex + 1); } if (!type.IsGenericType) { return(baseTypeName); } var genericTypes = type.GetGenericArguments().ToArray(); var simpleNames = new string[genericTypes.Length]; for (var idx = 0; idx < genericTypes.Length; idx++) { simpleNames[idx] = ConvertToSimpleTypeName(genericTypes[idx]); } return($"{baseTypeName}<{string.Join(", ", simpleNames)}>"); }
public static int GetGenericArgumentsToSkip(ITypeInfo type) { if (type.IsArray) { return(1 + GetGenericArgumentsToSkip(type.GetElementType())); } if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(Nullable <>)))) { return(0); } if (!type.IsGenericType) { return(type.IsValueType ? 0 : 1); } var count = 1; foreach (var argument in type.GetGenericArguments()) { count += GetGenericArgumentsToSkip(argument); } return(count); }
public static bool Equals(ITypeInfo self, ITypeInfo other) { if (self == null && other == null) { return(true); } if (self == null || other == null) { return(false); } if (self.IsArray) { return(other.IsArray && self.GetElementType().Equals(other.GetElementType())); } if (self.IsGenericParameter) { return(other.IsGenericParameter && self.Name == other.Name); } return(self.Name == other.Name && self.Namespace == other.Namespace && self.IsGenericTypeDefinition == other.IsGenericTypeDefinition && GenericArgumentsEquals(self.GetGenericArguments(), other.GetGenericArguments())); }
protected override TypeScriptType ReferenceFromInternal(ITypeInfo type, TypeScriptUnit targetUnit, ITypeGenerator typeGenerator) { var typeReference = typeGenerator.BuildAndImportType(targetUnit, type.GetGenericTypeDefinition()); return(new TypeScriptGenericTypeReference( (TypeScriptTypeReference)typeReference, type.GetGenericArguments().Select(x => GetArgumentType(x, typeGenerator, targetUnit)).ToArray() )); }
private TypeScriptType GetTypeScriptType(TypeScriptUnit targetUnit, ITypeInfo typeInfo, IAttributeProvider?attributeProvider) { if (typeDeclarations.ContainsKey(typeInfo)) { return(typeDeclarations[typeInfo].ReferenceFrom(targetUnit, this, attributeProvider)); } if (typeInfo.IsGenericTypeDefinition && typeInfo.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(Nullable <>)))) { return(new TypeScriptNullableType(GetTypeScriptType(targetUnit, typeInfo.GetGenericArguments()[0], null))); } return(ResolveType(typeInfo).ReferenceFrom(targetUnit, this, attributeProvider)); }
private ITypeBuildingContext GetTypeBuildingContext(string typeLocation, ITypeInfo typeInfo) { if (BuiltinTypeBuildingContext.Accept(typeInfo)) { return(new BuiltinTypeBuildingContext(typeInfo)); } if (ArrayTypeBuildingContext.Accept(typeInfo)) { return(new ArrayTypeBuildingContext(typeInfo, Options)); } if (DictionaryTypeBuildingContext.Accept(typeInfo)) { return(new DictionaryTypeBuildingContext(typeInfo, Options)); } if (typeInfo.IsEnum) { var targetUnit = typeUnitFactory.GetOrCreateTypeUnit(typeLocation); return(Options.EnumGenerationMode == EnumGenerationMode.FixedStringsAndDictionary ? (ITypeBuildingContext) new FixedStringsAndDictionaryTypeBuildingContext(targetUnit, typeInfo) : new TypeScriptEnumTypeBuildingContext(targetUnit, typeInfo)); } if (typeInfo.IsGenericType && !typeInfo.IsGenericTypeDefinition && typeInfo.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(Nullable <>)))) { var underlyingType = typeInfo.GetGenericArguments().Single(); if (Options.EnableExplicitNullability) { return(new NullableTypeBuildingContext(underlyingType, Options.UseGlobalNullable)); } return(GetTypeBuildingContext(typeLocation, underlyingType)); } if (typeInfo.IsGenericType && !typeInfo.IsGenericTypeDefinition) { return(new GenericTypeTypeBuildingContext(typeInfo, Options)); } if (typeInfo.IsGenericParameter) { return(new GenericParameterTypeBuildingContext(typeInfo)); } if (typeInfo.IsGenericTypeDefinition) { return(new CustomTypeTypeBuildingContext(typeUnitFactory.GetOrCreateTypeUnit(typeLocation), typeInfo)); } return(new CustomTypeTypeBuildingContext(typeUnitFactory.GetOrCreateTypeUnit(typeLocation), typeInfo)); }
protected override TypeScriptType ReferenceFromInternal(ITypeInfo type, TypeScriptUnit targetUnit, ITypeGenerator typeGenerator) { var itemTypeScriptType = typeGenerator.BuildAndImportType(targetUnit, type.GetGenericArguments()[0]); if (typeGenerator.Options.NullabilityMode == NullabilityMode.None) { return(itemTypeScriptType); } return(typeGenerator.Options.UseGlobalNullable ? (TypeScriptType) new TypeScriptNullableType(itemTypeScriptType) : new TypeScriptOrNullType(itemTypeScriptType)); }
protected virtual ITypeInfo ResolveReturnType(ITypeInfo typeInfo) { if (typeInfo.IsGenericType) { var genericTypeDefinition = typeInfo.GetGenericTypeDefinition(); if (genericTypeDefinition.Equals(TypeInfo.From(typeof(Task<>)))) return ResolveReturnType(typeInfo.GetGenericArguments()[0]); } if (typeInfo.Equals(TypeInfo.From<Task>())) return TypeInfo.From(typeof(void)); return typeInfo; }
private ITypeInfo GetElementType(ITypeInfo arrayType) { if (arrayType.IsArray) { return(arrayType.GetElementType() ?? throw new ArgumentNullException($"Array type's {arrayType.Name} element type is not defined")); } if (arrayType.IsGenericType && arrayType.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(List <>)))) { return(arrayType.GetGenericArguments()[0]); } throw new ArgumentException("arrayType should be either Array or List<T>", nameof(arrayType)); }
private static ITypeInfo GetTypeFromGenericArgumentReferenceType(IMetadataGenericArgumentReferenceType reference, ITypeInfo typeInfo, IMethodInfo methodInfo) { var argument = reference.Argument; switch (argument.Kind) { case GenericArgumentKind.Type: return typeInfo.GetGenericArguments().ToList()[(int)argument.Index]; case GenericArgumentKind.Method: return methodInfo.GetGenericArguments().ToList()[(int)argument.Index]; } return null; }
public static (bool, ITypeInfo) ProcessNullable(IAttributeProvider?attributeProvider, ITypeInfo type, NullabilityMode nullabilityMode) { if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(Nullable <>)))) { var underlyingType = type.GetGenericArguments()[0]; return(true, underlyingType); } if (attributeProvider == null || !type.IsClass && !type.IsInterface) { return(false, type); } return(CanBeNull(attributeProvider, nullabilityMode), type); }
private static ITypeInfo GetTypeFromGenericArgumentReferenceType(IMetadataGenericArgumentReferenceType reference, ITypeInfo typeInfo, IMethodInfo methodInfo) { var argument = reference.Argument; switch (argument.Kind) { case GenericArgumentKind.Type: return(typeInfo.GetGenericArguments().ToList()[(int)argument.Index]); case GenericArgumentKind.Method: return(methodInfo.GetGenericArguments().ToList()[(int)argument.Index]); } return(null); }
protected override ITypeInfo ResolveReturnType(ITypeInfo typeInfo) { if (typeInfo.IsGenericType) { var genericTypeDefinition = typeInfo.GetGenericTypeDefinition(); if (genericTypeDefinition.Equals(TypeInfo.From(typeof(Task <>))) || genericTypeDefinition.Equals(TypeInfo.From(typeof(ActionResult <>)))) { return(ResolveReturnType(typeInfo.GetGenericArguments()[0])); } } if (typeInfo.Equals(TypeInfo.From <Task>()) || typeInfo.Equals(TypeInfo.From <ActionResult>())) { return(TypeInfo.From(typeof(void))); } return(typeInfo); }
static string ConvertToSimpleTypeName(ITypeInfo type) { var baseTypeName = type.Name; var backTickIdx = baseTypeName.IndexOf('`'); if (backTickIdx >= 0) baseTypeName = baseTypeName.Substring(0, backTickIdx); var lastIndex = baseTypeName.LastIndexOf('.'); if (lastIndex >= 0) baseTypeName = baseTypeName.Substring(lastIndex + 1); if (!type.IsGenericType) return baseTypeName; var genericTypes = type.GetGenericArguments().ToArray(); var simpleNames = new string[genericTypes.Length]; for (var idx = 0; idx < genericTypes.Length; idx++) simpleNames[idx] = ConvertToSimpleTypeName(genericTypes[idx]); return $"{baseTypeName}<{string.Join(", ", simpleNames)}>"; }
public DictionaryTypeBuildingContext(ITypeInfo dictionaryType, TypeScriptGenerationOptions options) { keyType = dictionaryType.GetGenericArguments()[0]; valueType = dictionaryType.GetGenericArguments()[1]; this.options = options; }
public CollectionTypeBuildingContext(ITypeInfo arrayType) { elementType = arrayType.GetGenericArguments()[0]; }
static string ConvertToSimpleTypeName(ITypeInfo type) { var baseTypeName = type.Name; int backTickIdx = baseTypeName.IndexOf('`'); if (backTickIdx >= 0) baseTypeName = baseTypeName.Substring(0, backTickIdx); var lastIndex = baseTypeName.LastIndexOf('.'); if (lastIndex >= 0) baseTypeName = baseTypeName.Substring(lastIndex + 1); if (!type.IsGenericType) return baseTypeName; ITypeInfo[] genericTypes = type.GetGenericArguments().ToArray(); string[] simpleNames = new string[genericTypes.Length]; for (int idx = 0; idx < genericTypes.Length; idx++) simpleNames[idx] = ConvertToSimpleTypeName(genericTypes[idx]); return String.Format("{0}<{1}>", baseTypeName, String.Join(", ", simpleNames)); }
public static bool Accept(ITypeInfo typeInfo) { return(typeInfo.IsGenericType && typeInfo.GetGenericArguments().Length == 1 && typeInfo.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(ICollection <>))))); }
public IEnumerable <ITypeInfo> GetGenericArguments() { return(inner.GetGenericArguments()); }
public IEnumerable <ITypeInfo> GetGenericArguments() { return(_typeInfoImplementation.GetGenericArguments()); }