/// <summary> /// Initializes a new instance of the TsCollection class with the specific CLR type. /// </summary> /// <param name="type">The CLR collection represented by this instance of the TsCollection.</param> public TsCollection(Type type) : base(type) { var enumerableType = TsType.GetEnumerableType(this.Type); if (enumerableType != null) { this.ItemsType = TsType.Create(enumerableType); } else if (typeof(IEnumerable).IsAssignableFrom(this.Type)) { this.ItemsType = TsType.Any; } else { throw new ArgumentException(string.Format("The type '{0}' is not collection.", this.Type.FullName)); } this.Dimension = GetCollectionDimension(type); }
/// <summary> /// Initializes a new instance of the TsCollection class with the specific CLR type. /// </summary> /// <param name="type">The CLR collection represented by this instance of the TsCollection.</param> public TsCollection(Type type, TsPropertyVisibilityFormatter propertyVisibilityFormatter) : base(type) { var enumerableType = TsType.GetEnumerableType(this.Type); if (enumerableType != null) { this.ItemsType = TsType.Create(enumerableType, propertyVisibilityFormatter); } else if (typeof(IEnumerable).IsAssignableFrom(this.Type)) { this.ItemsType = TsType.Any; } else { throw new ArgumentException($"The type '{this.Type.FullName}' is not collection."); } this.Dimension = GetCollectionDimension(type); }
/// <summary> /// Initializes a new instance of the TsClass class with the specific CLR type. /// </summary> /// <param name="type">The CLR type represented by this instance of the TsClass</param> /// <param name="propertyVisibilityFormatter"></param> public TsClass(Type type, TsPropertyVisibilityFormatter propertyVisibilityFormatter) : base(type) { this.Properties = this.Type .GetProperties() .Where(pi => pi.DeclaringType == this.Type) .Select(pi => new TsProperty(pi)) .Where(tp => propertyVisibilityFormatter(tp)) .ToList(); this.Fields = this.Type .GetFields() .Where(fi => fi.DeclaringType == this.Type && !(fi.IsLiteral && !fi.IsInitOnly)) // skip constants .Select(fi => new TsProperty(fi)) .Where(tp => propertyVisibilityFormatter(tp)) .ToList(); this.Constants = this.Type .GetFields() .Where(fi => fi.DeclaringType == this.Type && fi.IsLiteral && !fi.IsInitOnly) // constants only .Select(fi => new TsProperty(fi)) .Where(tp => propertyVisibilityFormatter(tp)) .ToList(); if (type.IsGenericType) { this.Name = type.Name.Remove(type.Name.IndexOf('`')); this.GenericArguments = type .GetGenericArguments() .Select(ty => TsType.Create(ty, propertyVisibilityFormatter)) .ToList(); } else { this.Name = type.Name; this.GenericArguments = new TsType[0]; } if (this.Type.BaseType != null && this.Type.BaseType != typeof(object) && this.Type.BaseType != typeof(ValueType)) { this.BaseType = new TsType(this.Type.BaseType); } var interfaces = this.Type.GetInterfaces(); this.Interfaces = interfaces .Where(@interface => @interface.GetCustomAttribute <TsInterfaceAttribute>(false) != null) .Except(interfaces.SelectMany(@interface => @interface.GetInterfaces())) .Select(ty => TsType.Create(ty, propertyVisibilityFormatter)).ToList(); var attribute = this.Type.GetCustomAttribute <TsClassAttribute>(false); if (attribute != null) { if (!string.IsNullOrEmpty(attribute.Name)) { this.Name = attribute.Name; } if (attribute.Module != null) { this.Module.Name = attribute.Module; } } var ignoreAttribute = this.Type.GetCustomAttribute <TsIgnoreAttribute>(false); if (ignoreAttribute != null) { this.IsIgnored = true; } }