C# 利用反射动态调用类成员
使用反射動態調用類成員,需要Type類的一個方法:InvokeMember。對該方法的聲明如下:
public object InvokeMember(
string name,
BindingFlags invokeAttr,
Binder binder,
object target,
object[] args
);
參數
name
String,它包含要調用的構造函數、方法、屬性或字段成員的名稱。
- 或 -
空字符串 (""),表示調用默認成員。
invokeAttr
一 個位屏蔽,由一個或多個指定搜索執行方式的 BindingFlags 組成。訪問可以是 BindingFlags 之一,如 Public、NonPublic、Private、InvokeMethod 和 GetField 等。不需要指定查找類型。如果省略查找類型,則將應用 BindingFlags.Public | BindingFlags.Instance。
binder
一個 Binder 對象,該對象定義一組屬性并啟用綁定,而綁定可能涉及選擇重載方法、強制參數類型和通過反射調用成員。
- 或 -
若為空引用(Visual Basic 中為 Nothing),則使用 DefaultBinder。
target
要在其上調用指定成員的 Object。
args
包含傳遞給要調用的成員的參數的數組。
返回值
表示被調用成員的返回值的 Object。
備注:
下列 BindingFlags 篩選標志可用于定義包含在搜索中的成員:
為了獲取返回值,必須指定 BindingFlags.Instance 或 BindingFlags.Static。
指定 BindingFlags.Public 可在搜索中包含公共成員。
指定 BindingFlags.NonPublic 可在搜索中包含非公共成員(即私有成員和受保護的成員)。
指定 BindingFlags.FlattenHierarchy 可包含層次結構上的靜態成員。
下列 BindingFlags 修飾符標志可用于更改搜索的執行方式:
BindingFlags.IgnoreCase,表示忽略 name 的大小寫。
BindingFlags.DeclaredOnly,僅搜索 Type 上聲明的成員,而不搜索被簡單繼承的成員。
可以使用下列 BindingFlags 調用標志表示要對成員采取的操作:
CreateInstance,表示調用構造函數。忽略 name。對其他調用標志無效。
InvokeMethod,表示調用方法,而不調用構造函數或類型初始值設定項。對 SetField 或 SetProperty 無效。
GetField,表示獲取字段值。對 SetField 無效。
SetField,表示設置字段值。對 GetField 無效。
GetProperty,表示獲取屬性。對 SetProperty 無效。
SetProperty 表示設置屬性。對 GetProperty 無效。
代碼示例:
class Program
{
static void Main(string[] args)
{
object obj;
NewsEngine instance = (NewsEngine)System.Reflection.Assembly.Load("ReflectionData").CreateInstance("ReflectionData.NewsEngine");
System.Diagnostics.Debug.WriteLine(instance.GetItems());
//
//private string file = @"DataAccess.dll";
ReflectionEngine r = new ReflectionEngine(@"ReflectionData.dll");
r.DisplayModules();
r.DisplayTypes();
r.DisplayMethods();
r.InvokeStaticMethod();
r.InvokeMethod();
Console.Read();
}
}
public class ReflectionEngine
{
private string file;
public ReflectionEngine(string file)
{
this.file = file;
}
//顯示所有模塊名
public void DisplayModules()
{
Console.WriteLine("display modules ...");
Assembly assembly = Assembly.LoadFrom(file);
Module[] modules = assembly.GetModules();
foreach (Module module in modules)
{
Console.WriteLine("module name:" + module.Name);
}
}
//顯示所有類型名
public void DisplayTypes()
{
Console.WriteLine("display types ...");
Assembly assembly = Assembly.LoadFrom(file);
Type[] types = assembly.GetTypes();
foreach (Type type in types)
{
Console.WriteLine("type name:" + type.FullName);
}
}
//先是一個類型中的所有方法名
public void DisplayMethods()
{
Console.WriteLine("display methods in TestReflection Type ...");
Assembly assembly = Assembly.LoadFrom(file);
Type type = assembly.GetType("ReflectionData.NewsEngine");
MethodInfo[] methods = type.GetMethods();
foreach (MethodInfo method in methods)
{
Console.WriteLine("method name:" + method.Name);
}
}
//調用一個類中的靜態方法
public void InvokeStaticMethod()
{
Console.WriteLine("Invoke static method ...");
Assembly assembly = Assembly.LoadFrom(file);
Type type = assembly.GetType("ReflectionData.NewsEngine");
type.InvokeMember("GetName", BindingFlags.InvokeMethod, null, null, new object[] { });
}
//調用一個類中的非靜態方法
public void InvokeMethod()
{
Console.WriteLine("Invoke Method ...");
Assembly assembly = Assembly.LoadFrom(file);
Type type = assembly.GetType("ReflectionData.NewsEngine");
Object obj = Activator.CreateInstance(type);
ReflectionData.NewsEngine tc = (ReflectionData.NewsEngine)obj;
Console.WriteLine(type.InvokeMember("GetItems", BindingFlags.InvokeMethod, null, tc, new object[] { }));
Console.WriteLine(type.InvokeMember("GetItemById", BindingFlags.InvokeMethod, null, tc, new object[] { 1 }));
}
}
====================================================================================
測試代碼
using System;
using System.Collections.Generic;
using System.Text;
namespace ReflectionData
{
public interface INewsEngine
{
string Name { get;}
string GetItems();
string GetItemById(int Id);
}
public class NewsEngine :INewsEngine
{
private string m_Name = "Common News Engine";
public string Name
{
get { return m_Name; }
}
public string GetItems()
{
return "news's method - GetItems().";
}
public string GetItemById(int Id)
{
return "news's method - GetItemById(" + Id + ").";
}
public static string GetName()
{
return "news's static method - GetName().";
}
}
}
轉載于:https://www.cnblogs.com/yoshow/archive/2007/06/19/788628.html
總結
以上是生活随笔為你收集整理的C# 利用反射动态调用类成员的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大连公交客运集团认真安排做好2007年防
- 下一篇: asp.net学习历程。