public static bool CanAddApiTypeConfiguration(Type clrType)
        {
            Contract.Requires(clrType != null);

            var isAbstract = TypeReflection.IsAbstract(clrType);

            if (isAbstract)
            {
                return(false);
            }

            var hasPublicDefaultConstructor = TypeReflection.GetDefaultConstructor(clrType, ReflectionFlags.Public) != null;

            if (!hasPublicDefaultConstructor)
            {
                return(false);
            }

            var isSubclassOfApiTypeConfiguration = TypeReflection.IsSubclassOf(clrType, typeof(ApiTypeConfiguration));

            // ReSharper disable once ConvertIfStatementToReturnStatement
            if (!isSubclassOfApiTypeConfiguration)
            {
                return(false);
            }

            return(true);
        }
        public void Test001()
        {
            var type = new TypeReflection();
            var result = type.From(() => "x" == "y");

            Assert.AreEqual(typeof(bool), result);
        }
        public void Test002()
        {
            var type = new TypeReflection();
            var result = type.From(() => 1 + 3.4M);

            Assert.AreEqual(typeof(decimal), result);
        }
Ejemplo n.º 4
0
            protected override void Assert()
            {
                this.ActualResult.Should().Be(this.ExpectedResult);

                switch (this.ActualResult)
                {
                case ConvertResult.Success:
                {
                    var isExpectedValueEqualToDefaultTarget = ReferenceEquals(this.ExpectedValue, default(TTarget));
                    if (isExpectedValueEqualToDefaultTarget)
                    {
                        // Source is default(TSource)
                        this.ActualValue.Should().Be(default(TTarget));
                    }
                    else
                    {
                        // Special case if target is byte array.
                        if (typeof(TTarget) == typeof(byte[]))
                        {
                            this.ActualValue.Should().BeAssignableTo <TTarget>();

                            var expectedValue = this.ExpectedValue as byte[];
                            var actualValue   = this.ActualValue as byte[];
                            actualValue.Should().ContainInOrder(expectedValue);
                            return;
                        }

                        // Special case if target is nullable
                        if (TypeReflection.IsNullableType(typeof(TTarget)))
                        {
                            this.ActualValue.Should().BeAssignableTo <TTarget>();
                            this.ActualValue.Should().Be(this.ExpectedValue);
                            return;
                        }

                        // Special case if target is type.
                        if (typeof(TTarget) == typeof(Type))
                        {
                            this.ActualValue.Should().BeAssignableTo <Type>();
                            this.ActualValue.Should().Be(this.ExpectedValue);
                            return;
                        }

                        this.ActualValue.Should().BeAssignableTo <TTarget>();
                        this.ActualValue.Should().Be(this.ExpectedValue);
                    }
                    break;
                }

                case ConvertResult.Failure:
                {
                    this.ActualValue.Should().Be(default(TTarget));
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
Ejemplo n.º 5
0
        /*
         *  TODO(@Tyler):
         *      - Can we generate the ajax functions to invoke the correct api function/urlpath as needed automatically?
         */

        // Begin the process of generating typescript.
        // 1. We must search for the types we need to processes.
        // 2. Generate the appopriate typescript files for each type we are processing.
        void Run(string[] args)
        {
            if (args.Length > 0)
            {
                if (args[0] == "-g")
                {
                    string      path           = args[1];
                    ProjectFile exampleProject = new ProjectFile();

                    exampleProject.InputAssemblies = new string[] { "test.dll" };
                    exampleProject.OutputDirectory = "./ts";
                    exampleProject.Rules           = new TypeRule[] {
                        new TypeRule()
                        {
                            EnableClassGeneration     = true,
                            EnableEnumGeneration      = true,
                            EnableInterfaceGeneration = true,
                            Pattern = "InsertRegexHere"
                        }
                    };

                    ConfigFile.Save(path, exampleProject);
                    Logger.Info($"generated default project file: {path}");
                    return;
                }
            }

            if (args.Length == 0)
            {
                Logger.Error("no project file specified");
                Logger.Error("sharpts <path/to/project.json>");
                return;
            }

            ProjectPath = args[0];

            if (ConfigFile.Load <ProjectFile>(ProjectPath, out Project) == false)
            {
                Logger.Error($"failed to open project file: {ProjectPath}");
                return;
            }

            List <Type> types = TypeReflection.Load(Project);

            SharpTypeConverter typeConverter
                = new SharpTypeConverter(types);

            if (typeConverter.Convert() == false)
            {
                Console.WriteLine("failed during conversion of c# types into typescript types");
                return;
            }

            SharpSourceGenerator sourceGenerator
                = new SharpSourceGenerator(Project.OutputDirectory, typeConverter.MappedTypes);

            sourceGenerator.Generate();
        } // void Run
Ejemplo n.º 6
0
        // PUBLIC METHODS ///////////////////////////////////////////////////
        #region Methods
        /// <summary>Standardizes the discovery of CLR enumeration values on a CLR enum type.</summary>
        public static IReadOnlyCollection <FieldInfo> GetClrEnumerationValues(Type clrEnumerationType)
        {
            Contract.Requires(clrEnumerationType != null);

            var clrEnumValues = TypeReflection.GetFields(clrEnumerationType)
                                .Where(x => x.FieldType.IsEnum)
                                .ToList();

            return(clrEnumValues);
        }
Ejemplo n.º 7
0
        // PUBLIC METHODS //////////////////////////////////////////////////
        #region Methods
        public static Type GetActualType(Type clrType)
        {
            if (!TypeReflection.IsNullableType(clrType))
            {
                return(clrType);
            }

            var clrNullableUnderlyingType = Nullable.GetUnderlyingType(clrType);

            return(clrNullableUnderlyingType);
        }
Ejemplo n.º 8
0
        public static Expression <Func <TArgument, TObject> > New <TArgument, TObject>(ReflectionFlags reflectionFlags, Type objectType = null)
        {
            objectType = objectType ?? typeof(TObject);
            var argumentType       = typeof(TArgument);
            var argumentExpression = Expression.Parameter(argumentType, "a");
            var constructorInfo    = TypeReflection.GetConstructor(objectType, reflectionFlags, argumentType);
            var newExpression      = Expression.New(constructorInfo, argumentExpression);
            var lambdaExpression   = Expression.Lambda <Func <TArgument, TObject> >(newExpression, argumentExpression);

            return(lambdaExpression);
        }
Ejemplo n.º 9
0
            // PRIVATE METHODS //////////////////////////////////////////////
            #region Methods
            private static bool GetNullableHasValue <T>(T nullable)
            {
                var nullableType       = typeof(T);
                var instanceExpression = Expression.Parameter(nullableType, "i");
                var propertyInfo       = TypeReflection.GetProperty(nullableType, StaticReflection.GetMemberName <int?>(x => x.HasValue), ReflectionFlags.Public | ReflectionFlags.Instance);
                var propertyExpression = Expression.Property(instanceExpression, propertyInfo);
                var lambdaExpression   = (Expression <Func <T, bool> >)Expression.Lambda(propertyExpression, instanceExpression);
                var labmda             = lambdaExpression.Compile();
                var hasValue           = labmda(nullable);

                return(hasValue);
            }
Ejemplo n.º 10
0
        private static MethodCallExpression CreateStaticMethodCallExpression <TClass>(string methodName)
        {
            Contract.Requires(methodName.SafeHasContent());

            var classType = typeof(TClass);

            var methodInfo = TypeReflection.GetMethod(classType, methodName);

            var callExpression = Expression.Call(methodInfo);

            return(callExpression);
        }
Ejemplo n.º 11
0
        // PRIVATE METHODS //////////////////////////////////////////////////
        #region Call Methods
        private static MethodCallExpression CreateMethodCallExpression <TObject>(string methodName,
                                                                                 out ParameterExpression instanceExpression)
        {
            Contract.Requires(methodName.SafeHasContent());

            var objectType = typeof(TObject);

            var methodInfo = TypeReflection.GetMethod(objectType, methodName);

            instanceExpression = Expression.Parameter(objectType, "a");
            var callExpression = Expression.Call(instanceExpression, methodInfo);

            return(callExpression);
        }
Ejemplo n.º 12
0
        private static MethodCallExpression CreateStaticMethodCallExpression <TClass, TArgument>(string methodName,
                                                                                                 out ParameterExpression argumentExpression)
        {
            Contract.Requires(methodName.SafeHasContent());

            var classType    = typeof(TClass);
            var argumentType = typeof(TArgument);

            var methodInfo = TypeReflection.GetMethod(classType, methodName, argumentType);

            argumentExpression = Expression.Parameter(argumentType, "a");
            var callExpression = Expression.Call(methodInfo, argumentExpression);

            return(callExpression);
        }
        public void Test003()
        {
            var type = new TypeReflection();
            var result = type.From(() => new
            {
                A = 1,
                B = 2
            });

            Assert.AreEqual((new
            {
                A = 1,
                B = 2
            }).GetType(), result);
        }
        // PUBLIC METHODS ///////////////////////////////////////////////////
        #region Methods
        /// <summary>Standardizes the discovery of CLR properties on a CLR object type.</summary>
        public static IReadOnlyCollection <PropertyInfo> GetClrProperties(Type clrObjectType)
        {
            Contract.Requires(clrObjectType != null);

            // Use reflection, get all the directly declared, public, and instance-based type of properties that have a public getter and setter for the given CLR object type.
            var clrProperties = TypeReflection.GetProperties(clrObjectType, ReflectionFlags.DeclaredOnly | ReflectionFlags.Public | ReflectionFlags.Instance)
                                .Where(x =>
            {
                var hasPublicGetter = x.GetGetMethod(false) != null;
                var hasPublicSetter = x.GetSetMethod(false) != null;
                return(hasPublicGetter && hasPublicSetter);
            })
                                .ToList();

            return(clrProperties);
        }
Ejemplo n.º 15
0
            private static string TypeAsString <T>()
            {
                var type = typeof(T);

                if (!TypeReflection.IsNullableType(type))
                {
                    var typeName = type.Name;
                    return(typeName);
                }

                var underlyingType     = Nullable.GetUnderlyingType(type);
                var underlyingTypeName = underlyingType.Name;
                var nullableTypeName   = String.Format("Nullable<{0}>", underlyingTypeName);

                return(nullableTypeName);
            }
Ejemplo n.º 16
0
        public static Expression <Func <TArgument1, TArgument2, TArgument3, TArgument4, TObject> > New <TArgument1, TArgument2, TArgument3, TArgument4, TObject>(ReflectionFlags reflectionFlags, Type objectType = null)
        {
            objectType = objectType ?? typeof(TObject);
            var argument1Type       = typeof(TArgument1);
            var argument2Type       = typeof(TArgument2);
            var argument3Type       = typeof(TArgument3);
            var argument4Type       = typeof(TArgument4);
            var argument1Expression = Expression.Parameter(argument1Type, "a");
            var argument2Expression = Expression.Parameter(argument2Type, "b");
            var argument3Expression = Expression.Parameter(argument3Type, "c");
            var argument4Expression = Expression.Parameter(argument4Type, "d");
            var constructorInfo     = TypeReflection.GetConstructor(objectType, reflectionFlags, argument1Type, argument2Type, argument3Type, argument4Type);
            var newExpression       = Expression.New(constructorInfo, argument1Expression, argument2Expression, argument3Expression, argument4Expression);
            var lambdaExpression    = Expression.Lambda <Func <TArgument1, TArgument2, TArgument3, TArgument4, TObject> >(newExpression, argument1Expression, argument2Expression, argument3Expression, argument4Expression);

            return(lambdaExpression);
        }
Ejemplo n.º 17
0
            private static string ValueAsString <TValue>(TValue value)
            {
                var valueAsString = value.SafeToString();
                var valueType     = typeof(TValue);

                if (!TypeReflection.IsNullableType(valueType) && TypeReflection.IsValueType(valueType))
                {
                    return(valueAsString);
                }

                var valueAsObject = (object)value;

                if (valueAsObject == null)
                {
                    valueAsString = "null";
                }
                return(valueAsString);
            }
Ejemplo n.º 18
0
        private static MethodCallExpression CreateMethodCallExpression <TObject, TArgument1, TArgument2>(string methodName,
                                                                                                         out ParameterExpression instanceExpression,
                                                                                                         out ParameterExpression argument1Expression,
                                                                                                         out ParameterExpression argument2Expression)
        {
            Contract.Requires(methodName.SafeHasContent());

            var objectType    = typeof(TObject);
            var argument1Type = typeof(TArgument1);
            var argument2Type = typeof(TArgument2);

            var methodInfo = TypeReflection.GetMethod(objectType, methodName, argument1Type, argument2Type);

            instanceExpression  = Expression.Parameter(objectType, "a");
            argument1Expression = Expression.Parameter(argument1Type, "b");
            argument2Expression = Expression.Parameter(argument2Type, "c");
            var callExpression = Expression.Call(instanceExpression, methodInfo, argument1Expression, argument2Expression);

            return(callExpression);
        }
Ejemplo n.º 19
0
        // Static - Property Getter
        public static Expression <Func <TProperty> > StaticPropertyGetter <TClass, TProperty>(string propertyName)
        {
            Contract.Requires(propertyName.SafeHasContent());

            var type = typeof(TClass);
            var propertyExpression = default(Expression);
            var propertyNameSplit  = propertyName.Split('.');

            foreach (var name in propertyNameSplit)
            {
                var propertyInfo     = TypeReflection.GetProperty(type, name, ReflectionFlags.Public | ReflectionFlags.Static | ReflectionFlags.Instance);
                var isPropertyStatic = PropertyReflection.IsStatic(propertyInfo);
                propertyExpression = Expression.Property(isPropertyStatic ? null : propertyExpression, propertyInfo);
                type = propertyInfo.PropertyType;
            }

            var lambdaExpression = Expression.Lambda <Func <TProperty> >(propertyExpression);

            return(lambdaExpression);
        }
        // PROTECTED CONSTRUCTORS ///////////////////////////////////////////
        #region Constructors
        public ApiEnumerationType(string apiName,
                                  string apiDescription,
                                  IEnumerable <IApiEnumerationValue> apiEnumerationValues,
                                  Type clrEnumerationType)
            : base(apiName, apiDescription, clrEnumerationType)
        {
            Contract.Requires(clrEnumerationType != null);

            if (TypeReflection.IsEnum(clrEnumerationType) == false)
            {
                var message = $"Unable to create an API enumeration type, the CLR type [name={clrEnumerationType.Name}] is not a CLR enumeration type.";
                throw new ApiSchemaException(message);
            }

            this.ApiEnumerationValues = apiEnumerationValues.SafeToReadOnlyCollection();

            this.ApiEnumerationValueByApiNameDictionary  = this.ApiEnumerationValues.ToDictionary(x => x.ApiName);
            this.ApiEnumerationValueByClrNameDictionary  = this.ApiEnumerationValues.ToDictionary(x => x.ClrName);
            this.ApiEnumerationValueByClrValueDictionary = this.ApiEnumerationValues.ToDictionary(x => x.ClrOrdinal);
        }
Ejemplo n.º 21
0
        // Instance - Property Getter
        public static Expression <Func <TObject, TProperty> > PropertyGetter <TObject, TProperty>(string propertyName)
        {
            Contract.Requires(propertyName.SafeHasContent());

            var type = typeof(TObject);
            var instanceExpression = Expression.Parameter(type, "x");
            var propertyExpression = (Expression)instanceExpression;
            var propertyNameSplit  = propertyName.Split('.');

            foreach (var name in propertyNameSplit)
            {
                var propertyInfo     = TypeReflection.GetProperty(type, name, ReflectionFlags.Public | ReflectionFlags.Instance | ReflectionFlags.Static);
                var isPropertyStatic = PropertyReflection.IsStatic(propertyInfo);
                propertyExpression = Expression.Property(!isPropertyStatic ? propertyExpression : null, propertyInfo);
                type = propertyInfo.PropertyType;
            }

            var lambdaExpression = Expression.Lambda <Func <TObject, TProperty> >(propertyExpression, instanceExpression);

            return(lambdaExpression);
        }
        public void Test010()
        {
            var type = new TypeReflection();

            Assert.IsTrue(type.IsNullAssignable(typeof(string)));
        }
        public void Test007()
        {
            var type = new TypeReflection();

            Assert.IsFalse(type.IsNullableType(typeof(int)));
        }
        public void Test090()
        {
            var type = new TypeReflection();
            var result = type.NameOf(() => default(string));

            Assert.AreEqual("System.String", result);
        }
        public void Test088()
        {
            var type = new TypeReflection();
            var result = type.From(typeof(string));

            Assert.AreEqual(typeof(string), result);
        }
        public void Test086()
        {
            var type = new TypeReflection();
            var result = type.From(typeof(Dummy02).GetEvent("MyEvent"));

            Assert.AreEqual(typeof(EventHandler), result);
        }
        public void Test084()
        {
            var type = new TypeReflection();
            var result = type.From(typeof(string).GetMethod("GetHashCode"));

            Assert.AreEqual(typeof(int), result);
        }
        public void Test081()
        {
            var type = new TypeReflection();
            var result = type.From(typeof(string[]).GetMember("Length")[0]);

            Assert.AreEqual(typeof(int), result);
        }
 public void Test045()
 {
     var type = new TypeReflection();
     Assert.IsTrue(type.IsDecimal(typeof(decimal?)));
 }
 public void Test039()
 {
     var type = new TypeReflection();
     Assert.IsTrue(type.IsInteger(typeof(ulong?)));
 }
Ejemplo n.º 31
0
        public static bool IsClrCollectionType(this Type clrType, out Type clrItemType)
        {
            Contract.Requires(clrType != null);

            return(TypeReflection.IsEnumerableOfT(clrType, out clrItemType));
        }
 public void Test037()
 {
     var type = new TypeReflection();
     Assert.IsTrue(type.IsInteger(typeof(sbyte?)));
 }
Ejemplo n.º 33
0
        public static bool IsClrNullableType(this Type clrType)
        {
            Contract.Requires(clrType != null);

            return(TypeReflection.IsNullableType(clrType));
        }
 public void Test028()
 {
     var type = new TypeReflection();
     Assert.IsTrue(type.IsInteger(typeof(uint)));
 }
Ejemplo n.º 35
0
        public static bool IsClrObjectType(this Type clrType)
        {
            Contract.Requires(clrType != null);

            return(TypeReflection.IsComplex(clrType));
        }
 public void Test046()
 {
     var type = new TypeReflection();
     Assert.IsFalse(type.IsDecimal(typeof(string)));
 }
 public void Test076()
 {
     var type = new TypeReflection();
     Assert.IsFalse(type.IsInteger(typeof(string)));
 }
 public void Test058()
 {
     var type = new TypeReflection();
     Assert.IsFalse(type.IsDecimal(typeof(uint)));
 }
        public void Test082()
        {
            var type = new TypeReflection();
            var result = type.From(typeof(List<string>).GetMember("Count")[0]);

            Assert.AreEqual(typeof(int), result);
        }
 public void Test067()
 {
     var type = new TypeReflection();
     Assert.IsFalse(type.IsDecimal(typeof(sbyte?)));
 }
        public void Test085()
        {
            var type = new TypeReflection();
            var result = type.From(typeof(Dummy01).GetField("StringData"));

            Assert.AreEqual(typeof(string), result);
        }
 public void Test069()
 {
     var type = new TypeReflection();
     Assert.IsFalse(type.IsDecimal(typeof(ulong?)));
 }
        public void Test087()
        {
            var type = new TypeReflection();
            var result = type.From(typeof(Dummy03.Inner));

            Assert.AreEqual(typeof(Dummy03.Inner), result);
        }
        public void Test004()
        {
            var type = new TypeReflection();

            Assert.IsTrue(type.IsNullableType(typeof(long?)));
        }
        public void Test089()
        {
            var type = new TypeReflection();
            var result = type.From(typeof(string).GetConstructors()[0]);

            Assert.AreEqual(typeof(string), result);
        }
 public void Test073()
 {
     var type = new TypeReflection();
     Assert.IsFalse(type.IsInteger(typeof(float?)));
 }
        public void Test091()
        {
            var type = new TypeReflection();
            var result = type.NameOf(() => default(int?));

            Assert.AreEqual("System.Nullable`1[System.Int32]", result);
        }
 public void Test074()
 {
     var type = new TypeReflection();
     Assert.IsFalse(type.IsInteger(typeof(double?)));
 }
        public void Test009()
        {
            var type = new TypeReflection();

            Assert.IsTrue(type.IsNullAssignable(typeof(int?)));
        }
 public void Test075()
 {
     var type = new TypeReflection();
     Assert.IsFalse(type.IsInteger(typeof(decimal?)));
 }
 public void Test040()
 {
     var type = new TypeReflection();
     Assert.IsTrue(type.IsDecimal(typeof(float)));
 }
Ejemplo n.º 52
0
        public static bool IsClrScalarType(this Type clrType)
        {
            Contract.Requires(clrType != null);

            return(TypeReflection.IsSimple(clrType) && !TypeReflection.IsEnum(clrType));
        }