日本国产亚洲-日本国产一区-日本国产一区二区三区-日本韩国欧美一区-日本韩国欧美在线-日本韩国欧美在线观看

當前位置:雨林木風下載站 > 技術開發教程 > 詳細頁面

.net的reflection (2)

.net的reflection (2)

更新時間:2022-05-14 文章作者:未知 信息來源:網絡 閱讀次數:

一旦得到類對象,上表中所列的方法就能被叫來調用reflaction.第一個例子將檢查在CSharpReflectionSamples.Reflect類中的得到方法的信息。第一塊代碼用來定義類中的每個方法的名字,第二塊代碼將闡述得到方法信息。向下面所展示的,我們將用一個數組來保存用GetMethod()方法返回的方法信息。MethodInfo類包含信息為方法的名字,不管是否是虛擬的,它都是可見的,等等。

namespace CSharpReflectionSamples
{
using System;
using System.Reflection;

/// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
public static void Main()
{
// the typeof operator and the GetType method
// both return a 'Type' object.
Type type1 = typeof(Reflect);
Reflect objTest = new Reflect(0);
Type type2 = objTest.GetType();

Console.WriteLine("Type of objTest is {0}", type2);
Console.WriteLine();
// pause
Console.ReadLine();

// reflect method information
MethodInfo[] minfo = type1.GetMethods();
// iterate through methods
foreach (MethodInfo m in minfo)
{
Console.WriteLine(m);
}
Console.WriteLine();
}
}
}

下一個例子將展示動態得到對象有可能接觸的每個構造器的信息。類似與上面的例子,我們將返回一個包含每個構造器的信息ConstructorInfo對象。

namespace CSharpReflectionSamples
{
using System;
using System.Reflection;

/// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
public static void Main()
{
// the typeof operator and the GetType method
// both return a 'Type' object.
Type type1 = typeof(Reflect);
Reflect objTest = new Reflect(0);
Type type2 = objTest.GetType();

Console.WriteLine("Type of objTest is {0}", type2);
Console.WriteLine();
// pause
Console.ReadLine();

// reflect constructors
ConstructorInfo[] cinfo = type1.GetConstructors();
// iterate through constructors
foreach (ConstructorInfo c in cinfo)
{
Console.WriteLine(c);
}
}
}
}

最后一部分,也許是reflection名字空間中最激動人心的部分,是在運行時動態調用類方法。有兩種方法,首先,我們將建立一個數組來存儲參數,這些參數被構造器用來建造對象。第二,一個System.Object對象將對抗CreateInstance方法的對象。以得到想得到對象的例子。最后,當我們有了對象的資料,我們能夠調用任何使用MethodParm數組的方法。下面是代碼:

namespace CSharpReflectionSamples
{
using System;
using System.Reflection;

/// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
public static void Main()
{
// the typeof operator and the GetType method
// both return a 'Type' object.
Type type1 = typeof(Reflect);
Reflect objTest = new Reflect(0);
Type type2 = objTest.GetType();



// dynamic creation and invocation
// instantiate the Reflect object, passing
// a value of 1 to the constructor
object[] oConstructParms = new object[] {1};
object obj = Activator.CreateInstance(type1, oConstructParms);
// invoke method of reflect object
object[] oMethodParms = new object[] {17};
int intResult = (int)type1.InvokeMember("AMethod", BindingFlags.Default |
BindingFlags.InvokeMethod, null, obj, oMethodParms);
Console.WriteLine("Result of calling AMethod on {0} is {1}",
type1.Name, intResult);
// pause
Console.ReadLine();
}
}
}


這篇文章闡述了.net Reflaction的基礎,在下一部分,我將和大家討論進一步的話題,比如,動態發布中間語言,旗幟綁定,和中間語言原則。


溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

主站蜘蛛池模板: free youjizz 视频分类 | 欧美成人午夜做爰视频在线观看 | 天天干天天爱天天射 | 欧美日韩午夜精品不卡综合 | 国产99视频精品免视看7 | 欧美日本一道高清免费3区 欧美日本一道免费一区三区 | 欧美成人久久 | 中文字幕狠狠干 | 久久中文字幕在线 | 久久久久久亚洲精品中文字幕 | 91人人视频国产香蕉 | 天天爱天天做天天爽天天躁 | 天天摸天天看天天爽 | 亚州一级毛片 | 日韩专区亚洲综合久久 | 91成人免费观看网站 | 亚洲影院手机版777点击进入影院 | 韩国精品在线观看 | 久久黄色影院 | 欧美另类性视频在线看 | 欧美一区 亚洲 | 国产福利视频一区 | 欧美日韩小视频 | 久久影视免费观看网址 | 成年人网站在线观看视频 | 邪恶工番口番大全全彩色 | 香港三级日本三级人妇三级四 | 天天做天天爱天天综合网2021 | 在线观看精品91老司机 | 夜夜春夜夜爽久久 | 久久久五月 | 福利一区二区在线观看 | 五月天婷婷综合网 | 中国精品视频一区二区三区 | 伊人99在线观看 | 欧美高清xxx | 日韩特级毛片免费观看视频 | 日本午夜视频在线观看 | 欧美在线不卡视频 | 日本不卡在线观看 | 免费www xxx|