TsProperty

Namespace: TypeLite
We found 10 examples in language CSharp for this search. You will see 21 fragments of code.

        /// <summary>
        /// writes a typescript property with the accesability modifier.
        /// </summary>
        /// <param name="tsProperty">the property to write.</param>
        public void WriteProperty(TypescriptProperty tsProperty)
        {
            WriteProperty(tsProperty, true);
        }

using System.Net;
using Reinforced.Typings.Attributes;

namespace DnsTools.Web.Models
{
	/// <summary>
	/// Data about an IP address.
	/// </summary>
	[TsInterface(AutoI = false)]
	public class IpData
	{
		[TsProperty(ForceNullable = true)]
		public long? Asn { get; set; }

		[TsProperty(ForceNullable = true)]
		public string? AsnName { get; set; }

		[TsProperty(ForceNullable = true)]
		public string? City { get; set; }

		[TsProperty(ForceNullable = true)]
		public string? Country { get; set; }

		[TsProperty(ForceNullable = true)]
		public string? CountryIso { get; set; }

		[TsProperty(ForceNullable = true)]
		public string? HostName { get; set; }
	}
}

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using Calysto.TypeLite.Extensions;

namespace Calysto.TypeLite.TsModels {
    /// <summary>
    /// Represents a property of the class in the code model.
    /// </summary>
    [DebuggerDisplay("Name: {Name}")]
    public class TsProperty {
         public string Name { get; set; }

        /// <summary>
        /// Gets or sets type of the property.
        /// </summary>
        public TsType PropertyType { get; set; }

        /// <summary>
        /// Gets the CLR property represented by this TsProperty.
        /// </summary>
        public MemberInfo MemberInfo { get; set; }

        /// <summary>
        /// Gets or sets bool value indicating whether this property will be ignored by TsGenerator.
        /// </summary>
        public bool IsIgnored { get; set; }

        /// <summary>
        /// Gets or sets bool value indicating whether this property is optional in TypeScript interface.
        /// </summary>
        public bool IsOptional { get; set; }

        /// <summary>
        /// Gets the GenericArguments for this property.
        /// </summary>
        public IList<TsType> GenericArguments { get; private set; }

        /// <summary>
        /// Gets or sets the constant value of this property.
        /// </summary>
        public object ConstantValue { get; set; }

		/// <summary>
		/// Initializes a new instance of the TsProperty class with the specific CLR property.
		/// </summary>
		/// <param name="memberInfo">The CLR property represented by this instance of the TsProperty.</param>
		public TsProperty(PropertyInfo memberInfo)
		{
			this.MemberInfo = memberInfo;
			this.Name = memberInfo.Name;

			var propertyType = memberInfo.PropertyType;
			if (propertyType.IsNullable())
			{
				propertyType = propertyType.GetNullableValueType();
			}

			this.GenericArguments = propertyType.IsGenericType ? propertyType.GetGenericArguments().Select(o => new TsType(o)).ToArray() : new TsType[0];

			this.PropertyType = propertyType.IsEnum ? new TsEnum(propertyType) : new TsType(propertyType);

			var attribute = memberInfo.GetCustomAttribute<TsPropertyAttribute>(false);
			if (attribute != null)
			{
				if (!string.IsNullOrEmpty(attribute.Name))
				{
					this.Name = attribute.Name;
				}

				this.IsOptional = attribute.IsOptional;
			}

			this.IsIgnored = (memberInfo.GetCustomAttribute<TsIgnoreAttribute>(false) != null);

			// Only fields can be constants.
			this.ConstantValue = null;
		}

