DescriptionUtil
using Castle.Core;
using Castle.Core.Internal;
using Castle.MicroKernel;
using System;
using System.ComponentModel;
namespace Castle.Windsor.Diagnostics.Helpers
{
public static class DescriptionUtil
{
public static string GetComponentName(this IHandler handler)
{
ComponentName componentName = handler.ComponentModel.ComponentName;
if (componentName.SetByUser)
return $"""{componentName.Name}""{handler.GetServicesDescription()}";
return handler.GetServicesDescription();
}
public static string GetLifestyleDescription(this ComponentModel componentModel)
{
if (componentModel.LifestyleType == LifestyleType.Undefined)
return $"{LifestyleType.Singleton}""";
if (componentModel.LifestyleType != LifestyleType.Custom)
return componentModel.LifestyleType.ToString();
return componentModel.CustomLifestyle.get_Name();
}
public static string GetLifestyleDescriptionLong(this ComponentModel componentModel)
{
if (componentModel.LifestyleType == LifestyleType.Undefined)
return $"{componentModel.LifestyleType}""{LifestyleType.Singleton}""";
if (componentModel.LifestyleType == LifestyleType.Scoped) {
Type scopeAccessorType = componentModel.GetScopeAccessorType();
if ((object)scopeAccessorType == null)
return "Scoped explicitly";
DescriptionAttribute attribute = AttributesUtil.GetAttribute<DescriptionAttribute>(scopeAccessorType);
if (attribute != null)
return "Scoped " + attribute.Description;
return "Scoped via " + scopeAccessorType.ToCSharpString();
}
if (componentModel.LifestyleType != LifestyleType.Custom)
return componentModel.LifestyleType.ToString();
return "Custom: " + componentModel.CustomLifestyle.get_Name();
}
public static string GetServicesDescription(this IHandler handler)
{
return handler.ComponentModel.ToString();
}
}
}