获取程序集下的类
1、Assembly.CreateInstance ()
using System;
using System.Reflection;
using Contoso.Libraries;
namespace Contoso.Libraries
{
//自定义的一个Person类
public class Person
{
private string _name;
public Person()
{ }
public Person(string name)
{
this._name = name;
}
public string Name
{ get { return this._name; }
set { this._name = value; } }
public override string ToString()
{
return this._name;
}
}
}
public class Example
{
public static void Main()
{
Assembly assem= Assembly.GetExecutingAssembly();
// Contoso.Libraries.Person 是类所在的路径
Person p = (Person) assem.CreateInstance("Contoso.Libraries.Person");
if (! (p == null)) {
p.Name = "John";
Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
p.GetType().Name, p);
}
else {
Console.WriteLine("Unable to instantiate a Person object.");
}
}
}
2、CreateInstance(String, Boolean)
参数
typeName String
要查找类型的名称。
ignoreCase Boolean
如果为 true,则忽略类型名的大小写;否则,为 false。
public class Example
{
public static void Main()
{
String fullName = "contoso.libraries.person";
Assembly assem = typeof(Person).Assembly;
Person p = (Person) assem.CreateInstance(fullName);
if (! (p == null)) {
p.Name = "John";
Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
p.GetType().Name, p);
}
else {
Console.WriteLine("Unable to instantiate a Person object " +
"with Assembly.CreateInstance(String)");
// Try case-insensitive type name comparison.
p = (Person) assem.CreateInstance(fullName, true);
if (! (p == null)) {
p.Name = "John";
Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
p.GetType().Name, p);
}
else {
Console.WriteLine("Unable to instantiate a {0} object.",
fullName);
}
}
}
}
3、CreateInstance(String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])
参数
typeName String
要查找类型的 FullName。
ignoreCase Boolean
如果为 true,则忽略类型名的大小写;否则,为 false。
bindingAttr BindingFlags
影响执行搜索的方式的位掩码。 此值是 BindingFlags中的位标志的组合。
binder
Binder
一个对象,它启用绑定、对自变量类型的强制、对成员的调用,以及通过反射对 MemberInfo 对象的检索。 如果 binder 为 null,则使用默认联编程序。
args Object[]
包含要传递给构造函数的自变量的数组。 此自变量数组在数量、顺序和类型方面必须与要调用的构造函数的参数匹配。 如果需要无参数构造函数,则 args 必须是空数组或 null。
culture CultureInfo
用于控制类型强制的 CultureInfo 的实例。 如果这是 null,则使用当前线程的 CultureInfo。 (例如,这对于将表示 1000 的字符串转换为 Double 值来说是必需的,因为不同的区域性以不同的方式表示 1000。)
activationAttribute Object[]
包含一个或多个可以参与激活的特性的数组。 通常,为包含单个 UrlAttribute 对象的数组,该对象指定激活远程对象所需的 URL。 此参数与客户端激活的对象相关。 客户端激活是一项传统技术,保留用于向后兼容,但不建议用于新的开发。 应改用 Windows Communication Foundation 来开发分布式应用程序。
public class Example
{
public static void Main()
{
String fullName = "contoso.libraries.person";
Assembly assem = typeof(Person).Assembly;
Person p = (Person) assem.CreateInstance(fullName,true, BindingFlags.Default, null,
new object[] {
},
null, null);
if (! (p == null)) {
p.Name = "John";
Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
p.GetType().Name, p);
}
else {
Console.WriteLine("Unable to instantiate a Person object " +
"with Assembly.CreateInstance(String)");
// Try case-insensitive type name comparison.
p = (Person) assem.CreateInstance(fullName, true);
if (! (p == null)) {
p.Name = "John";
Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
p.GetType().Name, p);
}
else {
Console.WriteLine("Unable to instantiate a {0} object.",
fullName);
}
}
}
}
4、Activator.CreateInstance()
5、获取当前类下的方法
///获取方法
MethodInfo method_info = this.GetType().GetMethod("FunctionName",
BindingFlags.NonPublic | BindingFlags.Instance);
//执行方法 object[]数组内为参数
method_info?.Invoke(this, new object[] { });