您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)C#中怎么訪問私有成員,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
首先訪問一個(gè)類的私有成員不是什么好做法。大家都知道私有成員在外部是不能被訪問的。一個(gè)類中會存在很多私有成員:如私有字段、私有屬性、私有方法。對于私有成員造訪,可以套用下面這種非常好的方式去解決。
private string name; public string Name { get { return name; } set { name = value; } }
但是有時(shí)候,源代碼是別人的,只提供給你dll。或者你去維護(hù)別人的代碼,源代碼卻有丟失。這樣的情況或許你想知道私有成員的值,甚至去想直接調(diào)用類里面的私有方法。那怎么辦呢?在.net中訪問私有成員不是很難,這篇文章提供幾個(gè)簡單的方法讓你如愿以償。
為了讓代碼用起來優(yōu)雅,使用擴(kuò)展方法去實(shí)現(xiàn)。
1、得到私有字段的值:
public static T GetPrivateField<T>(this object instance, string fieldname) { BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic; Type type = instance.GetType(); FieldInfo field = type.GetField(fieldname, flag); return (T)field.GetValue(instance); }
2、得到私有屬性的值:
public static T GetPrivateProperty<T>(this object instance, string propertyname) { BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic; Type type = instance.GetType(); PropertyInfo field = type.GetProperty(propertyname, flag); return (T)field.GetValue(instance, null); }
3、設(shè)置私有成員的值:
public static void SetPrivateField(this objectinstance, stringfieldname, objectvalue) { BindingFlagsflag = BindingFlags.Instance | BindingFlags.NonPublic; Typetype = instance.GetType(); FieldInfofield = type.GetField(fieldname, flag); field.SetValue(instance, value); }
4、設(shè)置私有屬性的值:
public static void SetPrivateProperty(this objectinstance, stringpropertyname, objectvalue) { BindingFlagsflag = BindingFlags.Instance | BindingFlags.NonPublic; Typetype = instance.GetType(); PropertyInfofield = type.GetProperty(propertyname, flag); field.SetValue(instance, value, null); }
5、調(diào)用私有方法:
public static T CallPrivateMethod<T>(this object instance, string name, params object[] param) { BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic; Type type = instance.GetType(); MethodInfo method = type.GetMethod(name, flag); return (T)method.Invoke(instance, param); }
測試:
下面我們使用一個(gè)測試類,進(jìn)行測試。新建一個(gè)類庫項(xiàng)目,測試的類代碼如下:
public class TestClass { public TestClass() { privatefield1 = 1; privatefield2 = 99; PrivateFieldA = "Lo"; PrivateFieldB = "ve"; } private int privatefield1; private int privatefield2; private string PrivateFieldA { get; set; } private string PrivateFieldB { get; set; } private int Add() { return privatefield1 + privatefield2; } private string Join() { return PrivateFieldA + PrivateFieldB; } }
將上面類庫的dll引入控制臺項(xiàng)目中。使用下面代碼去使用這個(gè)類的私有成員:
TestClass obj = new TestClass(); System.Console.WriteLine("私有字段"); System.Console.WriteLine(obj.GetPrivateField<int>("privatefield1")); System.Console.WriteLine(obj.GetPrivateField<int>("privatefield2")); System.Console.WriteLine("私有屬性"); System.Console.WriteLine(obj.GetPrivateProperty<string>("PrivateFieldA")); System.Console.WriteLine(obj.GetPrivateProperty<string>("PrivateFieldB")); System.Console.WriteLine("私有方法"); System.Console.WriteLine(obj.CallPrivateMethod<int>("Add",null)); System.Console.WriteLine(obj.CallPrivateMethod<string>("Join", null)); System.Console.WriteLine("修改私有屬性"); obj.SetPrivateProperty("PrivateFieldA", "hello"); obj.SetPrivateProperty("PrivateFieldB", "world"); System.Console.WriteLine(obj.CallPrivateMethod<string>("Join", null)); System.Console.Read();
結(jié)果如下:
上述就是小編為大家分享的C#中怎么訪問私有成員了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。