		/// <summary>
		/// Initializes a new instance of the TsProperty class with the specific CLR field.
		/// </summary>
		/// <param name="memberInfo">The CLR field represented by this instance of the TsProperty.</param>
		public TsProperty(FieldInfo memberInfo)
		{
			this.MemberInfo = memberInfo;
			this.Name = memberInfo.Name;

			if (memberInfo.ReflectedType.IsGenericType)
			{
				var definitionType = memberInfo.ReflectedType.GetGenericTypeDefinition();
				var definitionTypeProperty = definitionType.GetProperty(memberInfo.Name);
				if (definitionTypeProperty.PropertyType.IsGenericParameter)
				{
					this.PropertyType = TsType.Any;
				}
				else
				{
					this.PropertyType = memberInfo.FieldType.IsEnum ? new TsEnum(memberInfo.FieldType) : new TsType(memberInfo.FieldType);
				}
			}
			else
			{
				var propertyType = memberInfo.FieldType;
				if (propertyType.IsNullable())
				{
					propertyType = propertyType.GetNullableValueType();
				}

				this.PropertyType = propertyType.IsEnum ? new TsEnum(propertyType) : new TsType(propertyType);
			}

			var attribute = memberInfo.GetCustomAttribute<TsPropertyAttribute>(false);
			if (attribute != null)
			{
				if (!string.IsNullOrEmpty(attribute.Name))
				{
					this.Name = attribute.Name;
				}

				this.IsOptional = attribute.IsOptional;
			}

			this.IsIgnored = (memberInfo.GetCustomAttribute<TsIgnoreAttribute>(false) != null);

			if (memberInfo.IsLiteral && !memberInfo.IsInitOnly)
			{
				// it's a constant
				this.ConstantValue = memberInfo.GetValue(null);
			}
			else
			{
				// not a constant
				this.ConstantValue = null;
			}
		}

	}
}

		[Fact]
		public void WhenInitialized_PropertyInfoIsSet() {
			var propertyInfo = typeof(Person).GetProperty("Name");

			var target = new TsProperty(propertyInfo);

			Assert.Same(propertyInfo, target.ClrProperty);
		}

		[Fact]
		public void WhenInitialized_IsIgnoredIsFalse() {
			var propertyInfo = typeof(Person).GetProperty("Name");

			var target = new TsProperty(propertyInfo);

			Assert.False(target.IsIgnored);
		}

        [Fact]
        public void WhenInitialized_IsOptionalIsFalse() {
            var propertyInfo = typeof(Person).GetProperty("Name");

            var target = new TsProperty(propertyInfo);

            Assert.False(target.IsOptional);
        }

		[Fact]
		public void WhenInitialized_NameIsSet() {
			var propertyInfo = typeof(Person).GetProperty("Name");

			var target = new TsProperty(propertyInfo);

			Assert.Equal("Name", target.Name);
		}

		[Fact]
		public void WhenInitialized_PropertyTypeIsSet() {
			var propertyInfo = typeof(Person).GetProperty("Name");

			var target = new TsProperty(propertyInfo);

			Assert.Equal(propertyInfo.PropertyType, target.PropertyType.ClrType);
		}

		[Fact]
		public void WhenInitializedAndHasCustomNameInAttribute_CustomNameIsUsed() {
			var propertyInfo = typeof(CustomClassName).GetProperty("CustomPorperty");

			var target = new TsProperty(propertyInfo);

			Assert.Equal("MyProperty", target.Name);
		}

        [Fact]
        public void TestSimpleProperty()
        {
            var expected =
@"public TestProperty: TestClass;
";
            var property = new TypescriptProperty
            {
                Name = "TestProperty",
                Type = new TypescriptType(TestData.simpleClass)
            };

            AssertThatWritingPropertyGivesTheExpectedResult(expected, property);
        }

        private static void AssertThatWritingPropertyGivesTheExpectedResult(string expectedresult, TypescriptProperty tsProperty)
        {
            var writer = new TypescriptWriter();
            writer.WriteProperty(tsProperty);
            var result = writer.ToString();

            result.ShouldBeEquivalentTo(expectedresult);
        }

    .WithMemberTypeFormatter((tsProperty, memberTypeName) => {
        var asCollection = tsProperty.PropertyType as TypeLite.TsModels.TsCollection;
        var isCollection = asCollection != null;
    
        if(tsProperty.GenericArguments.Count == 1 && 
           tsProperty.GenericArguments[0] is TypeLite.TsModels.TsClass && 
           ((TypeLite.TsModels.TsClass)tsProperty.GenericArguments[0]).Name =="T") {
               memberTypeName = "any";
        }
        
        return memberTypeName + (isCollection ? string.Concat(Enumerable.Repeat("[]", asCollection.Dimension)) : "");
    })

using Reinforced.Typings.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace incognote.web
{
    [TsClass]
    public static class Consts
    {
        [TsProperty(Constant = true)]
        public const string SignalRPath = "MessageHub";
        [TsProperty(Constant = true)]
        public const string NameParameterString = "name";
        [TsProperty(Constant = true)]
        public const string ConnectionIdString = nameof(ConnectionIdString);
        [TsProperty(Constant = true)]
        public const string MessageReceivedString = nameof(MessageReceivedString);
        [TsProperty(Constant = true)]
        public const string JoinString = nameof(JoinString);
        [TsProperty(Constant = true)]
        public const string SendMesssageString = nameof(SendMesssageString);
        [TsProperty(Constant = true)]
        public const string SetActionsString = nameof(SetActionsString);
        [TsProperty(Constant = true)]
        public const string SetActionString = nameof(SetActionString);
        [TsProperty(Constant = true)]
        public const string PerformActionString = nameof(PerformActionString);

        [TsProperty(Constant = true)]
        public const string StatePostString = nameof(StatePostString);
        public const string StatePutString = nameof(StatePutString);
    }
}

        /// <summary>
        /// Append class document.
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="classModel"></param>
        /// <param name="className"></param>
        void AppendClassDoc(ScriptBuilder sb, TsClass classModel, string className);
		 
        /// <summary>
        /// Append property document.
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="property"></param>
        /// <param name="propertyName"></param>
        /// <param name="propertyType"></param>
        void AppendPropertyDoc(ScriptBuilder sb, TsProperty property, string propertyName, string propertyType);

        /// <summary>
        /// Append constant document.
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="property"></param>
        /// <param name="propertyName"></param>
        /// <param name="propertyType"></param>
        void AppendConstantModuleDoc(ScriptBuilder sb, TsProperty property, string propertyName, string propertyType);

        /// <summary>
        /// Append Enum document.
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="enumModel"></param>
        /// <param name="enumName"></param>
        void AppendEnumDoc(ScriptBuilder sb, TsEnum enumModel, string enumName);

        /// <summary>
        /// Append Enum value document.
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="value"></param>
        void AppendEnumValueDoc(ScriptBuilder sb, TsEnumValue value);
        public void AppendClassDoc(ScriptBuilder sb, TsClass classModel, string className) {
        }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using TypeLite.Extensions;

