예제 #1
0
        public string Generate(Type type)
        {
            var stringBuilder = new StringBuilder();

            var typeScriptType = _nameConvertor.GetName(type);

            stringBuilder.AppendLine($"export class {typeScriptType} {{");
            stringBuilder.AppendLine("  constructor(");

            object instance = null;

            // create new instance if type contains parameterless constractor
            if (type.GetConstructor(Type.EmptyTypes) != null)
            {
                instance = Activator.CreateInstance(type);
            }

            var properties = TypeExtensions.GetProperties(type).ToList().OrderBy(x => x.Name);

            foreach (var property in properties)
            {
                if (property.IsTypeScriptIgnored())
                {
                    continue;
                }

                var propertyType  = property.PropertyType;
                var propertyName  = _nameConvertor.GetName(propertyType);
                var propertyValue = instance == null ? null : property.GetValue(instance);

                var valueCode = _valueConvertor.GetValue(propertyType, propertyValue);

                var noValueCode = string.IsNullOrEmpty(valueCode);

                var nullableCode = _typeHelper.IsNullable(propertyType) && noValueCode ? "?" : "";
                stringBuilder.Append($"  public {property.Name.ToCamelCase()}{nullableCode}: ");

                if (!noValueCode)
                {
                    stringBuilder.AppendLine($"{ propertyName} = {valueCode},");
                }
                else
                {
                    stringBuilder.AppendLine($"{ propertyName},");
                }
            }

            stringBuilder.AppendLine("  ) { }");
            stringBuilder.AppendLine("}");

            return(stringBuilder.ToString());
        }
예제 #2
0
        public string Generate(Type type)
        {
            var stringBuilder = new StringBuilder();

            var name = _nameConvertor.GetName(type).ToCamelCase();

            stringBuilder.AppendLine($"export const {name} = {{");

            object instance = null;

            // create new instance if type contains parameterless constractor
            if (type.GetConstructor(Type.EmptyTypes) != null)
            {
                instance = Activator.CreateInstance(type);
            }

            var properties = TypeExtensions.GetProperties(type);

            foreach (var property in properties)
            {
                if (property.IsTypeScriptIgnored())
                {
                    continue;
                }

                var propertyName  = property.Name.ToCamelCase();
                var propertyType  = property.PropertyType;
                var propertyValue = instance == null ? null : property.GetValue(instance);

                var valueCode = _valueConvertor.GetValue(propertyType, propertyValue, 2);

                if (!string.IsNullOrEmpty(valueCode))
                {
                    stringBuilder.AppendLine($"  {propertyName}: {valueCode},");
                }
            }

            stringBuilder.AppendLine("}");

            return(stringBuilder.ToString());
        }