c# Reflection obtains all classes that implement interfaces to solve the problem of failure to load files or assemblies

Type[] types = AppDomain.CurrentDomain.GetAssemblies()
                        .SelectMany(a => a.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IConfigConvert))))
                        .ToArray();
            Assembly assembly = Assembly.GetExecutingAssembly(); // 获取当前程序集
            foreach (Type type in types)
            {
                string name = type.FullName;
                IConfigConvert iconvert = (IConfigConvert)assembly.CreateInstance(name);
            }

EntityFramework is used in the class that implements the interface, and an error is reported after reflection.

Could not load file or assembly 'entityframework, version=4.4.0.0

A "Failed to load file or assembly" error was reported.

The EF version used in the project is 5.0, so it needs to be specified in config

<configuration>  
<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Guess you like

Origin blog.csdn.net/wangyue4/article/details/127637683