namespace TypeLite.TsModels {
    /// <summary>
    /// Represents a property of the class in the code model.
    /// </summary>
    public class TsProperty : IMemberIdentifier {
        /// <summary>
        /// Gets or sets name of the property.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets type of the property.
        /// </summary>
        public TsType PropertyType { get; set; }

        /// <summary>
        /// Gets the CLR property represented by this TsProperty.
        /// </summary>
        public PropertyInfo ClrProperty { get; private set; }

        /// <summary>
        /// Gets or sets bool value indicating whether this property will be ignored by TsGenerator.
        /// </summary>
        public bool IsIgnored { get; set; }

        /// <summary>
        /// Gets or sets bool value indicating whether this property is optional in TypeScript interface.
        /// </summary>
        public bool IsOptional { get; set;}

        /// <summary>
        /// Initializes a new instance of the TsProperty class with the specific CLR property.
        /// </summary>
        /// <param name="clrProperty">The CLR property represented by this instance of the TsProperty.</param>
        public TsProperty(PropertyInfo clrProperty) {
            this.ClrProperty = clrProperty;
            this.Name = clrProperty.Name;

            if (clrProperty.ReflectedType.IsGenericType) {
                var definitionType = clrProperty.ReflectedType.GetGenericTypeDefinition();
                var definitionTypeProperty = definitionType.GetProperty(clrProperty.Name);
                if (definitionTypeProperty.PropertyType.IsGenericParameter) {
                    this.PropertyType = TsType.Any;
                } else {
                    this.PropertyType = new TsType(clrProperty.PropertyType);
                }
            } else {
                var propertyType = clrProperty.PropertyType;
                if (propertyType.IsNullable()) {
                    propertyType = propertyType.GetNullableValueType();
                }

                this.PropertyType = propertyType.IsEnum ? new TsEnum(propertyType) : new TsType(propertyType);
            }

            var attribute = clrProperty.GetCustomAttribute<TsPropertyAttribute>(false);
            if (attribute != null) {
                if (!string.IsNullOrEmpty(attribute.Name)) {
                    this.Name = attribute.Name;
                }

                this.IsOptional = attribute.IsOptional;
            }

            this.IsIgnored = (clrProperty.GetCustomAttribute<TsIgnoreAttribute>(false) != null);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Calysto.TypeLite.TsModels;

namespace Calysto.TypeLite {
	/// <summary>
	/// Defines a method used to format class member types.
	/// </summary>
	/// <param name="tsProperty">The typescript property</param>
	/// <returns>The formatted type.</returns>
	public delegate string TsMemberTypeFormatter(TsProperty tsProperty, string memberTypeName);

	/// <summary>
	/// Defines a method used to format class member types.
	/// </summary>
	/// <param name="memberTypeName">The type name to format</param>
	/// <param name="isMemberCollection">Indicates if member is collection</param>
	/// <param name="collectionDimension">The dimension of the collection - 1 for an array, 2 for an array of arrays etc</param>
	/// <returns>The formatted type.</returns>
	[Obsolete]
	public delegate string TsSimpleMemberTypeFormatter(string memberTypeName, bool isMemberCollection, int collectionDimension = 1);
}

TypeLite.TsModels.TsProperty : Object

Constructors :

public TsProperty(PropertyInfo memberInfo = )
public TsProperty(FieldInfo memberInfo = )

Methods :

public String get_Name()
public Void set_Name(String value = )
public TsType get_PropertyType()
public Void set_PropertyType(TsType value = )
public MemberInfo get_MemberInfo()
public Void set_MemberInfo(MemberInfo value = )
public Boolean get_IsIgnored()
public Void set_IsIgnored(Boolean value = )
public Boolean get_IsOptional()
public Void set_IsOptional(Boolean value = )
public IList<TsType> get_GenericArguments()
public Object get_ConstantValue()
public Void set_ConstantValue(Object value = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()

Other methods