From d7ff1dabb6d9dc2cd239471fa728e979ec1510fb Mon Sep 17 00:00:00 2001 From: Krzysztof Kozmic Date: Thu, 13 Jan 2011 20:14:21 +1000 Subject: [PATCH] C#-ified type names shown in debugger views --- src/Castle.Windsor/Castle.Windsor.csproj | 1 + src/Castle.Windsor/Core/ComponentModel.cs | 8 +-- src/Castle.Windsor/Core/Internal/TypeUtil.cs | 69 ++++++++++++++++++++ 3 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 src/Castle.Windsor/Core/Internal/TypeUtil.cs diff --git a/src/Castle.Windsor/Castle.Windsor.csproj b/src/Castle.Windsor/Castle.Windsor.csproj index 5996486c10..3d0e871417 100644 --- a/src/Castle.Windsor/Castle.Windsor.csproj +++ b/src/Castle.Windsor/Castle.Windsor.csproj @@ -374,6 +374,7 @@ + diff --git a/src/Castle.Windsor/Core/ComponentModel.cs b/src/Castle.Windsor/Core/ComponentModel.cs index 3cfd8c6c93..cab235bfeb 100644 --- a/src/Castle.Windsor/Core/ComponentModel.cs +++ b/src/Castle.Windsor/Core/ComponentModel.cs @@ -352,21 +352,21 @@ public override string ToString() var services = Services.ToArray(); if (services.Length == 1 && services.Single() == Implementation) { - return Implementation.Name; + return Implementation.ToCSharpString(); } string value; if (Implementation == typeof(LateBoundComponent)) { - value = string.Format("late bound {0}", services[0].Name); + value = string.Format("late bound {0}", services[0].ToCSharpString()); } else if (Implementation == null) { - value = "no impl / " + services[0].Name; + value = "no impl / " + services[0].ToCSharpString(); } else { - value = string.Format("{0} / {1}", Implementation.Name, services[0].Name); + value = string.Format("{0} / {1}", Implementation.ToCSharpString(), services[0].ToCSharpString()); } if (services.Length > 1) { diff --git a/src/Castle.Windsor/Core/Internal/TypeUtil.cs b/src/Castle.Windsor/Core/Internal/TypeUtil.cs new file mode 100644 index 0000000000..c388147cf1 --- /dev/null +++ b/src/Castle.Windsor/Core/Internal/TypeUtil.cs @@ -0,0 +1,69 @@ +// Copyright 2004-2011 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Castle.Core.Internal +{ + using System; + using System.Text; + + public static class TypeUtil + { + public static string ToCSharpString(this Type type) + { + try + { + var name = new StringBuilder(); + ToCSharpString(type, name); + return name.ToString(); + } + catch (Exception) + { + // in case we messed up something... + return type.Name; + } + } + + private static void AppendGenericParameters(StringBuilder name, Type[] genericArguments) + { + name.Append("<"); + + for (var i = 0; i < genericArguments.Length - 1; i++) + { + ToCSharpString(genericArguments[i], name); + name.Append(", "); + } + if (genericArguments.Length > 0) + { + ToCSharpString(genericArguments[genericArguments.Length - 1], name); + } + name.Append(">"); + } + + private static void ToCSharpString(Type type, StringBuilder name) + { + if (type.IsGenericType == false) + { + if (type.IsGenericParameter) + { + name.AppendFormat("·{0}·", type.Name); + return; + } + name.Append(type.Name); + return; + } + name.Append(type.Name.Split('`')[0]); + AppendGenericParameters(name, type.GetGenericArguments()); + } + } +} \ No newline at end of file