C#反射調(diào)用dll文件中的方法操作泛型與屬性字段
一、使用方法
查找DLL文件,
通過Reflection反射類庫(kù)里的各種方法來操作dll文件
二、步驟
加載DLL文件
Assembly assembly1 = Assembly.Load("SqlServerDB");//方式一:這個(gè)DLL文件要在啟動(dòng)項(xiàng)目下
string filePath = Environment.CurrentDirectory + "";
Assembly assembly2 = Assembly.LoadFile(filePath + @"\SqlServerDB.dll");//方式二:完整路徑
Assembly assembly3 = Assembly.LoadFrom(filePath + @"\SqlServerDB.dll");//方式三:完整路徑
Assembly assembly4 = Assembly.LoadFrom(@"SqlServerDB.dll");//方式三:完整路徑獲取指定類型
foreach (var item in assembly4.GetTypes())//查找所有的類型,就是有多少個(gè)類
{
Console.WriteLine(item.Name);
}獲取構(gòu)造函數(shù)
Type type = assembly4.GetType("SqlServerDB.ReflectionTest");//在ReflectionTest類中調(diào)用
foreach (var ctor in type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
{
Console.WriteLine($"構(gòu)造方法:{ctor.Name}");
foreach (var param in ctor.GetParameters())
{
Console.WriteLine($"構(gòu)造方法的參數(shù):{param.ParameterType}");
}
}
//【3】實(shí)例化
//ReflectionTest reflectionTest = new ReflectionTest();//這種實(shí)例化是知道具體類型--靜態(tài)
//object objTest = Activator.CreateInstance(type);//動(dòng)態(tài)實(shí)例化--調(diào)用我們的構(gòu)造方法
object objTest1 = Activator.CreateInstance(type, new object[] { "string" });//動(dòng)態(tài)實(shí)例化--調(diào)用我們的有參數(shù)構(gòu)造方法
//調(diào)用私有構(gòu)造函數(shù)
//ReflectionTest reflectionTest = new ReflectionTest(); //普通調(diào)用
object objTest2 = Activator.CreateInstance(type, true);調(diào)用非構(gòu)造方法
object objTest2 = Activator.CreateInstance(type, true);
//調(diào)用普通方法
ReflectionTest reflectionTest = objTest2 as ReflectionTest;//as轉(zhuǎn)換的好處,它不報(bào)錯(cuò),類型不對(duì)的話就返回null
reflectionTest.Show1();
//調(diào)用私有方法
var method = type.GetMethod("Show2", BindingFlags.Instance | BindingFlags.NonPublic);
method.Invoke(objTest2, new object[] { });調(diào)用泛型方法
//泛型無參數(shù)
var method3 = type.GetMethod("Show3");//查找指定方法
var genericMethod = method3.MakeGenericMethod(new Type[] { typeof(int) });//指定泛型參數(shù)類型T
genericMethod.Invoke(objTest2, new object[] { });
//泛型有參數(shù)
var method4 = type.GetMethod("Show4");//查找指定方法
var genericMethod4 = method4.MakeGenericMethod(new Type[] { typeof(string) });//指定泛型參數(shù)類型T
genericMethod4.Invoke(objTest2, new object[] { 123, "泛型string參數(shù)" });反射測(cè)試類
位于SqlServerDB.dll中的ReflectionTest.cs文件中
/// <summary>
/// 反射測(cè)試類
/// </summary>
public class ReflectionTest
{
//私有構(gòu)造函數(shù)
private ReflectionTest()
{
Console.WriteLine("這是私有無參數(shù)構(gòu)造方法");
}
//普通構(gòu)造函數(shù)
//public ReflectionTest()
//{
// Console.WriteLine("這是無參數(shù)構(gòu)造方法");
//}
public ReflectionTest(string name)
{
Console.WriteLine($"這是有參數(shù)構(gòu)造方法+參數(shù)值是:{name}");
}
public void Show1()
{
Console.WriteLine("調(diào)用普通方法", this.GetType());
}
private void Show2()
{
Console.WriteLine("調(diào)用私有方法",this.GetType());
}
public void Show3<T>()
{
Console.WriteLine("調(diào)用無參數(shù)泛型方法", this.GetType());
}
public void Show4<T>(int id,string name)
{
Console.WriteLine($"調(diào)用有參數(shù)泛型方法,參數(shù)是{id},{name}", this.GetType());
}
}操作泛型類和泛型方法
加載DLL文件
Assembly assembly = Assembly.LoadFrom(@"SqlServerDB.dll");
獲取指定類型
Type type = assembly.GetType("SqlServerDB.GenericClass`2").MakeGenericType(typeof(int), typeof(string));//一定給定具體類型參數(shù)調(diào)用泛型方法
object objTest2 = Activator.CreateInstance(type);
var method = type.GetMethod("GenericMethod").MakeGenericMethod(typeof(int));
method.Invoke(objTest2, new object[] { });反射測(cè)試類
位于SqlServerDB.dll中的GenericClass.cs文件中
public class GenericClass<T,W>
{
public void GenericMethod<TType>()
{
Console.WriteLine("泛型類調(diào)用+泛型方法");
}
}操作類屬性字段
加載DLL文件
Assembly assembly2 = Assembly.LoadFrom("SqlServerDB.dll");獲取指定類型
Type type2 = assembly2.GetType("SqlServerDB.PropertyClass");調(diào)用泛型方法
object obj = Activator.CreateInstance(type2);
foreach (var property in type2.GetProperties())
{
Console.WriteLine(property.Name);
//給屬性設(shè)置值
if (property.Name.Equals("Id"))
{
property.SetValue(obj, 1);
}
else if (property.Name.Equals("Name"))
{
property.SetValue(obj, "學(xué)習(xí)編程");
}
else if (property.Name.Equals("Phone"))
{
property.SetValue(obj, "123459789");
}
//獲取屬性值
Console.WriteLine(property.GetValue(obj));
}
反射測(cè)試類
位于SqlServerDB.dll中的PropertyClass.cs文件中
public class PropertyClass
{
public int Id { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)協(xié)同過濾算法的實(shí)例代碼
這篇文章介紹了C#實(shí)現(xiàn)協(xié)同過濾算法的實(shí)例代碼,有需要的朋友可以參考一下2013-07-07
c# DevExpress gridcontrol日期行的顯示格式設(shè)置
這篇文章主要介紹了c# DevExpress gridcontrol日期行的顯示格式設(shè)置,需要的朋友可以參考下2017-02-02
C#實(shí)現(xiàn)最簡(jiǎn)單的文本加密方法
這篇文章主要介紹了C#實(shí)現(xiàn)最簡(jiǎn)單的文本加密方法,可實(shí)現(xiàn)簡(jiǎn)單的文本加密功能,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12
c# 使用谷歌身份驗(yàn)證GoogleAuthenticator的示例
這篇文章主要介紹了c# 使用谷歌身份驗(yàn)證GoogleAuthenticator的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01
C#從文件或標(biāo)準(zhǔn)輸入設(shè)備讀取指定行的方法
這篇文章主要介紹了C#從文件或標(biāo)準(zhǔn)輸入設(shè)備讀取指定行的方法,涉及C#文件及IO操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
C#實(shí)現(xiàn)功能強(qiáng)大的中國(guó)農(nóng)歷日歷操作類
這篇文章主要介紹了C#實(shí)現(xiàn)功能強(qiáng)大的中國(guó)農(nóng)歷日歷操作類,實(shí)例分析了C#操作時(shí)間及字符串的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03
C#中幾個(gè)未知的Visual Studio編碼技巧分享
用了多年的Visual Studio,今天才發(fā)現(xiàn)這個(gè)編碼技巧,真是慚愧,分享出來,算是拋磚引玉吧,需要的朋友可以參考下2012-11-11
C# 使用WPF 用MediaElement控件實(shí)現(xiàn)視頻循環(huán)播放
在WPF里用MediaElement控件,實(shí)現(xiàn)一個(gè)循環(huán)播放單一視頻的程序,同時(shí)可以控制視頻的播放、暫停、停止。這篇文章給大家介紹了C# 使用WPF 用MediaElement控件實(shí)現(xiàn)視頻循環(huán)播放,需要的朋友參考下吧2018-04-04

