这篇“C#怎么操作泛型与属性字段”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“C#怎么操作泛型与属性字段”文章吧。
查找DLL文件,
通过Reflection反射类库里的各种方法来操作dll文件
加载DLL文件
Assembly assembly1 = Assembly.Load("SqlServerDB");//方式一:这个DLL文件要在启动项目下
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())//查找所有的类型,就是有多少个类
{
Console.WriteLine(item.Name);
}
获取构造函数
Type type = assembly4.GetType("SqlServerDB.ReflectionTest");//在ReflectionTest类中调用
foreach (var ctor in type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
{
Console.WriteLine($"构造方法:{ctor.Name}");
foreach (var param in ctor.GetParameters())
{
Console.WriteLine($"构造方法的参数:{param.ParameterType}");
}
}
//【3】实例化
//ReflectionTest reflectionTest = new ReflectionTest();//这种实例化是知道具体类型--静态
//object objTest = Activator.CreateInstance(type);//动态实例化--调用我们的构造方法
object objTest1 = Activator.CreateInstance(type, new object[] { "string" });//动态实例化--调用我们的有参数构造方法
//调用私有构造函数
//ReflectionTest reflectionTest = new ReflectionTest(); //普通调用
object objTest2 = Activator.CreateInstance(type, true);
调用非构造方法
object objTest2 = Activator.CreateInstance(type, true);
//调用普通方法
ReflectionTest reflectionTest = objTest2 as ReflectionTest;//as转换的好处,它不报错,类型不对的话就返回null
reflectionTest.Show1();
//调用私有方法
var method = type.GetMethod("Show2", BindingFlags.Instance | BindingFlags.NonPublic);
method.Invoke(objTest2, new object[] { });
调用泛型方法
//泛型无参数
var method3 = type.GetMethod("Show3");//查找指定方法
var genericMethod = method3.MakeGenericMethod(new Type[] { typeof(int) });//指定泛型参数类型T
genericMethod.Invoke(objTest2, new object[] { });
//泛型有参数
var method4 = type.GetMethod("Show4");//查找指定方法
var genericMethod4 = method4.MakeGenericMethod(new Type[] { typeof(string) });//指定泛型参数类型T
genericMethod4.Invoke(objTest2, new object[] { 123, "泛型string参数" });
反射测试类
位于SqlServerDB.dll中的ReflectionTest.cs文件中
/// <summary>
/// 反射测试类
/// </summary>
public class ReflectionTest
{
//私有构造函数
private ReflectionTest()
{
Console.WriteLine("这是私有无参数构造方法");
}
//普通构造函数
//public ReflectionTest()
//{
// Console.WriteLine("这是无参数构造方法");
//}
public ReflectionTest(string name)
{
Console.WriteLine($"这是有参数构造方法+参数值是:{name}");
}
public void Show1()
{
Console.WriteLine("调用普通方法", this.GetType());
}
private void Show2()
{
Console.WriteLine("调用私有方法",this.GetType());
}
public void Show3<T>()
{
Console.WriteLine("调用无参数泛型方法", this.GetType());
}
public void Show4<T>(int id,string name)
{
Console.WriteLine($"调用有参数泛型方法,参数是{id},{name}", this.GetType());
}
}
加载DLL文件
Assembly assembly = Assembly.LoadFrom(@"SqlServerDB.dll");
获取指定类型
Type type = assembly.GetType("SqlServerDB.GenericClass`2").MakeGenericType(typeof(int), typeof(string));//一定给定具体类型参数
调用泛型方法
object objTest2 = Activator.CreateInstance(type);
var method = type.GetMethod("GenericMethod").MakeGenericMethod(typeof(int));
method.Invoke(objTest2, new object[] { });
反射测试类
位于SqlServerDB.dll中的GenericClass.cs文件中
public class GenericClass<T,W>
{
public void GenericMethod<TType>()
{
Console.WriteLine("泛型类调用+泛型方法");
}
}
加载DLL文件
Assembly assembly2 = Assembly.LoadFrom("SqlServerDB.dll");
获取指定类型
Type type2 = assembly2.GetType("SqlServerDB.PropertyClass");
调用泛型方法
object obj = Activator.CreateInstance(type2);
foreach (var property in type2.GetProperties())
{
Console.WriteLine(property.Name);
//给属性设置值
if (property.Name.Equals("Id"))
{
property.SetValue(obj, 1);
}
else if (property.Name.Equals("Name"))
{
property.SetValue(obj, "学习编程");
}
else if (property.Name.Equals("Phone"))
{
property.SetValue(obj, "123459789");
}
//获取属性值
Console.WriteLine(property.GetValue(obj));
}
反射测试类
位于SqlServerDB.dll中的PropertyClass.cs文件中
public class PropertyClass
{
public int Id { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
}
以上就是关于“C#怎么操作泛型与属性字段”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。