.NET中如果需要在一段程序中调用另一个程序集的方法,就需要添加引用
如果不添加引用就需要用到反射了
下面是.NET中的一个自定义的类库代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Person
{
public class Person
{
private string _gtfs;
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public string Email
{
get;
set;
}
public Person()
{
Console.WriteLine("W无参数的构造函数");
}
public Person(string name, int age,string email)
{
this.Name = name;
this.Age = age;
this.Email = email;
}
public void PublicMethod()
{
Console.WriteLine("无参数的Public方法");
}
public void PublicMethod(string msg)
{
Console.WriteLine("有参数的Public方法"+msg);
}
private void PrivateMethod()
{
Console.WriteLine("无参数的Private方法");
}
public void SayHi()
{
Console.WriteLine("Hi~~~~");
}
}
public struct PublicStruct
{
}
struct PrivateStruct
{
}
internal class InternalMyClass
{
}
public interface IFly
{
}
internal class Fly : IFly
{
}
}
这是反射的集中常用的方法,这两个类不在同一个程序集中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace 不同程序集的反射
{
class Program
{
static void Main(string[] args)
{
//动态加载程序集
//根据程序集的绝对路径加载程序集
Assembly assembly = Assembly.LoadFile(@"F:\C#\反射\Person\bin\Debug\Person.dll");
//获取程序集中的所有的类型
Type[] types = assembly.GetTypes();
for (int i = 0; i < types.Length; i++)
{
Console.WriteLine(types[i].FullName);
}
Console.WriteLine("==================");
Console.ReadKey();
//获取public的类型
Type[] types1 = assembly.GetExportedTypes();
for (int i = 0; i < types1.Length; i++)
{
Console.WriteLine(types1[i].FullName);
}
Console.WriteLine("==================");
Console.ReadKey();
//获取指定的类型
Type typePerson = assembly.GetType("Person.Person"); //这里需要将命名空间也加上
//Console.WriteLine(typePerson);
//获取所有的非私有方法,注意获取不到构造方法
Console.WriteLine("--------所有的非私有方法---------");
MethodInfo[] methodInfos = typePerson.GetMethods();
for (int i = 0; i < methodInfos.Length; i++)
{
Console.WriteLine(methodInfos[i].Name);
}
Console.WriteLine("==================");
Console.ReadKey();
//获取所有的属性
Console.WriteLine("-----所有的属性------");
PropertyInfo[] propertyInfos = typePerson.GetProperties();
for (int i = 0; i < propertyInfos.Length; i++)
{
Console.WriteLine(propertyInfos[i].Name);
}
Console.WriteLine("==================");
Console.ReadKey();
//1.调用某个普通方法
//1.1要想调用方法,首先需要创建此类的对象
object objPerson = Activator.CreateInstance(typePerson);
//1.2获取方法
MethodInfo methodInfo = typePerson.GetMethod("SayHi");
Console.WriteLine("------调用了SayHi方法-------");
//1.3调用
methodInfo.Invoke(objPerson, null);//invoke的两个参数,第一个是需要调用的方法的类的对象,第二个是调用的方法的参数
Console.WriteLine("==================");
Console.ReadKey();
//2.调用无参构造方法
Console.WriteLine("------调用了无参数的构造方法-------");
ConstructorInfo constructorInfo = typePerson.GetConstructor(new Type[] { });
constructorInfo.Invoke(objPerson,null);
Console.WriteLine("==================");
Console.ReadKey();
//3.调用有参数的构造方法
Console.WriteLine("------调用了有参数的构造方法-------");
ConstructorInfo constructorInfo1 = typePerson.GetConstructor(new Type[] {typeof(string),typeof(int),typeof(string) });
//要注意一定要把构造函数绑定到对象上,invoke方法一定需要两个参数,第一个就是对象
constructorInfo1.Invoke(objPerson,new object[] { "liuyang", 23, "13012506912@163.com" });
Console.WriteLine("调用成功");
object nameValue = typePerson.GetProperty("Name").GetValue(objPerson);
object ageValue = typePerson.GetProperty("Age").GetValue(objPerson);
object emailValue = typePerson.GetProperty("Email").GetValue(objPerson);
Console.WriteLine(nameValue+" "+ageValue+" "+emailValue);
Console.WriteLine("==================");
Console.ReadKey();
//调用某个有重载的方法
//1.获取方法
MethodInfo methodInfo1 = typePerson.GetMethod("PublicMethod",new Type[] { });
//2.创建对象,上面已经有一个对象了,就不用再创建了
//3.调用方法
methodInfo1.Invoke(objPerson, null);
Console.WriteLine("----无参数的重载方法调用完毕-----");
//调用有参数的重载方法
methodInfo1 = typePerson.GetMethod("PublicMethod", new Type[] { typeof(string) });
methodInfo1.Invoke(objPerson, new object[] { "yangyang"});
Console.WriteLine("----有参数的重载方法调用完毕-----");
Console.WriteLine("结束ok》》》》》》》》》》");
Console.ReadKey();
}
}
}