From: <fli...@us...> - 2009-11-02 00:32:25
|
Revision: 272 http://structuremap.svn.sourceforge.net/structuremap/?rev=272&view=rev Author: flimflan Date: 2009-11-02 00:32:15 +0000 (Mon, 02 Nov 2009) Log Message: ----------- Moved all functionality from TypeRules base class to TypeExtensions Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/Graph/Constructor.cs trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs trunk/Source/StructureMap/Graph/ITypeScanner.cs trunk/Source/StructureMap/Graph/Plugin.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/Graph/SetterProperty.cs trunk/Source/StructureMap/Graph/SingleImplementationScanner.cs trunk/Source/StructureMap/InstanceBuilderList.cs trunk/Source/StructureMap/Interceptors/FilteredInstanceInterceptor.cs trunk/Source/StructureMap/Model.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs trunk/Source/StructureMap/Pipeline/LiteralInstance.cs trunk/Source/StructureMap/Pipeline/PrototypeInstance.cs trunk/Source/StructureMap/Pipeline/UserControlInstance.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap/TypeExtensions.cs trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs trunk/Source/StructureMap.Testing/Graph/PluginTester.cs trunk/Source/StructureMap.Testing/Graph/TypeFindingTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj trunk/Source/StructureMap.Testing/TypeExtensionsTester.cs Removed Paths: ------------- trunk/Source/StructureMap/Graph/TypeRules.cs trunk/Source/StructureMap.Testing/Pipeline/TypeRulesTester.cs Modified: trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -28,7 +28,7 @@ throw new StructureMapException(180, _pluggedType.AssemblyQualifiedName); } - if (!TypeRules.CanBeCast(pluginType, _pluggedType)) + if (!_pluggedType.CanBeCastTo(pluginType)) { throw new StructureMapException( 303, Modified: trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs =================================================================== --- trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -5,13 +5,13 @@ namespace StructureMap.Configuration { - public class PrimitiveArrayReader : TypeRules, ITypeReader + public class PrimitiveArrayReader : ITypeReader { #region ITypeReader Members public bool CanProcess(Type pluginType) { - return IsPrimitiveArray(pluginType); + return pluginType.IsPrimitiveArray(); } public Instance Read(XmlNode node, Type pluginType) Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Container.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -10,7 +10,7 @@ namespace StructureMap { - public class Container : TypeRules, IContainer + public class Container : IContainer { private InterceptorLibrary _interceptorLibrary; private PipelineGraph _pipelineGraph; @@ -276,7 +276,7 @@ [Obsolete("Please use GetInstance(Type) instead")] public object FillDependencies(Type type) { - if (!IsConcrete(type)) + if (!type.IsConcrete()) { throw new StructureMapException(230, type.FullName); } @@ -614,7 +614,7 @@ /// <param name="object"></param> public void Inject(Type pluginType, object @object) { - if (!CanBeCast(pluginType, @object.GetType())) + if (!@object.GetType().CanBeCastTo(pluginType)) { throw new StructureMapException(220, pluginType.FullName, @object.GetType().FullName); Modified: trunk/Source/StructureMap/Graph/Constructor.cs =================================================================== --- trunk/Source/StructureMap/Graph/Constructor.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/Constructor.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -4,7 +4,7 @@ namespace StructureMap.Graph { - public class Constructor : TypeRules + public class Constructor { private ConstructorInfo _ctor; private readonly Type _pluggedType; @@ -69,7 +69,7 @@ foreach (ParameterInfo parameter in _ctor.GetParameters()) { - if (!IsAutoFillable(parameter.ParameterType)) + if (!parameter.ParameterType.IsAutoFillable()) { return false; } @@ -125,11 +125,11 @@ private void visitParameter(ParameterInfo info, Type parameterType, IArgumentVisitor visitor) { - if (IsPrimitive(parameterType)) visitor.PrimitiveParameter(info); - if (IsChild(parameterType)) visitor.ChildParameter(info); - if (IsChildArray(parameterType)) visitor.ChildArrayParameter(info); - if (IsEnum(parameterType)) visitor.EnumParameter(info); - if (IsString(parameterType)) visitor.StringParameter(info); + if (parameterType.IsPrimitive()) visitor.PrimitiveParameter(info); + if (parameterType.IsChild()) visitor.ChildParameter(info); + if (parameterType.IsChildArray()) visitor.ChildArrayParameter(info); + if (parameterType.IsEnum) visitor.EnumParameter(info); + if (parameterType.IsString()) visitor.StringParameter(info); } public bool HasArguments() Modified: trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs =================================================================== --- trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -2,7 +2,7 @@ namespace StructureMap.Graph { - public class FindAllTypesFilter : TypeRules, ITypeScanner + public class FindAllTypesFilter : ITypeScanner { private readonly Type _pluginType; private Func<Type, string> _getName = type => PluginCache.GetPlugin(type).ConcreteKey; Modified: trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -24,13 +24,13 @@ public Type PluginType { get; private set; } } - internal class TypeMapBuilder : TypeRules, ITypeScanner, IDisposable + internal class TypeMapBuilder : ITypeScanner, IDisposable { private readonly Cache<Type, List<Type>> _implementations = new Cache<Type, List<Type>>(t => new List<Type>()); public void Process(Type type, PluginGraph graph) { - if (!IsConcrete(type) || !Constructor.HasConstructors(type)) return; + if (!type.IsConcrete() || !Constructor.HasConstructors(type)) return; var pluginTypes = FindPluginTypes(type); foreach (var pluginType in pluginTypes) Modified: trunk/Source/StructureMap/Graph/ITypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -7,13 +7,13 @@ void Process(Type type, PluginGraph graph); } - public class DefaultConventionScanner : TypeRules, ITypeScanner + public class DefaultConventionScanner : ITypeScanner { #region ITypeScanner Members public void Process(Type type, PluginGraph graph) { - if (!IsConcrete(type)) return; + if (!type.IsConcrete()) return; Type pluginType = FindPluginType(type); if (pluginType != null && Constructor.HasConstructors(type)) Modified: trunk/Source/StructureMap/Graph/Plugin.cs =================================================================== --- trunk/Source/StructureMap/Graph/Plugin.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/Plugin.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -11,7 +11,7 @@ /// PluginFamily\x92s PluginType. The properties of a Plugin are the CLR Type of the concrete class, /// and the human-friendly concrete key that StructureMap will use to identify the Type. /// </summary> - public class Plugin : TypeRules + public class Plugin { public static readonly string DEFAULT = "DEFAULT"; private readonly Constructor _constructor; Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -11,7 +11,7 @@ /// the system. A PluginFamily defines a CLR Type that StructureMap can build, and all of the possible /// Plugin\x92s implementing the CLR Type. /// </summary> - public class PluginFamily : TypeRules, IPluginFamily + public class PluginFamily : IPluginFamily { private readonly Cache<string, Instance> _instances = new Cache<string, Instance>(delegate { return null; }); private readonly List<InstanceMemento> _mementoList = new List<InstanceMemento>(); @@ -173,7 +173,7 @@ private void assertPluggability(Type pluggedType) { - if (!CanBeCast(_pluginType, pluggedType)) + if (!pluggedType.CanBeCastTo(_pluginType)) { throw new StructureMapException(104, pluggedType, _pluginType); } @@ -291,7 +291,7 @@ public void AddType(Type concreteType) { - if (!CanBeCast(_pluginType, concreteType)) return; + if (!concreteType.CanBeCastTo(_pluginType)) return; if (FindPlugin(concreteType) == null) { @@ -301,7 +301,7 @@ public void AddType(Type concreteType, string name) { - if (!CanBeCast(_pluginType, concreteType)) return; + if (!concreteType.CanBeCastTo(_pluginType)) return; if (FindPlugin(name) == null) { Modified: trunk/Source/StructureMap/Graph/SetterProperty.cs =================================================================== --- trunk/Source/StructureMap/Graph/SetterProperty.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/SetterProperty.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -7,7 +7,7 @@ /// <summary> /// Represents a PropertyInfo of a Plugin.PluggedType that is filled by Setter Injection /// </summary> - public class SetterProperty : TypeRules + public class SetterProperty { private readonly PropertyInfo _property; @@ -34,7 +34,7 @@ public bool CanBeAutoFilled { - get { return IsAutoFillable(_property.PropertyType); } + get { return _property.PropertyType.IsAutoFillable(); } } public void Visit(IArgumentVisitor visitor) @@ -44,11 +44,11 @@ // Ignore indexer properties if (_property.GetIndexParameters().Length > 0) return; - if (IsPrimitive(propertyType)) visitor.PrimitiveSetter(_property, IsMandatory); - if (IsChild(propertyType)) visitor.ChildSetter(_property, IsMandatory); - if (IsChildArray(propertyType)) visitor.ChildArraySetter(_property, IsMandatory); - if (IsEnum(propertyType)) visitor.EnumSetter(_property, IsMandatory); - if (IsString(propertyType)) visitor.StringSetter(_property, IsMandatory); + if (propertyType.IsPrimitive()) visitor.PrimitiveSetter(_property, IsMandatory); + if (propertyType.IsChild()) visitor.ChildSetter(_property, IsMandatory); + if (propertyType.IsChildArray()) visitor.ChildArraySetter(_property, IsMandatory); + if (propertyType.IsEnum) visitor.EnumSetter(_property, IsMandatory); + if (propertyType.IsString()) visitor.StringSetter(_property, IsMandatory); } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/SingleImplementationScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/SingleImplementationScanner.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/SingleImplementationScanner.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -3,7 +3,7 @@ namespace StructureMap.Graph { - public class SingleImplementationScanner : TypeRules, IHeavyweightTypeScanner + public class SingleImplementationScanner : IHeavyweightTypeScanner { public void Process(PluginGraph graph, IEnumerable<TypeMap> typeMaps) { Deleted: trunk/Source/StructureMap/Graph/TypeRules.cs =================================================================== --- trunk/Source/StructureMap/Graph/TypeRules.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/TypeRules.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -1,117 +0,0 @@ -using System; - -namespace StructureMap.Graph -{ - public class TypeRules - { - /// <summary> - /// Determines if the pluggedType can be upcast to the pluginType - /// </summary> - /// <param name="pluginType"></param> - /// <param name="pluggedType"></param> - /// <returns></returns> - public static bool CanBeCast(Type pluginType, Type pluggedType) - { - if (pluggedType.IsInterface || pluggedType.IsAbstract) - { - return false; - } - - if (noPublicConstructors(pluggedType)) - { - return false; - } - - if (IsGeneric(pluginType)) - { - return GenericsPluginGraph.CanBeCast(pluginType, pluggedType); - } - - if (IsGeneric(pluggedType)) - { - return false; - } - - - return pluginType.IsAssignableFrom(pluggedType); - } - - public static bool IsGeneric(Type pluggedType) - { - return pluggedType.IsGenericTypeDefinition || pluggedType.ContainsGenericParameters; - } - - /// <summary> - /// Determines if the PluggedType is a valid Plugin into the - /// PluginType - /// </summary> - /// <param name="pluginType"></param> - /// <param name="pluggedType"></param> - /// <returns></returns> - public static bool IsExplicitlyMarkedAsPlugin(Type pluginType, Type pluggedType) - { - bool returnValue = false; - - bool markedAsPlugin = PluggableAttribute.MarkedAsPluggable(pluggedType); - if (markedAsPlugin) - { - returnValue = CanBeCast(pluginType, pluggedType); - } - - return returnValue; - } - - private static bool noPublicConstructors(Type pluggedType) - { - return pluggedType.GetConstructors().Length == 0; - } - - protected bool IsString(Type type) - { - return type.Equals(typeof (string)); - } - - protected bool IsPrimitive(Type type) - { - return type.IsPrimitive && !IsString(type) && type != typeof (IntPtr); - } - - protected internal bool IsSimple(Type type) - { - return type.IsPrimitive || IsString(type) || IsEnum(type); - } - - protected bool IsEnum(Type type) - { - return type.IsEnum; - } - - protected bool IsChild(Type type) - { - return IsPrimitiveArray(type) || (!type.IsArray && !IsSimple(type)); - } - - protected bool IsChildArray(Type type) - { - return type.IsArray && !IsSimple(type.GetElementType()); - } - - protected bool IsPrimitiveArray(Type type) - { - return type.IsArray && IsSimple(type.GetElementType()); - } - - protected internal bool IsConcrete(Type type) - { - return !type.IsInterface && !type.IsAbstract; - } - - - protected bool IsAutoFillable(Type type) - { - return IsChild(type) || IsChildArray(type); - } - } - - -} \ No newline at end of file Modified: trunk/Source/StructureMap/InstanceBuilderList.cs =================================================================== --- trunk/Source/StructureMap/InstanceBuilderList.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/InstanceBuilderList.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -42,7 +42,7 @@ } // Add a missing PluggedType if we can - if (TypeRules.CanBeCast(_pluginType, pluggedType)) + if (pluggedType.CanBeCastTo(_pluginType)) { var plugin = new Plugin(pluggedType); processPlugin(plugin); Modified: trunk/Source/StructureMap/Interceptors/FilteredInstanceInterceptor.cs =================================================================== --- trunk/Source/StructureMap/Interceptors/FilteredInstanceInterceptor.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Interceptors/FilteredInstanceInterceptor.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -35,7 +35,7 @@ public bool MatchesType(Type type) { - return TypeRules.CanBeCast(_pluginType, type); + return type.CanBeCastTo(_pluginType); } public object Process(object target, IContext context) Modified: trunk/Source/StructureMap/Model.cs =================================================================== --- trunk/Source/StructureMap/Model.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Model.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -64,7 +64,7 @@ bool HasImplementationsFor<T>(); } - public class Model : TypeRules, IModel + public class Model : IModel { private readonly PipelineGraph _graph; Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -62,7 +62,7 @@ Type specificType = _pluggedType.IsGenericTypeDefinition ? _pluggedType.MakeGenericType(templateTypes) : _pluggedType; - if (TypeRules.CanBeCast(family.PluginType, specificType)) + if (specificType.CanBeCastTo(family.PluginType)) { var instance = new ConfiguredInstance(specificType); instance._arrays = _arrays; Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -178,7 +178,7 @@ protected override bool canBePartOfPluginFamily(PluginFamily family) { - return TypeRules.CanBeCast(family.PluginType, _pluggedType); + return _pluggedType.CanBeCastTo(family.PluginType); } internal override bool Matches(Plugin plugin) Modified: trunk/Source/StructureMap/Pipeline/LiteralInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/LiteralInstance.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Pipeline/LiteralInstance.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -36,7 +36,7 @@ protected override bool canBePartOfPluginFamily(PluginFamily family) { - return TypeRules.CanBeCast(family.PluginType, _object.GetType()); + return _object.GetType().CanBeCastTo(family.PluginType); } protected override string getDescription() Modified: trunk/Source/StructureMap/Pipeline/PrototypeInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/PrototypeInstance.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Pipeline/PrototypeInstance.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -27,7 +27,7 @@ protected override bool canBePartOfPluginFamily(PluginFamily family) { - return TypeRules.CanBeCast(family.PluginType, _prototype.GetType()); + return _prototype.GetType().CanBeCastTo(family.PluginType); } protected override string getDescription() Modified: trunk/Source/StructureMap/Pipeline/UserControlInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/UserControlInstance.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Pipeline/UserControlInstance.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -29,7 +29,7 @@ Control control = new Page().LoadControl(_url); Type pluggedType = control.GetType(); - if (!TypeRules.CanBeCast(pluginType, pluggedType)) + if (!pluggedType.CanBeCastTo(pluginType)) { throw new StructureMapException(303, pluginType, pluggedType); } Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/StructureMap.csproj 2009-11-02 00:32:15 UTC (rev 272) @@ -151,7 +151,6 @@ <Compile Include="Graph\IArgumentVisitor.cs" /> <Compile Include="Graph\IPluginFamily.cs" /> <Compile Include="Graph\AssemblyScanner.cs" /> - <Compile Include="Graph\TypeRules.cs" /> <Compile Include="InstanceBuilderList.cs" /> <Compile Include="InstanceCache.cs" /> <Compile Include="InstanceFamily.cs" /> Modified: trunk/Source/StructureMap/TypeExtensions.cs =================================================================== --- trunk/Source/StructureMap/TypeExtensions.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/TypeExtensions.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -17,26 +17,11 @@ return new ReferencedInstance(key); } - public static bool IsSimple(this Type type) - { - return new TypeRules().IsSimple(type); - } - - public static bool IsConcrete(this Type type) - { - return new TypeRules().IsConcrete(type); - } - public static bool IsGeneric(this Type type) { return type.IsGenericTypeDefinition || type.ContainsGenericParameters; } - public static bool CanBeCastTo(this Type pluggedType, Type pluginType) - { - return TypeRules.CanBeCast(pluginType, pluggedType); - } - public static bool IsConcreteAndAssignableTo(this Type pluggedType, Type pluginType) { return pluggedType.IsConcrete() && pluginType.IsAssignableFrom(pluggedType); @@ -87,7 +72,7 @@ if (type.IsGenericType) { string[] parameters = Array.ConvertAll(type.GetGenericArguments(), t => t.GetName()); - var parameterList = string.Join(", ", parameters); + var parameterList = String.Join(", ", parameters); return "{0}<{1}>".ToFormat(type.Name, parameterList); } @@ -99,12 +84,109 @@ if (type.IsGenericType) { string[] parameters = Array.ConvertAll(type.GetGenericArguments(), t => t.GetName()); - var parameterList = string.Join(", ", parameters); + var parameterList = String.Join(", ", parameters); return "{0}<{1}>".ToFormat(type.Name, parameterList); } return type.FullName; } + /// <summary> + /// Determines if the pluggedType can be upcast to the pluginType + /// </summary> + /// <param name="pluginType"></param> + /// <param name="pluggedType"></param> + /// <returns></returns> + public static bool CanBeCastTo(this Type pluggedType, Type pluginType) + { + if (pluggedType.IsInterface || pluggedType.IsAbstract) + { + return false; + } + + if (noPublicConstructors(pluggedType)) + { + return false; + } + + if (IsGeneric(pluginType)) + { + return GenericsPluginGraph.CanBeCast(pluginType, pluggedType); + } + + if (IsGeneric(pluggedType)) + { + return false; + } + + + return pluginType.IsAssignableFrom(pluggedType); + } + + + /// <summary> + /// Determines if the PluggedType is a valid Plugin into the + /// PluginType + /// </summary> + /// <param name="pluginType"></param> + /// <param name="pluggedType"></param> + /// <returns></returns> + public static bool IsExplicitlyMarkedAsPlugin(this Type pluggedType, Type pluginType) + { + bool returnValue = false; + + bool markedAsPlugin = PluggableAttribute.MarkedAsPluggable(pluggedType); + if (markedAsPlugin) + { + returnValue = CanBeCastTo(pluggedType, pluginType); + } + + return returnValue; + } + + private static bool noPublicConstructors(Type pluggedType) + { + return pluggedType.GetConstructors().Length == 0; + } + + public static bool IsString(this Type type) + { + return type.Equals(typeof (string)); + } + + public static bool IsPrimitive(this Type type) + { + return type.IsPrimitive && !IsString(type) && type != typeof (IntPtr); + } + + public static bool IsSimple(this Type type) + { + return type.IsPrimitive || IsString(type) || type.IsEnum; + } + + public static bool IsChild(this Type type) + { + return IsPrimitiveArray(type) || (!type.IsArray && !IsSimple(type)); + } + + public static bool IsChildArray(this Type type) + { + return type.IsArray && !IsSimple(type.GetElementType()); + } + + public static bool IsPrimitiveArray(this Type type) + { + return type.IsArray && IsSimple(type.GetElementType()); + } + + public static bool IsConcrete(this Type type) + { + return !type.IsAbstract; + } + + public static bool IsAutoFillable(this Type type) + { + return IsChild(type) || IsChildArray(type); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -137,11 +137,10 @@ [Test] public void CanPlugGenericConcreteClassIntoGenericInterfaceWithNoGenericParametersSpecified() { - bool canPlug = TypeRules.CanBeCast(typeof (IGenericService<>), typeof (GenericService<>)); + bool canPlug = typeof (GenericService<>).CanBeCastTo(typeof (IGenericService<>)); Assert.IsTrue(canPlug); } - [Test] public void Define_profile_with_generics_and_concrete_type() { Modified: trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -181,7 +181,7 @@ var family = new PluginFamily(typeof (IGateway), graph); var instance = new ConfiguredInstance(typeof (ColorRule)); - Assert.IsFalse(TypeRules.CanBeCast(typeof (IGateway), typeof (Rule))); + Assert.IsFalse(typeof (Rule).CanBeCastTo(typeof (IGateway))); family.AddInstance(instance); Modified: trunk/Source/StructureMap.Testing/Graph/PluginTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/PluginTester.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap.Testing/Graph/PluginTester.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -97,13 +97,13 @@ [Test] public void BadPluginToAbstractClass() { - Assert.AreEqual(false, TypeRules.CanBeCast(_widgetmaker, _colorWidget), "ColorWidget is NOT a WidgetMaker"); + Assert.AreEqual(false, _colorWidget.CanBeCastTo(_widgetmaker), "ColorWidget is NOT a WidgetMaker"); } [Test] public void BadPluginToInterface() { - Assert.AreEqual(false, TypeRules.CanBeCast(_iwidget, _moneywidgetmaker), + Assert.AreEqual(false, _moneywidgetmaker.CanBeCastTo(_iwidget), "MoneyWidgetMaker is NOT an IWidget"); } @@ -190,15 +190,13 @@ [Test] public void CanNotPluginWithoutAttribute() { - string msg = "NotPluggableWidget cannot plug into IWidget automatically"; - Assert.AreEqual(false, TypeRules.IsExplicitlyMarkedAsPlugin(_iwidget, typeof (NotPluggable)), msg); + Assert.IsFalse(typeof (NotPluggable).IsExplicitlyMarkedAsPlugin(_iwidget), "NotPluggableWidget cannot plug into IWidget automatically"); } [Test] public void CanPluginWithAttribute() { - Assert.AreEqual(true, TypeRules.IsExplicitlyMarkedAsPlugin(_iwidget, _colorWidget), - "ColorWidget plugs into IWidget"); + Assert.IsTrue(_colorWidget.IsExplicitlyMarkedAsPlugin(_iwidget), "ColorWidget plugs into IWidget"); } @@ -300,14 +298,14 @@ [Test] public void GoodPluginToAbstractClass() { - Assert.AreEqual(true, TypeRules.CanBeCast(_widgetmaker, _moneywidgetmaker), + Assert.AreEqual(true, _moneywidgetmaker.CanBeCastTo(_widgetmaker), "MoneyWidgetMaker is a WidgetMaker"); } [Test] public void GoodPluginToInterface() { - Assert.AreEqual(true, TypeRules.CanBeCast(_iwidget, _colorWidget), "ColorWidget is an IWidget"); + Assert.AreEqual(true, _colorWidget.CanBeCastTo(_iwidget), "ColorWidget is an IWidget"); } [Test] Modified: trunk/Source/StructureMap.Testing/Graph/TypeFindingTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/TypeFindingTester.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap.Testing/Graph/TypeFindingTester.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -31,7 +31,7 @@ [Test] public void DoNotFindPluginWithNoPublicCTOR() { - Assert.IsFalse(TypeRules.CanBeCast(typeof (TypeIWantToFind), typeof (GreenType))); + Assert.IsFalse(typeof (GreenType).CanBeCastTo(typeof (TypeIWantToFind))); } Deleted: trunk/Source/StructureMap.Testing/Pipeline/TypeRulesTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/TypeRulesTester.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap.Testing/Pipeline/TypeRulesTester.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -1,86 +0,0 @@ -using NUnit.Framework; -using StructureMap.Graph; -using StructureMap.Testing.Widget2; -using StructureMap.Testing.Widget3; - -namespace StructureMap.Testing.Pipeline -{ - [TestFixture] - public class TypeRulesTester : TypeRules - { - #region Setup/Teardown - - [SetUp] - public void SetUp() - { - } - - #endregion - - [Test] - public void IsChild() - { - Assert.IsFalse(IsChild(typeof (int))); - Assert.IsFalse(IsChild(typeof (bool))); - Assert.IsFalse(IsChild(typeof (double))); - Assert.IsFalse(IsChild(typeof (string))); - Assert.IsFalse(IsChild(typeof (BreedEnum))); - Assert.IsFalse(IsChild(typeof (IGateway[]))); - Assert.IsTrue(IsChild(typeof (IGateway))); - } - - [Test] - public void IsChildArray() - { - Assert.IsFalse(IsChildArray(typeof (int))); - Assert.IsFalse(IsChildArray(typeof (bool))); - Assert.IsFalse(IsChildArray(typeof (double))); - Assert.IsFalse(IsChildArray(typeof (double[]))); - Assert.IsFalse(IsChildArray(typeof (string))); - Assert.IsFalse(IsChildArray(typeof (string[]))); - Assert.IsFalse(IsChildArray(typeof (BreedEnum))); - Assert.IsTrue(IsChildArray(typeof (IGateway[]))); - Assert.IsFalse(IsChildArray(typeof (IGateway))); - } - - [Test] - public void IsEnum() - { - Assert.IsFalse(IsEnum(typeof (int))); - Assert.IsFalse(IsEnum(typeof (bool))); - Assert.IsFalse(IsEnum(typeof (double))); - Assert.IsFalse(IsEnum(typeof (string))); - Assert.IsTrue(IsEnum(typeof (BreedEnum))); - Assert.IsFalse(IsEnum(typeof (IGateway))); - } - - [Test] - public void IsPrimitive() - { - Assert.IsTrue(IsPrimitive(typeof (int))); - Assert.IsTrue(IsPrimitive(typeof (bool))); - Assert.IsTrue(IsPrimitive(typeof (double))); - Assert.IsFalse(IsPrimitive(typeof (string))); - Assert.IsFalse(IsPrimitive(typeof (BreedEnum))); - Assert.IsFalse(IsPrimitive(typeof (IGateway))); - } - - [Test] - public void IsSimple() - { - Assert.IsTrue(IsSimple(typeof (int))); - Assert.IsTrue(IsSimple(typeof (bool))); - Assert.IsTrue(IsSimple(typeof (double))); - Assert.IsTrue(IsSimple(typeof (string))); - Assert.IsTrue(IsSimple(typeof (BreedEnum))); - Assert.IsFalse(IsSimple(typeof (IGateway))); - } - - [Test] - public void IsString() - { - Assert.IsTrue(IsString(typeof (string))); - Assert.IsFalse(IsString(typeof (int))); - } - } -} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-11-02 00:32:15 UTC (rev 272) @@ -374,7 +374,6 @@ <Compile Include="Pipeline\SmartInstanceTester.cs" /> <Compile Include="Pipeline\StubBuildSession.cs" /> <Compile Include="Pipeline\ThreadLocalStorageLifecycleTester.cs" /> - <Compile Include="Pipeline\TypeRulesTester.cs" /> <Compile Include="PrimitiveArrayTester.cs" /> <Compile Include="Properties\Settings.Designer.cs"> <AutoGen>True</AutoGen> Modified: trunk/Source/StructureMap.Testing/TypeExtensionsTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/TypeExtensionsTester.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap.Testing/TypeExtensionsTester.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -1,9 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using NUnit.Framework; +using NUnit.Framework; using StructureMap.Testing.GenericWidgets; +using StructureMap.Testing.Widget2; +using StructureMap.Testing.Widget3; namespace StructureMap.Testing { @@ -39,5 +37,61 @@ typeof(Service2).FindInterfaceThatCloses(typeof(IService<>)) .ShouldBeNull(); } + + [Test] + public void IsChild() + { + Assert.IsFalse(TypeExtensions.IsChild(typeof(int))); + Assert.IsFalse(TypeExtensions.IsChild(typeof(bool))); + Assert.IsFalse(TypeExtensions.IsChild(typeof(double))); + Assert.IsFalse(TypeExtensions.IsChild(typeof(string))); + Assert.IsFalse(TypeExtensions.IsChild(typeof(BreedEnum))); + Assert.IsFalse(TypeExtensions.IsChild(typeof(IGateway[]))); + Assert.IsTrue(TypeExtensions.IsChild(typeof(IGateway))); + } + + [Test] + public void IsChildArray() + { + Assert.IsFalse(TypeExtensions.IsChildArray(typeof(int))); + Assert.IsFalse(TypeExtensions.IsChildArray(typeof(bool))); + Assert.IsFalse(TypeExtensions.IsChildArray(typeof(double))); + Assert.IsFalse(TypeExtensions.IsChildArray(typeof(double[]))); + Assert.IsFalse(TypeExtensions.IsChildArray(typeof(string))); + Assert.IsFalse(TypeExtensions.IsChildArray(typeof(string[]))); + Assert.IsFalse(TypeExtensions.IsChildArray(typeof(BreedEnum))); + Assert.IsTrue(TypeExtensions.IsChildArray(typeof(IGateway[]))); + Assert.IsFalse(TypeExtensions.IsChildArray(typeof(IGateway))); + } + + [Test] + public void IsPrimitive() + { + Assert.IsTrue(typeof(int).IsPrimitive()); + Assert.IsTrue(typeof(bool).IsPrimitive()); + Assert.IsTrue(typeof(double).IsPrimitive()); + Assert.IsFalse(typeof(string).IsPrimitive()); + Assert.IsFalse(typeof(BreedEnum).IsPrimitive()); + Assert.IsFalse(typeof(IGateway).IsPrimitive()); + } + + [Test] + public void IsSimple() + { + Assert.IsTrue(TypeExtensions.IsSimple(typeof(int))); + Assert.IsTrue(TypeExtensions.IsSimple(typeof(bool))); + Assert.IsTrue(TypeExtensions.IsSimple(typeof(double))); + Assert.IsTrue(TypeExtensions.IsSimple(typeof(string))); + Assert.IsTrue(TypeExtensions.IsSimple(typeof(BreedEnum))); + Assert.IsFalse(TypeExtensions.IsSimple(typeof(IGateway))); + } + + [Test] + public void IsString() + { + Assert.IsTrue(TypeExtensions.IsString(typeof(string))); + Assert.IsFalse(TypeExtensions.IsString(typeof(int))); + } + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |