mercredi 5 juillet 2017

Registering types in unity 2.0 using Reflection (Not generic)

I need to to dynamically register my types in unity based on namespace filtering.

Here is my code :

 var q = from t in AppDomain.CurrentDomain.GetAssemblies()
                  .SelectMany(s => s.GetTypes())
                    where (t.IsInterface || t.IsClass) && t.Namespace == namespaceToAdd
                    select t;

            var types = q.ToList();

            types.Where(t => t.IsInterface).Select(i=>               
                    {
                        var matchingClass = types.Where(c => c.IsClass && i.IsAssignableFrom(c)).FirstOrDefault();
                         if (matchingClass != null)
                        {
                            container.RegisterType(i, matchingClass, null, new ContainerControlledLifetimeManager())();
                        }

                    });

but the container call fails : "Method name" expected. "i" is the reflected interface "matchingClass" the corresponding implementation type.

I've read the topics about this in SO but did not find my solution (switching to Unity 3.0, which does this is not an option).

Msdn wasn't helpful either (http://ift.tt/2uolife)

The code will be cleaned after (I'll use only one form of linq, once this works :) )

What did I miss ?





Aucun commentaire:

Enregistrer un commentaire