Glass.Mapper.Sc.DataMappers.SitecoreFieldIEnumerableMapper.CanHandle C# (CSharp) Method

CanHandle() public method

Determines whether this instance can handle the specified configuration.
public CanHandle ( Mapper configuration, Context context ) : bool
configuration Mapper The configuration.
context Context The context.
return bool
        public override bool CanHandle(Mapper.Configuration.AbstractPropertyConfiguration configuration, Context context)
        {
            var scConfig = configuration as SitecoreFieldConfiguration;

            if (scConfig == null)
                return false;

            Type type = scConfig.PropertyInfo.PropertyType;

            if (!type.IsGenericType) return false;

            if (type.GetGenericTypeDefinition() != typeof(IEnumerable<>) && type.GetGenericTypeDefinition() != typeof(IList<>))
                return false;

            return true;
        }

Usage Example

        public void CanHandle_PropertyIsArray_ReturnsFalse()
        {
            //Assign
            var config = new SitecoreFieldConfiguration();
            config.PropertyInfo = typeof(StubClass).GetProperty("Array");
            var mapper = new SitecoreFieldIEnumerableMapper();

            //Act
            var result = mapper.CanHandle(config, null);

            //Assert
            Assert.IsFalse(result);
        }