StartableContributor
using Castle.Core;
using Castle.Core.Internal;
using Castle.MicroKernel;
using Castle.MicroKernel.ModelBuilder;
using Castle.MicroKernel.SubSystems.Conversion;
using System;
using System.Collections.Specialized;
using System.Reflection;
namespace Castle.Facilities.Startable
{
public class StartableContributor : IContributeComponentModelConstruction
{
private readonly ITypeConverter converter;
public StartableContributor(ITypeConverter converter)
{
this.converter = converter;
}
public void ProcessModel(IKernel kernel, ComponentModel model)
{
bool flag = CheckIfComponentImplementsIStartable(model) || HasStartableAttributeSet(model);
model.ExtendedProperties["startable"] = flag;
if (flag) {
AddStart(model);
AddStop(model);
}
}
private void AddStart(ComponentModel model)
{
string text = ((NameValueCollection)model.Configuration.get_Attributes())["startMethod"];
if (text != null) {
MethodInfo method = model.Implementation.GetTypeInfo().GetMethod(text, Type.EmptyTypes);
if (method == (MethodInfo)null)
throw new ArgumentException($"""{text}""{model.Implementation}""");
model.ExtendedProperties.Add("Castle.StartableFacility.StartMethod", method);
}
model.Lifecycle.Add(StartConcern.Instance);
}
private void AddStop(ComponentModel model)
{
string text = ((NameValueCollection)model.Configuration.get_Attributes())["stopMethod"];
if (text != null) {
MethodInfo method = model.Implementation.GetTypeInfo().GetMethod(text, Type.EmptyTypes);
if (method == (MethodInfo)null)
throw new ArgumentException($"""{text}""{model.Implementation}""");
model.ExtendedProperties.Add("Castle.StartableFacility.StopMethod", method);
}
model.Lifecycle.AddFirst(StopConcern.Instance);
}
private bool HasStartableAttributeSet(ComponentModel model)
{
if ((object)model.Configuration == null)
return false;
string text = ((NameValueCollection)model.Configuration.get_Attributes())["startable"];
if (text != null)
return converter.PerformConversion<bool>(text);
return false;
}
private static bool CheckIfComponentImplementsIStartable(ComponentModel model)
{
return model.Implementation.Is<IStartable>();
}
}
}