/// <summary> /// Gets the name of the friendly type. Example, Int32 => 'int' /// </summary> /// <param name="type">The type.</param> /// <returns></returns> protected string GetFriendlyTypeName(Type type) { var compiler = new Microsoft.CSharp.CSharpCodeProvider(); string name; if (Nullable.GetUnderlyingType(type) != null) { name = compiler.GetTypeOutput(new System.CodeDom.CodeTypeReference(Nullable.GetUnderlyingType(type))) + "?"; } else { name = compiler.GetTypeOutput(new System.CodeDom.CodeTypeReference(type)); } return(name.Split('.').Last()); }
public static string Friendly(Type type) { using (var provider = new Microsoft.CSharp.CSharpCodeProvider()) { var @ref = new CodeTypeReference(type); return provider.GetTypeOutput(@ref); } }
private string GetTypeOutput( ) { var type = Type.GetType(FieldTypeName); if (type != null) { using (var code = new Microsoft.CSharp.CSharpCodeProvider( )) { return(code.GetTypeOutput(new System.CodeDom.CodeTypeReference(type.FullName))); } } else { return(FieldTypeName); } }
/// <summary> /// Gets the correct type name for the Generic property /// </summary> /// <returns></returns> private string GetReadableTypeName() { //If it isn't a generic just pass return the class name if (!fieldInfo.FieldType.IsGenericType) { return(fieldInfo.FieldType.Name); } //Gets primitive names (e.g. Float instead of single) var compiler = new Microsoft.CSharp.CSharpCodeProvider(); string readableName = compiler.GetTypeOutput(new System.CodeDom.CodeTypeReference(fieldInfo.FieldType.GetGenericArguments()[0])); //Remove Namespace by finding last '.' int lastIndex = readableName.LastIndexOf('.'); if (lastIndex >= 0 && lastIndex != readableName.Length - 1) { readableName = readableName.Substring(lastIndex + 1); } //Add variable to make clear it's a generic return($"Variable<{readableName}>"); }