Example #1
0
        /// <summary>
        /// Creates a TsCollection from TsType
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private TsCollection CreateCollectionType(TsType type, TsPropertyVisibilityFormatter propertyVisibilityFormatter)
        {
            var resolved = new TsCollection(type.Type, propertyVisibilityFormatter);

            resolved.ItemsType = this.ResolveType(resolved.ItemsType, propertyVisibilityFormatter, false);
            return(resolved);
        }
Example #2
0
        /// <summary>
        /// Creates a TsCollection from TsType
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private TsCollection CreateCollectionType(TsType type)
        {
            var resolved = new TsCollection(type.Type);

            resolved.ItemsType = this.ResolveType(resolved.ItemsType, false);
            return(resolved);
        }
Example #3
0
 /// <summary>
 /// Recursively finds the module name for the underlaying ItemsType of a TsCollection.
 /// </summary>
 /// <param name="collectionType">The TsCollection object.</param>
 /// <param name="moduleName">The module name.</param>
 /// <returns></returns>
 public string GetCollectionModuleName(TsCollection collectionType, string moduleName)
 {
     if (collectionType.ItemsType as TsModuleMember != null && !_typeConvertors.IsConvertorRegistered(collectionType.ItemsType.Type))
     {
         if (!collectionType.ItemsType.Type.IsGenericParameter)
         {
             moduleName = ((TsModuleMember)collectionType.ItemsType).Module != null?GetModuleName(((TsModuleMember)collectionType.ItemsType).Module) : string.Empty;
         }
     }
     if (collectionType.ItemsType as TsCollection != null)
     {
         moduleName = GetCollectionModuleName((TsCollection)collectionType.ItemsType, moduleName);
     }
     return(moduleName);
 }
        /// <summary>
        /// Recursively finds the module name for the underlaying ItemsType of a TsCollection.
        /// </summary>
        /// <param name="collectionType">The TsCollection object.</param>
        /// <param name="moduleName">The module name.</param>
        /// <returns></returns>
        public string GetCollectionModuleName(TsCollection collectionType, string moduleName)
        {
            var type = collectionType.ItemsType as TsModuleMember;

            if (type != null && !Options.TypeConverters.IsConverterRegistered(type.Type))
            {
                if (!type.Type.IsGenericParameter)
                {
                    moduleName = type.Module != null?GetModuleName(type.Module) : string.Empty;
                }
            }

            var collection = collectionType.ItemsType as TsCollection;

            if (collection != null)
            {
                moduleName = GetCollectionModuleName(collection, moduleName);
            }
            return(moduleName);
        }
        public void WhenInitializedWithUnyypedCollection_ItemsTypeIsSetToAny()
        {
            var target = new TsCollection(typeof(IList));

            Assert.Equal(TsType.Any, target.ItemsType);
        }
        public void WhenInitializedWithTypedCollection_ItemsTypeIsSetToGenericParameter()
        {
            var target = new TsCollection(typeof(List<Address>));

            Assert.Equal(typeof(Address), target.ItemsType.Type);
        }
        public void WhenInitializedWithUnyypedCollection_ItemsTypeIsSetToAny()
        {
            var target = new TsCollection(typeof(IList));

            Assert.Equal(TsType.Any, target.ItemsType);
        }
        public void WhenInitializedWithTypedCollection_ItemsTypeIsSetToGenericParameter()
        {
            var target = new TsCollection(typeof(List <Address>));

            Assert.Equal(typeof(Address), target.ItemsType.Type);
        }
 /// <summary>
 /// Creates a TsCollection from TsType
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 private TsCollection CreateCollectionType(TsType type)
 {
     var resolved = new TsCollection(type.Type);
     resolved.ItemsType = this.ResolveType(resolved.ItemsType, false);
     return resolved;
 }
Example #10
0
        private void MapToTsType(Type type)
        {
            if (mTypeToTsTypeMap.ContainsKey(type))
            {
                return;
            }
            var foundMapping = mAllMappings.FirstOrDefault(m => m.MatchesType.Invoke(type));

            if (foundMapping != null)
            {
                mTypeToTsTypeMap[type] = new TsMappedType(foundMapping.DestinationType, foundMapping.DestinationAssignmentTemplate);
            }
            else
            {
                Type itemType;
                Type keyItemType;
                Type valueItemType;

                if (IsDictionary(type, out keyItemType, out valueItemType))
                {
                    MapToTsType(keyItemType);
                    MapToTsType(valueItemType);
                    mTypeToTsTypeMap[type] = new TsDictionary(mTypeToTsTypeMap[keyItemType], mTypeToTsTypeMap[valueItemType]);
                }
                else if (IsIEnumerableType(type, out itemType))
                {
                    MapToTsType(itemType);
                    mTypeToTsTypeMap[type] = new TsCollection(mTypeToTsTypeMap[itemType]);
                }
                else if (type.IsEnum)
                {
                    var enumType    = new TsEnum(type.Namespace, type.Name);
                    var names       = Enum.GetNames(type);
                    var valuesArray = Enum.GetValues(type);
                    var values      = valuesArray.Cast <object>().Select(v => Convert.ToInt64(v)).ToArray();
                    for (int i = 0; i < names.Length; i++)
                    {
                        enumType.Members.Add(new KeyValuePair <string, long>(names[i], values[i]));
                    }
                    mTypeToTsTypeMap[type] = enumType;
                }
                // nullable enum
                else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>) &&
                         type.GetGenericArguments()[0].IsEnum)
                {
                    var enumType    = new TsEnum(type.GetGenericArguments()[0].Namespace, type.GetGenericArguments()[0].Name);
                    var names       = Enum.GetNames(type.GetGenericArguments()[0]);
                    var valuesArray = Enum.GetValues(type.GetGenericArguments()[0]);
                    var values      = valuesArray.Cast <object>().Select(v => Convert.ToInt64(v)).ToArray();
                    for (int i = 0; i < names.Length; i++)
                    {
                        enumType.Members.Add(new KeyValuePair <string, long>(names[i], values[i]));
                    }
                    mTypeToTsTypeMap[type] = enumType;
                }
                else
                {
                    // Check if current TypeWithProperties has a base class that is not object
                    TsType baseTsType = null;
                    if (type.IsClass && type != typeof(Object) && type.BaseType != typeof(Object))
                    {
                        MapToTsType(type.BaseType);
                        baseTsType = mTypeToTsTypeMap[type.BaseType];
                    }

                    var tsType = new TsTypeWithProperties(type.Namespace, type.Name, baseTsType);
                    mTypeToTsTypeMap[type] = tsType;

                    var propertyInfos = type
                                        .GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public)
                                        .Where(p => p.GetMethod.IsPublic && !p.GetMethod.GetParameters().Any());

                    // iterate properties.
                    foreach (var prop in propertyInfos)
                    {
                        MapToTsType(prop.PropertyType);
                        tsType.Properties.Add(new TsProperty {
                            Name = prop.Name, TsType = mTypeToTsTypeMap[prop.PropertyType]
                        });
                    }
                }
            }
        }