Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the ObjectToTypeName value converter to give prettier C# names. #400

Merged
merged 9 commits into from
Mar 26, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
using System;
using System.Globalization;
using Xenko.Core.Annotations;
using System.CodeDom;
using Microsoft.CSharp;
using System.Text.RegularExpressions;

namespace Xenko.Core.Presentation.ValueConverters
{
Expand All @@ -19,11 +22,22 @@ public class ObjectToTypeName : OneWayValueConverter<ObjectToTypeName>
/// </summary>
public const string NullObjectType = "(None)";

private readonly CSharpCodeProvider codeProvider = new CSharpCodeProvider();

/// <inheritdoc/>
[NotNull]
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value?.GetType().Name ?? NullObjectType;
if (value == null) return NullObjectType;

Type valueType = value.GetType();
var fullTypeName = codeProvider.GetTypeOutput(new CodeTypeReference(valueType));

//Change System.Nullable to "Type?" format
var typeName = Regex.Replace(fullTypeName, @"System\.Nullable<([^>]*)>", "$1?");
dfkeenan marked this conversation as resolved.
Show resolved Hide resolved

//Simplify name. i.e. remove namespaces etc.
return Regex.Replace(typeName, @"[^,<>]*\.", "");
}
}
}