Пример #1
0
        public static Method CreateMethod(MethodInfo methodInfo, TsType parent)
        {
            var method = new Method(methodInfo.Name, parent)
            {
                IsStatic = methodInfo.IsStatic
            };

            var parameters = methodInfo.GetParameters();
            foreach (var parameterInfo in parameters)
            {
                method.Parameters.Add(MethodParameterFactory.Create(parameterInfo, method));
            }

            if (methodInfo.IsGenericMethod)
            {
                var genericArguments = methodInfo.GetGenericArguments();
                foreach (var argument in genericArguments)
                {
                    method.Generics.Add(new GenericDefinition(argument.Name));
                }
            }

            if (methodInfo.ReturnType != typeof(void))
            {
                method.Return = new TsUndefined { UnderlyingType = methodInfo.ReturnType };
            }

            return method;
        }
Пример #2
0
 public Module(TsType parent, string name)
     : base(parent, name)
 {
     Name = name;
     Types = new List<TsType>();
     Modules = new List<Module>();
 }
Пример #3
0
        public static TsType CreateClass(TsType parent, Type type)
        {
            var name = type.Name;

            var newClass = new Class(parent, name);

            if (type.IsGenericType)
            {
                var genericWartIndex = name.IndexOf("`");
                if (genericWartIndex > 0)
                {
                    newClass.Name = name.Substring(0, genericWartIndex);
                }

                var generics = type.GetGenericArguments();

                foreach (var generic in generics)
                {
                    newClass.Generics.Add(new GenericDefinition(generic.Name));
                }
            }

            AddConstructors(type, newClass);

            AddFields(type, newClass);

            AddProperties(type, newClass);

            AddMethods(type, newClass);

            return newClass;
        }
Пример #4
0
 public Method(string name, TsType parent)
 {
     Name = name;
     Parent = parent;
     Generics = new List<TsType>();
     Parameters = new List<MethodParameter>();
 }
Пример #5
0
        public static Constructor Create(ConstructorInfo constructorInfo, TsType parent)
        {
            var constructor = new Constructor(parent);

            foreach (var parameterInfo in constructorInfo.GetParameters())
            {
                constructor.Parameters.Add(MethodParameterFactory.Create(parameterInfo, constructor));
            }

            return constructor;
        }
Пример #6
0
        public static TsType Create(TsType parent, Type type)
        {
            TsType result = null;

            if (type.IsClass)
            {
                result = ClassFactory.CreateClass(parent, type);
            }
            else if (type.IsEnum)
            {
                result = EnumFactory.CreateEnum(parent, type);
            }
            else if (type == typeof(string))
            {
                result = TsString.Instance;
            }
            else if (IsNumberType(type))
            {
                result = TsNumber.Instance;
            }
            else if (type == typeof (char))
            {
                result = TsString.Instance;
            }
            else if (type == typeof (bool))
            {
                result = TsBoolean.Instance;
            }
            else if (type.IsArray)
            {
                result = new TsArray(type.GetElementType());
            }
            else if (type == typeof(object))
            {
                result = TsAny.Instance;
            }
            else if (type.IsValueType)
            {
                result = ClassFactory.CreateClass(parent, type);
            }
            else if (type.IsInterface)
            {
                result = ClassFactory.CreateClass(parent, type);
            }

            if (result != null)
            {
                Resolver.Types[type] = result;
                return result;
            }

            throw new UnsupportedTypeException(type);
        }
Пример #7
0
        public bool Matches(TsType type)
        {
            if (type == this)
                return true;

            if (type == null)
                return false;

            if (type.UnderlyingType == UnderlyingType && type.UnderlyingType != null)
                return true;

            return false;
        }
Пример #8
0
        public static TsType CreateEnum(TsType parent, Type type)
        {
            var typeName = type.Name;
            var newEnum = new TypeScript.Enum(parent, typeName);
            var names = System.Enum.GetNames(type);
            var typeCode = Type.GetTypeCode(System.Enum.GetUnderlyingType(type));

            foreach (var enumName in names)
            {
                var val = System.Enum.Parse(type, enumName);
                var i = Convert.ChangeType(val, typeCode);
                newEnum.Values.Add(new EnumValue(enumName, Convert.ToInt64(i)));
            }

            return newEnum;
        }
Пример #9
0
        public void ResolveTypes(TypeResolver typeResolver)
        {
            foreach (var generic in Generics)
            {
                generic.ResolveTypes(typeResolver);
            }
            foreach (var methodParameter in Parameters)
            {
                methodParameter.ResolveTypes(typeResolver);
            }

            if (Return != null)
            {
                Return = typeResolver.GetType(Return.UnderlyingType, Parent);
            }
        }
Пример #10
0
        public TsType GetType(Type type, TsType parent = null)
        {
            if (TsTypeFactory.IsNumberType(type))
                return TsNumber.Instance;
            if (type == typeof (string))
                return TsString.Instance;
            if (type == typeof(bool))
                return TsBoolean.Instance;

            if (type.IsArray)
            {
                var tsArray = new TsArray(type.GetElementType());
                tsArray.ResolveTypes(this);
                return tsArray;
            }

            TsType tsType;
            if (Types.TryGetValue(type, out tsType))
            {
                return tsType;
            }

            if (!type.IsGenericParameter)
            {
                return TsAny.Instance;
            }

            var parentClass = parent as Class;

            if (parentClass == null)
            {
                return TsAny.Instance;
            }

            // if the parent is generic we might have match.
            var generic = parentClass.Generics.SingleOrDefault(x => x.Name == type.Name);
            if (generic != null)
            {
                return new GenericDefinition(type.Name);
            }

            return TsAny.Instance;
        }
Пример #11
0
 public virtual bool IsBetterThan(TsType otherType)
 {
     return true;
 }
Пример #12
0
 protected TsType(TsType parent, string name)
 {
     Parent = parent;
     Name = name;
 }
Пример #13
0
 public Field(string name, TsType parent)
 {
     Name = name;
     Parent = parent;
 }
Пример #14
0
 public void ResolveTypes(TypeResolver typeResolver)
 {
     Type = typeResolver.GetType(Type.UnderlyingType, Method.Parent);
 }
Пример #15
0
 public Constructor(TsType parent)
     : base("", parent)
 {
 }
Пример #16
0
 public Enum(TsType parent, string name)
     : base(parent, name)
 {
     Values = new List<EnumValue>();
 }