C# dynamically load DLL assembly and use reflection to create examples-a brief note

tags: C# related  Asp.net WebForm  Asp.net MVC  c#  Assembly  reflection  

Assembly dynamic loading assembly:

There are two situations:

1. The assembly that needs to be loaded has been referenced in the program, you can directly search from the current program domain:

Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.GetName().Name.Contains("theAssemblyName"));

 

2. If the assembly to be loaded is not loaded, use the assembly name to load:

Method one, some necessary information of the specific assembly is required, as follows:

Assembly assembly = Assembly.Load(@"theAssemblyName, Version=55.0.0.0, Culture=neutral, PublicKeyToken=ef0f902af9dee505");

Description: theAssemblyName: the name of the assembly, Version: the version of the assembly, Culture: generally default to neutral, PublicKeyToken: the unique identification ID of the assembly, no; the above information can be in the AssemblyInfo.cs file in the assembly project Obtain, as shown below:

Method two, load through the assembly file path, only load the assembly file, and not load other assemblies that the assembly depends on, as follows:

Assembly assembly = Assembly.LoadFile (specific path + "DynamicReferenceDLL.dll")

 

Method three, load through the assembly file path, and load the assembly that this assembly depends on at the same time, as follows:

Assembly assembly = Assembly.LoadFrom (specific path + "DynamicReferenceDLL.dll")

 

 

 

Use reflection to create an instance:

  //Get the specified class type in the assembly, the parameter is: namespace. Type name
 var type = assembly.GetType("DynamicReferenceDLL.MyMath");

   //Create an instance, method one
 //var obj = Activator.CreateInstance(type); 

   //Create an instance, method two, the parameters are: namespace. type name    
 var obj = assembly.CreateInstance("DynamicReferenceDLL.MyMath");

   //Get the method in the instance
 var method = type.GetMethod("Sum");

   //Call the instance method
 object result=method.Invoke(obj, new object[] { 1, 2 });

 Console.WriteLine(result);

How to get the file of the assemblyPublicKeyToken,Culture,Version:

Assembly assembly = Assembly.LoadFile(@"C:\Program Files\ReferencedAssemblies\theAssemblyName.dll");
string name = assembly.FullName;

 

Intelligent Recommendation

C# assembly reflection dll file content

C# assembly reflection dll file content Writing and generating DLL files Reference DLL file reflection Writing and generating DLL files Link:How to generate DLL files in C# (Virsual Studio2019). It sh...

C # reflection to dynamically create a form

now wrote a control, I want to load the form according to the form's settings, the use of reflection, to add information to the database form, do not know what form the place, nor to know more about t...

C # dynamically load third-party DLL

C#Dynamic loading third-party DLL When we need to load a third-party non-host DLL, it usually uses directly.DllImportThe way, the code is as follows:   Calling mode of the above picture, defaultG...

Note and reflection - create objects dynamically through reflex

Entity class Create an object by reflecting dynamic...

More Recommendation

C # dynamically loading DLL invoked by reflection parameter method, the form

.net Central Standing Committee to use dynamically loaded DLL, and the DLL may include a variety of parameters, methods, forms, how to call dynamically load these parameters, methods, forms of it? In ...

C ++ calls C# DLL fails to load file or assembly

1. Preparation before calling: (1) Normal compilation of C# class library project; (2) C ++ project settings "support (/clr)"; (3) Add C# DLL paths and declare the name space of the C# DLL p...

Use Win32 assembly to develop a dll and call it in C#

Use RadASM to create a new Dll Project; Next step, default; dll contains Def files; Complete the project construction; all default; After completion, the project structure is as follows; asm code; def...

[VS2010 Study Notes] [Programming Examples] (Create and use DLL with C++ in Visual Studio)

What is a DLL (Dynamic Link Library)? DLL is a package that can be used by multiple programs at the same timeCodewithData library. For example: In the Windows operating system, Comdlg32 DLL executes c...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top