C# 混淆 .NET 可执行文件破坏了加载程序集的使用

问题描述

我正在尝试为我的 .NET 4.7.2 Framework 应用编写插件系统。

我已经写了代码但是有一个问题,当应用程序被混淆时,插件系统在启动时抛出错误

下面两张图片很可能可以解释错误,但我也会提供文本的复制和粘贴。

Error Part 1

Error Part 2

错误文本:

   System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeModule.GetTypes()
   at System.Reflection.Assembly.GetTypes()
   at @ӛ.<>c.<Loadplugins>b__4_0(Assembly a)
   at System.Linq.Enumerable.<SelectManyIterator>d__17`2.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at @ӛ.@ӗ()
   at @Ӗ..ctor()
   at @Ӕ.<checkForPrevIoUsLogin>d__2.MoveNext()
   at System.Runtime.CompilerServices.AsyncmethodBuilderCore.<>c.<ThrowAsync>b__6_0(Object state)

加载插件代码段:

public class PluginLoader
    {
        public static List<IPlugin> Plugins { get; set; }

        public void Loadplugins()
        {
            Plugins = new List<IPlugin>();

            //Load the DLLs from the Plugins directory
            if (Directory.Exists(Constants.folderName))
            {
                string[] files = Directory.GetFiles(Constants.folderName);
                foreach (string file in files)
                {
                    if (file.EndsWith(".dll"))
                    {
                        Constants.raidTool.log("Loading Plugin File: " + Path.GetFullPath(file));
                        Assembly.LoadFile(Path.GetFullPath(file));
                    }
                }
            }

            Type interfaceType = typeof(IPlugin);
            //Fetch all types that implement the interface IPlugin and are a class
            Type[] types = AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(a => a.GetTypes())
                .Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass)
                .ToArray();
            foreach (Type type in types)
            {
                //Create a new instance of all found types
                Plugins.Add((IPlugin)Activator.CreateInstance(type));
            }
            
            foreach (IPlugin plugin in Plugins)
            {
                plugin.Start();
            }
        }
        public void ShutdownPlugins()
        {
            foreach (IPlugin plugin in Plugins)
            {
                plugin.Shutdown();
            }
        }
        public void WebRequestPlugin(HttpWebRequest request)
        {
            foreach (IPlugin plugin in Plugins)
            {
                plugin.OnNetworkRequest(request);
            }
        }
    }

请记住此错误不会在应用程序未混淆时发生。

我还尝试了不同的混淆器,例如 ConfuserEx 或 this "free obfuscator"

我也肯定这些错误是由插件系统引起的,因为当没有插件加载混淆版本时,这样做没有问题。

如果您需要我澄清或解释某些事情,请告诉我!

我还会向其他免费的混淆器推荐一些长期,因为它们可以很好地保护我的代码并且可以很好地使用此插件加载设置(顺便说一句,Constants.Foldername 只是“插件”) >

我对堆栈溢出还是很陌生,所以如果我搞砸了,我真的很抱歉!

解决方法

我会避免在任何使用混淆的项目上使用反射。反射查看组件的方式与您试图阻止人们查看您的组件的方式相同。换句话说,如果反射工作得很好,那么你就没有保护你的代码。

相关阅读: Should you obfuscate a commercial .Net application?

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...