object[] GetConstructorParametersForType(Type t)
        {
            var constructors = t.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            if (constructors.Length == 0)
            {
                throw new ArgumentException("Found no constructor for type " + t);
            }
            if (constructors.Length > 1)
            {
                throw new ArgumentException("Found multiple constructors for type " + t + " currently supported is only one constructor");
            }
            var constructor = constructors[0];
            var parameters  = constructor.GetParameters();

            if (parameters.Length == 0)
            {
                return(new object[0]);
            }

            var pars = new List <object>();

            foreach (var p in parameters)
            {
                var    attrs          = p.GetCustomAttributes(typeof(NamedDependencyAttribute));
                string dependencyName = null;
                if (attrs.FirstOrDefault() is NamedDependencyAttribute attr)
                {
                    dependencyName = attr.Name;
                }

                var dependency =
                    dependencyName == null
                        ? _factory.Get(p.ParameterType)
                        : _factory.Get(dependencyName, p.ParameterType);

                if (dependency == null)
                {
                    throw new ArgumentException("Unknown type " + p.ParameterType + " needed for construction of " + t);
                }
                pars.Add(dependency);
            }
            return(pars.ToArray());
        }
Example #2
0
 public static TAni Play <TAni>() where TAni : IAnimation
 {
     return(Play(_create.Get <TAni>()));
 }