6

我想在 Flutter Dart 中实现一个插件架构。流程如下: 1. 用户下载应用。2. 用户从我们的网站加载插件。3. 应用程序查看插件是否实现了接口。4. 如果实现了接口,则将信息和小部件从插件加载到应用程序。

我已经在 C# 中使用运行时编译的 DLL 加载实现了相同的过程,但无法为 Flutter 找到它。

我查看了互联网上以前的一些问题和资源,我发现最接近的是https://pub.dev/packages/plugins但该插件在 Dart 2 中不受支持且已弃用

这是我在 C# 中实现的代码。

            int i = 0;

            if (Directory.Exists("Plugins"))
            {
                dllFileNames = Directory.GetFiles("Plugins", "*.dll");

                ICollection<Assembly> assemblies = new List<Assembly>(dllFileNames.Length);
                foreach (string dllFile in dllFileNames)
                {
                    AssemblyName an = AssemblyName.GetAssemblyName(dllFile);
                    Assembly assembly = Assembly.Load(an);
                    assemblies.Add(assembly);
                }

                Type pluginType = typeof(IPlugin);
                List<Type> pluginTypes = new List<Type>();
                foreach (Assembly assembly in assemblies)
                {
                    if (assembly != null)
                    {
                        Type[] types = assembly.GetTypes();
                        foreach (Type type in types)
                        {
                            if (type.IsInterface || type.IsAbstract)
                            {
                                continue;
                            }
                            else if (pluginType.IsAssignableFrom(type))
                            {
                                pluginTypes.Add(type);
                            }
                        }
                    }

                    i++;
                }

                ICollection<IPlugin> plugins = new List<IPlugin>(pluginTypes.Count);
                foreach (Type type in pluginTypes)
                {
                    IPlugin plugin = (IPlugin)Activator.CreateInstance(type);
                    plugin.Initiate();
                    plugins.Add(plugin);
                }

                return plugins;
            }

            return null;
4

1 回答 1

0

这可能是不可能的。

准备您的应用程序以上传到商店的 AOT 编译的一部分是摇树,删除当前构建不需要的所有内容。因此,您的插件需要调用的任何东西都消失了。

于 2019-10-17T22:16:56.370 回答