예제 #1
0
        public void D()
        {
            Assembly a = Assembly.LoadFile(@"f:\UTE\Experiments\ComReflection\Ref\bin\Debug\ref.dll");

            MessageBox.Show("Create");
            dynamic dyn = a.CreateInstance("Ref.A");

            MessageBox.Show("Type name: " + dyn.GetType().FullName);
            IA pr = (IA)dyn;

            MessageBox.Show("Type name: " + pr.GetType().FullName);
            MessageBox.Show(pr.J());

            /*Type t = a.GetType("Ref.A");
             * Type[] tt = Type.EmptyTypes;
             * MessageBox.Show("GetConstructor");
             * ConstructorInfo c = t.GetConstructor(tt);
             * MessageBox.Show("Invoke");
             * var pr = (IA)c.Invoke(new object[] { });
             * MessageBox.Show("pr.J");
             * pr.J();*/


            //Type t = a.GetType("Ref.Fac");
            //dynamic fac = c.Invoke(new object[] { });
            //MessageBox.Show("New");
            //var p = fac.New();
            //IA pr = (IA)p;
            //I pr  = (I)fac.New();
            //PropertyInfo prop = t.GetProperty("B");
            //MessageBox.Show((string)prop.GetValue(pr, null));
            //dynamic pr = c.Invoke(new object[] { });
            //pr.J();
        }
 public static void Get <T>(this IA a, Action <T[]> action, params Type[] types) where T : IB
 {
     foreach (var type in types)
     {
         var method = a.GetType().GetMethod("Get").MakeGenericMethod(type);
         var ts     = (T[])method.Invoke(a, null);
         action(ts);
     }
 }
예제 #3
0
        /// <summary>
        /// Gets the instances from any given assembly path with specific filter
        /// </summary>
        /// <typeparam name="T">Generic - any interface</typeparam>
        /// <param name="assemblyPath">assemblies folder location</param>
        /// <param name="filter">file filter</param>
        /// <returns>List of Ts</returns>
        public static IList <T> GetInstancesFromAssemblyPath <T>(string assemblyPath, string filter)
        {
            IList <T> instances = new List <T>();

            foreach (string filename in Directory.GetFiles(assemblyPath, filter))
            {
                IList <object> registeredInstances = AssemblyResolver.Resolve(filename);
                IList <T>      instancesT          = Register <T>(registeredInstances);
                foreach (T obj in instancesT)
                {
                    if (obj != null && instances.FirstOrDefault(IA => string.Compare(IA.GetType().FullName,
                                                                                     obj.GetType().FullName, StringComparison.InvariantCultureIgnoreCase) == 0) == null)
                    {
                        instances.Add((T)obj);
                    }
                }
            }
            return(instances);
        }
예제 #4
0
        public void ConfigureContainer()
        {
            ApplicationConfiguration config = new ApplicationConfiguration();

            config.ContainerConfiguration.Items.Add(new ObjectContainerAdd()
            {
                KeyType = GetTypeFullname("IA"), ObjectType = GetTypeFullname("A")
            });
            config.ContainerConfiguration.Items.Add(new ObjectContainerAdd()
            {
                KeyType = GetTypeFullname("IB"), ObjectType = GetTypeFullname("B")
            });

            ObjectContainer container = new ObjectContainer();

            ConfigManager.ConfigureContainer(container, config);

            Assert.AreEqual(2, container.AllObjects.Length);
            IA a = container.Get <IA>();

            Assert.IsNotNull(a);
            Assert.AreSame(typeof(A), a.GetType());
        }