Ejemplo n.º 1
0
        protected virtual string[] GetEnumDefinitions(TypeScriptModel model)
        {
            if (model.Type.IsEnum == false)
            {
                throw new InvalidCastException($"The type {model.Type.Name} ({model.Name}) is not an Enum.");
            }

            Array values = Enum.GetValues(model.Type);

            List <string> converted = new List <string>();

            for (int i = 0; i < values.Length; i++)
            {
                string value = values.GetValue(i).ToString();

                if (model.Ignore != null && model.Ignore.Any())
                {
                    if (model.Ignore.Contains(value) == false)
                    {
                        converted.Add(value);
                    }
                }
                else
                {
                    converted.Add(value);
                }
            }

            return(converted.ToArray());
        }
Ejemplo n.º 2
0
        protected virtual AngularFormGroup GetGroup(TypeScriptModel model)
        {
            AngularFormGroup group = new AngularFormGroup(model.Name);

            TypeScriptPropertyDefinition[] definitions = this.GetPropertyDefinitions(model);

            foreach (TypeScriptPropertyDefinition definition in definitions)
            {
                group.Controls.Add(GetControl(definition));
            }

            return(group);
        }
Ejemplo n.º 3
0
        protected virtual string GetDeclarationName(TypeScriptModel model)
        {
            //string[] arguments = GetGenericArguments(type);

            /*if (arguments.Any())
             * {
             *      // Remove `1 `2 etc when generic
             *      name = name.Substring(0, name.IndexOf("`"));
             *
             *      name += "<" + string.Join(", ", arguments)  + ">";
             * }*/

            return($"{model.Type.Namespace}.{model.Type.Name}");
        }
Ejemplo n.º 4
0
        protected virtual bool IsIgnoredProperty(TypeScriptModel model, PropertyInfo property)
        {
            bool isIgnored = false;

            //HINT: This is a total hack. The problem is when we have different versions of Newtonsoft.Json installed
            // (where the T4 gets builded and here in this solutions) the JsonIgnoreAttribute is always null!
            //TODO: Find a way or the right solution for that or the reason?!
            IEnumerable <Attribute> inheritedAttributes = property.GetCustomAttributes <Attribute>(true);
            //IEnumerable<Attribute> ownAttributes = property.GetCustomAttributes<Attribute>(false);

            // https://stackoverflow.com/questions/28674167/how-can-i-un-jsonignore-an-attribute-in-a-derived-class
            // It seems that this is the way how JsonProperty & JsonIgnore works.
            // if you have one and inherit from it it gets ignored when posting...
            // a fix is using the 'new' keyword e.g. new string Property { get; set; } and add the JsonProperty attribute over it

            /*Attribute attribute = ownAttributes.SingleOrDefault(
             *      o => o.GetType().FullName.Equals(typeof(JsonIgnoreAttribute).FullName, StringComparison.OrdinalIgnoreCase));
             *
             * if (attribute == null && ownAttributes.Any(
             *      o => o.GetType().FullName.Equals(typeof(JsonPropertyAttribute).FullName, StringComparison.OrdinalIgnoreCase)) == false)
             * {
             *      attribute = inheritedAttributes.SingleOrDefault(
             *         o => o.GetType().FullName.Equals(typeof(JsonIgnoreAttribute).FullName, StringComparison.OrdinalIgnoreCase));
             * }*/

            Attribute attribute = inheritedAttributes.SingleOrDefault(
                o => o.GetType().FullName.Equals(typeof(JsonIgnoreAttribute).FullName, StringComparison.OrdinalIgnoreCase));

            //JsonIgnoreAttribute attribute = property.GetCustomAttribute<JsonIgnoreAttribute>(true);
            if (attribute != null)
            {
                isIgnored = true;
            }

            if (inheritedAttributes.Any(o => o is TypeScriptPropertyAttribute))
            {
                isIgnored = false;
            }

            // If we have an array of ignored we check that.
            if (isIgnored == false && model.Ignore != null && model.Ignore.Any())
            {
                isIgnored = model.Ignore.Contains(property.Name);
            }

            return(isIgnored);
        }
Ejemplo n.º 5
0
        protected override bool IsIgnoredProperty(TypeScriptModel model, PropertyInfo property)
        {
            bool isIgnored = base.IsIgnoredProperty(model, property);

            if (isIgnored == false)
            {
                IEnumerable <Attribute> attributes = property.GetCustomAttributes <Attribute>(true);

                if (attributes.Any(o => o is AngularFormIgnoreAttribute))
                {
                    isIgnored = true;
                }
            }

            if (isIgnored == false)
            {
                isIgnored = property.CanWrite == false;
            }

            return(isIgnored);
        }
Ejemplo n.º 6
0
        protected virtual TypeScriptPropertyDefinition[] GetPropertyDefinitions(TypeScriptModel model)
        {
            if (model.Type.IsEnum)
            {
                throw new InvalidCastException($"The type {model.Type.Name} ({model.Name}) is an Enum and you tried to handle it as a class.");
            }

            List <TypeScriptPropertyDefinition> definitions = new List <TypeScriptPropertyDefinition>();

            PropertyInfo[] properties = model.Type.GetProperties();

            foreach (PropertyInfo property in properties)
            {
                if (IsIgnoredProperty(model, property))
                {
                    continue;
                }

                definitions.Add(GetPropertyDefintion(property));
            }

            return(definitions.ToArray());
        }