Ejemplo n.º 1
0
        public WidgetComponentDescriptor Create(WidgetDescriptor widgetDescriptor)
        {
            var invokeMethod = widgetDescriptor.ComponentType.GetMethod("Invoke");

            if (invokeMethod == null)
            {
                invokeMethod = widgetDescriptor.ComponentType.GetMethod("InvokeAsync");
            }

            if (invokeMethod == null)
            {
                throw new Exception($"The Type '{widgetDescriptor.ComponentType.Name}' method Invoke or InvokeAsync not found. ");
            }

            return(new WidgetComponentDescriptor()
            {
                TypeInfo = widgetDescriptor.ComponentType.GetTypeInfo(),
                MethodInfo = invokeMethod,
                Parameters = invokeMethod.GetParameters(),
                RootName = widgetDescriptor.Assembly.GetName().Name,
            });
        }
Ejemplo n.º 2
0
        private WidgetDescriptor CreateWidgetDescriptor(WidgetConfigDescritpion descritpion, IEnumerable <Assembly> assemblies)
        {
            var descriptior = new WidgetDescriptor()
            {
                Author      = descritpion.Author,
                Description = descritpion.Description,
                Id          = descritpion.Id,
                Name        = descritpion.Name,
                Version     = descritpion.Version,
            };


            foreach (var assembly in assemblies)
            {
                Logger.LogDebug("Load assembly '{0}' ", assembly.FullName);

                var mainType = assembly.ExportedTypes.FirstOrDefault(t => !t.IsAbstract && t.BaseType != null && typeof(IWidget).IsAssignableFrom(t));

                if (mainType == null)
                {
                    continue;
                }

                descriptior.Instance = (IWidget)Activator.CreateInstance(mainType);
                descriptior.Assembly = assembly;

                Logger.LogDebug("Find widget assembly '{0}' ", assembly.FullName);

                descriptior.ComponentType = assembly.ExportedTypes.FirstOrDefault(t => !t.IsAbstract && t.BaseType != null && typeof(WidgetComponent).IsAssignableFrom(t));;
            }

            if (descriptior.Assembly == null)
            {
                return(null);
            }

            return(descriptior);



            //WidgetAssemblyLoadContext loadContext = new WidgetAssemblyLoadContext(directory.FullName);

            //foreach (var item in directory.GetFiles("*.dll"))
            //{
            //	//var assembly = loadContext.LoadFromAssemblyPath(item.FullName);
            //	var assembly = LoadAssembly(item.FullName);

            //	if (assembly == null)
            //		continue;

            //	Logger.LogDebug("Load assembly file '{0}' ", item.FullName);

            //	var mainType = assembly.ExportedTypes.FirstOrDefault(t => !t.IsAbstract && t.BaseType != null && (t.BaseType.Name == "WidgetBase" || t.BaseType.Name == "IWidget" || typeof(IWidget).IsAssignableFrom(t)));

            //	if (mainType == null)
            //	{
            //		continue;
            //	}

            //	descriptior.Instance = (IWidget)Activator.CreateInstance(mainType);
            //	descriptior.Assembly = assembly;

            //	Logger.LogDebug("Find widget assembly '{0}' ", assembly.FullName);

            //	var viewComponentType = assembly.ExportedTypes.FirstOrDefault(t => !t.IsAbstract && t.BaseType != null && t.BaseType.Name == "WidgetComponent");

            //	descriptior.ComponentType = viewComponentType;
            //}

            //if (descriptior.Assembly == null)
            //	return null;

            //return descriptior;
        }