简体   繁体   中英

C# Obfuscating .NET Executable breaks the use of Loading Assemblies

I am trying to write a plugin system for my .NET 4.7.2 Framework App.

I already wrote the code but there is a issue, When the app is obfuscated, the plugin system throws an error on launch.

There two images below should most likely explain the error, but I will also provide a copy and paste of the text.

Error Part 1

Error Part 2

Error Text:

   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)

The code segment for loading plugins:

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);
            }
        }
    }

KEEP IN MIND THIS ERROR DOES NOT HAPPEN WHEN THE APP IS NOT OBFUSCATED.

I have also tried different obfuscators such as ConfuserEx or this "free obfuscator"

Also I am positive that these errors happen due to the plugin system since when there are no plugins to load the obfuscated version has no problem doing so.

If you need me to clarify or explain something please let me know!

I would also take some recommendations to other free obfuscators as long as they protect my code well and work fine with this plugin loading setup (Btw the Constants.Foldername is just "Plugins")

I am still pretty new to stack overflow so if I messed something up I am really sorry!

I would avoid using reflection on any project that uses obfuscation. Reflection looks at the assembly in the same way that you are trying to stop people from looking at your assembly. In other words, if reflection works perfectly then you haven't protected your code.

Related reading: Should you obfuscate a commercial.Net application?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM