


下面小编就为大家带来一篇c# 动态加载dll文件,并实现调用其中的方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
以下是测试代码:
新建一个classlibrary,包含两个类class1和class2,这两个类中分别有一个方法,都是返回一个字符串,代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace mydll { public class Class1 { public Class1() { } public string sayhello() { return "hello,word!"; } } public class Class2 { public Class2() { } public string saybeautiful() { return "beautiful,very good!"; } } }
在编译完成后会生成一个mydll.dll动态链接库,然后新建一个winform项目(其他也可以,调试用):
private void button1_Click(object sender, EventArgs e) { string path = @"D:\123\mydll\mydll\bin\Debug\mydll.dll"; //Byte[] byte1 = System.IO.File.ReadAllBytes(path);//也是可以的 //Assembly assem = Assembly.Load(byte1); Assembly assem = Assembly.LoadFile(path); //string t_class = "mydll.Class1";//理论上已经加载了dll文件,可以通过命名空间加上类名获取类的类型,这里应该修改为如下: //string t_class = "mydll.Class1,mydll";//如果你想要得到的是被本工程内部的类,可以“命名空间.父类……类名”;如果是外部的,需要在后面加上“,链接库名”; //再次感谢thy38的帮助。 //Type ty = Type.GetType(t_class);//这儿在调试的时候ty=null,一直不理解,望有高人可以解惑 Type[] tys = assem.GetTypes();//只好得到所有的类型名,然后遍历,通过类型名字来区别了 foreach (Type ty in tys)//huoquleiming { if (ty.Name == "Class1") { ConstructorInfo magicConstructor = ty.GetConstructor(Type.EmptyTypes);//获取不带参数的构造函数 object magicClassObject = magicConstructor.Invoke(new object[] { });//这里是获取一个类似于类的实例的东东 //object magicClassObject = Activator.CreateInstance(t);//获取无参数的构造实例还可以通过这样 MethodInfo mi = ty.GetMethod("sayhello"); object aa=mi.Invoke(magicClassObject, null); MessageBox.Show(aa.ToString());//这儿是执行类class1的sayhello方法 } if (ty.Name == "Class2") { ConstructorInfo magicConstructor = ty.GetConstructor(Type.EmptyTypes); //获取不带参数的构造函数,如果有构造函数且没有不带参数的构造函数时,这儿就不能这样子啦 object magicClassObject = magicConstructor.Invoke(new object[] { }); MethodInfo mi = ty.GetMethod("saybeautiful"); object aa = mi.Invoke(magicClassObject, null);//方法有参数时,需要把null替换为参数的集合 MessageBox.Show(aa.ToString()); } } //AppDomain pluginDomain = (pluginInstanceContainer[key] as PluginEntity).PluginDomain; //if (pluginDomain != null) //{ // AppDomain.Unload(pluginDomain); // } }
以上就是c# 动态加载dll文件,并实现调用其中的方法(推荐)分析的内容,更多相关内容请关注PHP中文网(www.php.cn)!

The latest developments and best practices in C#.NET development include: 1. Asynchronous programming improves application responsiveness, and simplifies non-blocking code using async and await keywords; 2. LINQ provides powerful query functions, efficiently manipulating data through delayed execution and expression trees; 3. Performance optimization suggestions include using asynchronous programming, optimizing LINQ queries, rationally managing memory, improving code readability and maintenance, and writing unit tests.

How to build applications using .NET? Building applications using .NET can be achieved through the following steps: 1) Understand the basics of .NET, including C# language and cross-platform development support; 2) Learn core concepts such as components and working principles of the .NET ecosystem; 3) Master basic and advanced usage, from simple console applications to complex WebAPIs and database operations; 4) Be familiar with common errors and debugging techniques, such as configuration and database connection issues; 5) Application performance optimization and best practices, such as asynchronous programming and caching.

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

C# and .NET are suitable for web, desktop and mobile development. 1) In web development, ASP.NETCore supports cross-platform development. 2) Desktop development uses WPF and WinForms, which are suitable for different needs. 3) Mobile development realizes cross-platform applications through Xamarin.

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download
The most popular open source editor
