Skip to content

Commit

Permalink
C#-ified type names shown in debugger views
Browse files Browse the repository at this point in the history
  • Loading branch information
kkozmic committed Jan 13, 2011
1 parent 4a1ea16 commit d7ff1da
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Castle.Windsor/Castle.Windsor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@
<Compile Include="Compatibility\SilverlightHacks.cs" />
<Compile Include="Core\Internal\ThreadSafeInit.cs" />
<Compile Include="Core\Internal\TypeByInheritanceDepthMostSpecificFirstComparer.cs" />
<Compile Include="Core\Internal\TypeUtil.cs" />
<Compile Include="Core\LifestyleAttributes.cs" />
<Compile Include="Core\LifestyleType.cs" />
<Compile Include="Core\PropertiesInspectionBehavior.cs" />
Expand Down
8 changes: 4 additions & 4 deletions src/Castle.Windsor/Core/ComponentModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
69 changes: 69 additions & 0 deletions src/Castle.Windsor/Core/Internal/TypeUtil.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}

0 comments on commit d7ff1da

Please sign in to comment.