使用反射-動態建立物件及呼叫物件方法
namespace ConsoleApplication1
{
public class ReflectionSample
{
private string firstName = string.Empty;
private string lastName = string.Empty;
public ReflectionSample() { }
public ReflectionSample(string firstName, string lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public string SayHello()
{
return string.Format("Hello {0} {1}", this.firstName, this.lastName);
}
public static string StaticHello()
{
return string.Format("Hello, I am a static method");
}
}
{
public class ReflectionSample
{
private string firstName = string.Empty;
private string lastName = string.Empty;
public ReflectionSample() { }
public ReflectionSample(string firstName, string lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public string SayHello()
{
return string.Format("Hello {0} {1}", this.firstName, this.lastName);
}
public static string StaticHello()
{
return string.Format("Hello, I am a static method");
}
}
}
建立物件,方法一:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->1Assembly asm = Assembly.GetExecutingAssembly();
2Object obj = asm.CreateInstance("ConsoleApplication1.ReflectionSample", true);
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->1Assembly asm = Assembly.GetExecutingAssembly();
2Object obj = asm.CreateInstance("ConsoleApplication1.ReflectionSample", true);
方法二:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->1ObjectHandle handler = Activator.CreateInstance(null, "ConsoleApplication1.ReflectionSample");//第一個參數列示程式集名稱,為null表示當前程式集
2Object obj = handler.Unwrap();
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->1ObjectHandle handler = Activator.CreateInstance(null, "ConsoleApplication1.ReflectionSample");//第一個參數列示程式集名稱,為null表示當前程式集
2Object obj = handler.Unwrap();
帶引數建構函式的情況:
建立引數和修改createintance方法:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1//建立引數:
2Object[] paras = new Object[2];
3paras[0] = "Jimmy";
4paras[1] = "Zhang";
5
6//建立物件:
7Assembly asm = Assembly.GetExecutingAssembly();
8Object obj = asm.CreateInstance("ConsoleApplication1.ReflectionSample", true, BindingFlags.Default, null, paras, null, null);
9
10//The second way
11//ObjectHandle handler = Activator.CreateInstance(null, "ConsoleApplication1.ReflectionSample", true, BindingFlags.Default, null, paras, null, null, null);
12//Object obj = handler.Unwrap();
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1//建立引數:
2Object[] paras = new Object[2];
3paras[0] = "Jimmy";
4paras[1] = "Zhang";
5
6//建立物件:
7Assembly asm = Assembly.GetExecutingAssembly();
8Object obj = asm.CreateInstance("ConsoleApplication1.ReflectionSample", true, BindingFlags.Default, null, paras, null, null);
9
10//The second way
11//ObjectHandle handler = Activator.CreateInstance(null, "ConsoleApplication1.ReflectionSample", true, BindingFlags.Default, null, paras, null, null, null);
12//Object obj = handler.Unwrap();
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-465894/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- JS_建立物件+呼叫物件方法JS物件
- 00024.06 反射:反射的應用2:動態建立物件反射物件
- java反射構建物件和方法的反射呼叫Java反射物件
- Java中物件的動態建立及其反射機制Java物件反射
- 使用反射建立窗體物件時,物件為NULL反射物件Null
- 物件呼叫動態變數物件變數
- Objective-C使用靜態方法建立字串物件Object字串物件
- PHP通過反射實現物件的成員方法呼叫PHP反射物件
- Spring 解耦反射建立物件Spring解耦反射物件
- 通過字串動態建立類物件字串物件
- 波哥學JAVA,定義類 宣告屬性 建立物件 例項化物件 物件呼叫屬性或者方法Java物件
- js建立物件的方法JS物件
- Python中動態類和動態方法的建立與呼叫Python
- 全面解析RayFire的動態物件與靜態物件物件
- 對於物件的動態建立例項問題物件
- C++中物件的動態建立與釋放C++物件
- 程式碼中使用DataView物件----建立DataView物件View物件
- go物件導向方法的呼叫Go物件
- JS中建立物件的方法JS物件
- js正則建立物件方法JS物件
- 物件導向 -- 反射物件反射
- 反射_Class物件功能反射物件
- JavaScript 基礎(二) – 建立 function 物件的方法, String物件, Array物件JavaScriptFunction物件
- javascript使用new建立物件JavaScript物件
- js建立物件及其使用JS物件
- js 物件反射使用程式碼例項JS物件反射
- C#動態建立介面的實現例項物件C#物件
- [緊急求救]關於動態建立物件的問題物件
- File 物件呼叫 renameTo() 方法失敗原因物件
- Java建立物件的方法有哪些?Java物件
- JavaScript建立物件的多種方法JavaScript物件
- JavaScript物件的的建立及屬性狀態維護詳解JavaScript物件
- js常見物件及方法JS物件
- javascript基礎(this,工廠方法來建立物件,建構函式建立物件)(十六)JavaScript物件函式
- 全面梳理JS物件的訪問控制及代理反射JS物件反射
- (C#)利用反射動態呼叫類成員C#反射
- 動態庫的建立和呼叫
- 使用Proxy.newProxyInstance包裝物件池,免去returnObject方法呼叫物件Object