Skip to content

Error in Generic class with Ienumerable generic parameter #11

Description

@luizfbicalho

Description

I have an error when I use the unity.configuration that I can't reproduce without the configuration,
I have Interfaces IRepository and IRule
and the Repository class receives an IEnumerable<IRule in the constructor

If I use the unity.config file to register those registrations, I get an error in the resolve of the IRepository.
But if I register by code, I can't reproduce the error, I think that something is missing in the registration, but I can't find what it is.

To Reproduce

Please provide UnitTest in the form of:

class ErrorGenericAndConfig
	{
		[TestMethod]
		static void ErrorWithGenericIEnumerableAndUnityConfig()
		{
			var container = new UnityContainer();

			var unitySection = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
			if (unitySection != null)
			{
				unitySection.Configure(container);

			}
			var rep = container.Resolve<IRepository<Entity>>();

			AssertExtensions.IsInstanceOfType(rep, typeof(Repository<Entity>));

			var container2 = new UnityContainer();
			container2.RegisterType(typeof(IRepository<>), typeof(Repository<>),new HierarchicalLifetimeManager());
			container2.RegisterType(typeof(IRule<>), typeof(Rule1<>), "Rule1", new HierarchicalLifetimeManager(),new Unity.Registration.InjectionMember[] { });
			container2.RegisterType(typeof(IRule<>), typeof(Rule2<>), "Rule2", new HierarchicalLifetimeManager(), new Unity.Registration.InjectionMember[] { });
			var rep2 = container2.Resolve<IRepository<Entity>>();

			AssertExtensions.IsInstanceOfType(rep2, typeof(Repository<Entity>));


		}
	}

	public interface IRepository<T>
	{
	}

	public class Repository<T> : IRepository<T>
	{
		public Repository(IEnumerable<IRule<T>> rules)
		{
			this.Rules = rules;
		}

		public IEnumerable<IRule<T>> Rules { get; }
	}

	public interface IRule<T>
	{

	}
	public class Rule2<T> : IRule<T>
	{

	}
	public class Rule1<T> : IRule<T>
	{

	}

	public class Entity
	{
		public int Id { get; set; }
		public string Description { get; set; }
	}

Additional context

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> <container> <register type="ErroUnityGeneric.IRepository1, ErroUnityGeneric"
mapTo="ErroUnityGeneric.Repository`1, ErroUnityGeneric"
name="">




	<register type="ErroUnityGeneric.IRule`1, ErroUnityGeneric"
					mapTo="ErroUnityGeneric.Rule1`1, ErroUnityGeneric"
					name="Rule1">
		<lifetime  type="hierarchical"/>
		<constructor >
		</constructor>
	</register>
	<register type="ErroUnityGeneric.IRule`1, ErroUnityGeneric"
					mapTo="ErroUnityGeneric.Rule2`1, ErroUnityGeneric"
					name="Rule2">
		<lifetime  type="hierarchical"/>
		<constructor >
		</constructor>
	</register>

</container>

`

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions