日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

.NET Framework- 反射特性序列化(Day4)

發(fā)布時間:2025/4/16 asp.net 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .NET Framework- 反射特性序列化(Day4) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?????????????????????????????????????? ?反射&特性&序列化

先回憶一下:
????? 兩次編譯一次運行:1)第一次將源代碼轉(zhuǎn)換為中間語言(exe、dll內(nèi)部都存放的中間語言)
???????????????????????????????????? ????? 2)第二次將中間語言轉(zhuǎn)換成機器語言 1、反射:編程的讀取與類型相關(guān)聯(lián)的元數(shù)據(jù)的行為,給定一個路徑通過特定方法得到一個類中所有的成員,這樣比較方便,不必添加引用就可以動態(tài)獲取內(nèi)部成員,所屬命名空間System.Reflection 使用反射的步驟:
?????? 1)利用Assembly的LoadForm方法引用一個要反射的程序集,將路徑引用進來
????????? 例:Assembly assembly=Assembly.LoadFrom (@"C:\Users\ling890316\Desktop\暑期實訓(xùn)\ManageMinpian\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe");
? 2)為被反射的程序集創(chuàng)建實例
????????? 例:object obj=assembly.CreateInstance(" Client.Program");//此時就會調(diào)用一次反射類的無參構(gòu)造函數(shù)
?????? 3)可以反射不同類型的成員(方法、屬性、構(gòu)造函數(shù)、字段、類型等等) 反射方法
??????????? MethodInfo mi= obj.GetType().GetMethod("Fangfa",BindingFlags.NonPublic |BindingFlags.Instance? );//將方法名引進來,反射私有的方法
??????????? //MethodInfo mi = obj.GetType().GetMethod("Fangfa");//反射公有的方法
??????????? mi.Invoke(obj,null);//調(diào)用被反射的成員或類型的方法 ? 反射屬性:
?????????? PropertyInfo pi=? obj.GetType().GetProperty("Str");//反射程序集的屬性Str
?????????? pi.SetValue(obj,29,null);//為屬性Str設(shè)置值,調(diào)用屬性中的set訪問器
?????????? Console.WriteLine ( pi.GetValue(obj, null));//獲取屬性中的值,調(diào)用屬性的get訪問器,并打印出來 ?? 反射所有的成員:????????????
??????????? object[] member = obj.GetType().GetMembers(BindingFlags.NonPublic | BindingFlags.Instance);//獲取被反射類的所有私有成員
??????????? //最終用一個object類型的數(shù)組保存
??????????? foreach (object a in member)
???????????? {
??????????????? Console.WriteLine(a);
???????????? }//將所有反射到的私有成員顯示出來
??????????
?? 反射構(gòu)造函數(shù)(有參或無參):???????????????
???????????? 1)反射有參構(gòu)造函數(shù)
??????????? Type[] types = new Type[] {typeof (string )};
??????????? ConstructorInfo ci= obj.GetType().GetConstructor(types);//反射有參的構(gòu)造函數(shù),該方法必須接收一個Type類型的數(shù)組
??????????? //數(shù)組中存放的是string類型的值然后轉(zhuǎn)化成Type類型
??????????? object [] obj1=new? object []{"7373"};//定義一個object類型的數(shù)組,該數(shù)組保存一個值是string類型的,與有參構(gòu)造函數(shù)
??????????? //的參數(shù)匹配
??????????? ci.Invoke(obj,obj1);//在實現(xiàn)時就會調(diào)用有參構(gòu)造函數(shù) 2)反射無參構(gòu)造函數(shù)
??????????? Type[] types = new Type[] {};
??????????? ConstructorInfo ci= obj.GetType().GetConstructor(types);//反射無參的構(gòu)造函數(shù),該方法必須接收一個Type類型的數(shù)組
??????????? //要想使得可以反射無參構(gòu)造函數(shù),可以在types數(shù)組中不存放值???????????
??????????? ci.Invoke(null);//直接就可以調(diào)用無參的構(gòu)造函數(shù)了,這里同ci.Invoke(obj,null);是一樣的,都調(diào)用無參構(gòu)造函數(shù) 反射字段:
??????????? FieldInfo fi= obj.GetType().GetField("ziduan",BindingFlags.NonPublic |BindingFlags.Instance );???????????
??????????? Console.WriteLine ( fi.GetValue(obj)); 反射索引器: 這個地方我自己還沒有做出來,回頭補上!
???????????
注意:只有屬于命名空間的類型或該類型的成員才會反射回來,至于被反射的的命名空間使用的成員或類型無法反射得到 2、特性(屬性):Attribute非property(類的成員),它不是類的成員,只是附屬于類的,屬性提供功能強大的方法以將聲明信息與C#代碼(類型、方法、屬性等)相關(guān)聯(lián)。
??????? 屬性與程序?qū)嶓w關(guān)聯(lián)后,即可在運行時使用名為“反射”的技術(shù)查詢屬性。 特性以兩種形式出現(xiàn):
??????1) 一種是在公共語言運行庫 (CLR) 中定義的屬性(微軟定義好的)。
?????? 2)另一種是可以創(chuàng)建的用于向代碼中添加附加信息的自定義屬性。此信息可在以后以編程方式檢索。 特性具有以下特點:
?????? 1)屬性可向程序中添加元數(shù)據(jù)。元數(shù)據(jù)是嵌入程序中的信息,如編譯器指令或數(shù)據(jù)描述。
?????? 2)程序可以使用反射檢查自己的元數(shù)據(jù)。
??????3) 通常使用屬性與 COM 交互。 自定義特性:
????? 特性類實例化時需要放在括號“[ ]”中,語法:
????? [attributeClass(定位參數(shù)1,… 命名參數(shù)1,…)] 自定義特性有兩種類型:
????????? 1)定位參數(shù)(相當(dāng)于實例類的構(gòu)造函數(shù))
????????? 2)命名參數(shù)(相當(dāng)于實例類的屬性,可以賦值也可以不賦值(默認值)) 例子:以下就是使用Windows的一個特性實現(xiàn)在控制臺彈出窗口
????
??????? [DllImportAttribute("user32.dll", EntryPoint = "MessageBoxW")]//自定義一個特性,"user32.dll"是定位參數(shù)
????????????? //EntryPoint = "MessageBoxW"是命名參數(shù)
??????? public static extern int MessageBoxW([InAttribute()] System.IntPtr hWnd,
??????????? [InAttribute()] [MarshalAsAttribute(UnmanagedType.LPWStr)] string lpText,
??????????? [InAttribute()] [MarshalAsAttribute(UnmanagedType.LPWStr)] string lpCaption,
??????????? uint uType); static void Main(string[] args)
??????? {
??????????? Console.WriteLine(MessageBoxW(IntPtr.Zero, "確定嗎?", "提示", 1));
??????? } 小練習(xí):講這個自定義特性的時候老師用了一個很形象的比喻的比喻就是豬與虱子的關(guān)系,虱子是附加在豬身上的,不是豬的一部分,這里的虱子就相當(dāng)于一個自定義的特性,下面我們來看看實現(xiàn)了的這個小程序吧! using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection; namespace ConsoleApplication4
{
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
??????????? zhu zu = new zhu();//實例化豬這個類的對象
??????????? foreach (object obj in zu.GetType().GetCustomAttributes(true))//用反射的方法得到zu這個類的特性,?GetCustomAttributes(true)此處必須填true,至于什么原因我還沒弄明白,記住吧!!!
??????????????? //得到了特性,由于它返回的是一個object類型的數(shù)組,所以先遍歷出來
??????????? {
??????????????? shiziAttribute shizi = obj as shiziAttribute;//將得到的object類型的特性先轉(zhuǎn)換成shiziAttribute 類型
??????????????? Console.WriteLine("名稱: {0} 大小: {1}",shizi.Name,shizi.Size? );//打印輸出該特性的屬性值
??????????? }
???????????
??????? } }
??? [AttributeUsage(AttributeTargets.Class,AllowMultiple=true,Inherited=true)]//定義一個特性,它針對類Class建立的,
??? //AllowMultiple=true表示它允許有多個實例,Inherited=true表示其可以被其他類繼承
??? class shiziAttribute : Attribute
??? {
??????? string name;
??????? public shiziAttribute(string name)
??????? {
??????????? this.name= name;
??????? }//為該特性的類定義一個有參構(gòu)造函數(shù),接收一個string類型的參數(shù),在內(nèi)部設(shè)置其值
??????? public string Name
??????? {
??????????? get
??????????? {
??????????????? return name;
??????????? }
??????? }//屬性Name,只有可讀屬性
??????? double size;
??????? public double Size
??????? {
??????????? get
??????????? {
??????????????? return size;
??????????? }
??????????? set
??????????? {
??????????????? size = value;
??????????? }
??????? }//屬性Size,可讀可寫
??? }
??? [shizi("大虱子", Size = 2.3)]//特性的使用方法,"大虱子"是特性定義中的定位參數(shù)(相當(dāng)于實例類中的構(gòu)造函數(shù));
??? // Size = 2.3是特性定義中的命名參數(shù)(相當(dāng)于實例類中的屬性)
??? [shizi("小虱子", Size = 1.0)]//因為在定義特性類的時候設(shè)置了AllowMultiple=true屬性,所以允許有多個特性的實例
??????? //存在,虱子的特性附加在豬上,一定要分清楚該特性不是豬的一部分,不能通過豬這個類的實例化對象直接訪問
??? class zhu
??? {
??? }
} 練習(xí):利用反射的原理實現(xiàn)操作數(shù)據(jù)庫數(shù)據(jù)的功能 思路總結(jié):這個由于最近要做項目,所以在這里先空出來,以后補上啦!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection; namespace ConsoleApplication1
{
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
??????????? //第三步:反身特性類信息,使用反射的方法,反射回類Blog所有的特性
??????????? Blog blog = new Blog();
??????????? blog.Author = "桂素偉";
??????????? blog.Title = "技術(shù)有你,共同前行";
??????????? blog.CreateDate = DateTime.Parse("2007-9-1");
??????????? blog.ID = 10; //delete 表名 where 主鍵字段名=值 Console.WriteLine(GetDelete(blog)); Article article = new Article();
??????????? article.Title = "c#學(xué)習(xí)方法";
??????????? article.Type = "C#";
??????????? article.Content = "每天學(xué)習(xí)18個小時";
??????????? article.ID = 200;
??????????? Console.WriteLine(GetUpdate(article));
??????? }
??????? //update 表 set 字段1=值1,字段2=值2 where 主鍵字段=值
??????? static string GetUpdate(IEntity entity)
??????? {
??????????? object[] objarr = entity.GetType().GetCustomAttributes(true);
??????????? if (objarr.Length == 0)
??????????? {
??????????????? return "";
??????????? }
??????????? else
??????????? {
??????????????? foreach (object o in objarr)
??????????????? {
??????????????????? if (o is TableAttribute)
??????????????????? {
??????????????????????? TableAttribute ta = o as TableAttribute;//將反射得到的特性(object類型)轉(zhuǎn)換成TableAttribute格式
??????????????????????? string tablename = ta.TableName; string fieldname = "";
??????????????????????? string fieldvalue = "";
??????????????????????? Dictionary<string, string> pros = new Dictionary<string, string>();
??????????????????????? foreach (PropertyInfo pi in entity.GetType().GetProperties())//獲取blog這個對象的全部的屬性,比如id、zuozhe、biaoti、shijian
??????????????????????? { object[] proarr = pi.GetCustomAttributes(true);//反射得到blog屬性的特性,將其保存在一個object類型的數(shù)組中
??????????????????????????? if (proarr.Length == 0)
??????????????????????????? {
??????????????????????????????? continue;
??????????????????????????? }//如果數(shù)組為空,表示blog的當(dāng)前循環(huán)中的這個屬性沒有附加特性,返回屬性(返回foreach (PropertyInfo pi in entity.GetType().GetProperties())?繼續(xù)循環(huán)
??????????????????????????? else
??????????????????????????? {
??????????????????????????????? foreach (object ob in proarr)//遍歷blog的當(dāng)前屬性的特性
??????????????????????????????? {
??????????????????????????????????? if (ob is FieldAttribute)//判斷blog的當(dāng)前屬性是否附加了特性
??????????????????????????????????? {
??????????????????????????????????????? FieldAttribute fa = ob as FieldAttribute;//將反射得到的屬性的特性(object類型)轉(zhuǎn)換成FieldAttribute格式
??????????????????????????????????????? if (fa.IsPrimaryKey)//判斷當(dāng)前的特性是否是主鍵
??????????????????????????????????????? {
??????????????????????????????????????????? fieldname = fa.FieldName;//判斷如果是主鍵,則獲取該屬性特性的名稱賦值給外部定義好的fieldname中
??????????????????????????????????????????
??????????????????????????????????????????? fieldvalue = pi.GetValue(entity, null).ToString();//獲取該屬性(為主鍵)的值(該屬性對應(yīng)的特性)將其保存在外部定好的fieldvalue中
??????????????????????????????????????? }
??????????????????????????????????????? else
??????????????????????????????????????? {
??????????????????????????????????????????? pros.Add(fa.FieldName, pi.GetValue(entity, null).ToString());//將不為主鍵的其他的屬性的特性和獲取到的屬性對應(yīng)的值保存在定義好的泛型哈希表中
??????????????????????????????????????? }
??????????????????????????????????? }//表示blog的屬性附加有特性,那么就將得到的特性(保存在object類型的數(shù)組o中)轉(zhuǎn)換為FieldAttribute?先判斷,如果該特性的IsPrimary為真,就將該特性的名稱保存在primaryname中,將該特性的值保存在?primaryvalue中,供后來使用
??????????????????????????????? }
??????????????????????????? }
??????????????????????? } if (pros.Count == 0)
??????????????????????? {
??????????????????????????? return "";
??????????????????????? }//判斷,如果哈希表中沒有元素,即沒有檢索到屬性的特性和對應(yīng)的屬性的值 if (fieldname == "" || fieldvalue == "")
??????????????????????? {
??????????????????????????? return "";
??????????????????????? }//判斷如果沒有主鍵或者主鍵為空,就返回空值 string sql = "update " + tablename + " set ";
??????????????????????? foreach (string key in pros.Keys)
??????????????????????? {
??????????????????????????? sql += key + "='" + pros[key] + "',";
??????????????????????? }
??????????????????????? sql = sql.TrimEnd(',') + " where " + fieldname + "=" + fieldvalue;
??????????????????????? return sql;
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? return "";
??????????????????? }
??????????????? }
??????????????? return "";
??????????? }
??????? }
???????
??????? static string GetDelete(IEntity entity)
??????? {
??????????? TableAttribute ta = entity.GetType().GetCustomAttributes(true)[0] as TableAttribute;
??????????? string tablename = ta.TableName; string fieldname = "";
??????????? string fieldvalue = "";
??????????? foreach (PropertyInfo pi in entity.GetType().GetProperties())
??????????? {
??????????????? FieldAttribute fa = pi.GetCustomAttributes(true)[0] as FieldAttribute;
??????????????? if (fa.IsPrimaryKey)
??????????????? {
??????????????????? fieldname = fa.FieldName;
??????????????????? fieldvalue = pi.GetValue(entity, null).ToString();
??????????????? }
??????????? } string sql = "delete " + tablename + " where " + fieldname + "=" + fieldvalue;
??????????? return sql; }
??? }
??? //第一步:定義特性類
??? /// <summary>
??? /// 定義一個特性類,用來保存表名
??? /// </summary>
??? [AttributeUsage(AttributeTargets.Class)]
??? class TableAttribute : Attribute
??? {
??????? string tablename;
??????? public TableAttribute(string tablename)
??????? {
??????????? this.tablename = tablename;
??????? }//為特性建立一個定位參數(shù),該參數(shù)表示表的名稱,必須有值
??????? public string TableName
??????? {
??????????? get
??????????? {
??????????????? return tablename;
??????????? }
??????? }//為name定義一個只讀屬性,使得它在外部可以被訪問的到
??? }
??? /// <summary>
??? /// 定義一個特性類,用來指向table表中不同的字段
??? /// </summary>
??? [AttributeUsage(AttributeTargets.Property)]
??? class FieldAttribute : Attribute
??? {
??????? string fieldname;
??????? public FieldAttribute(string fieldname)
??????? {
??????????? this.fieldname = fieldname;
??????? }//為特性類定義一個定位參數(shù),用來保存唯一且必須存在的值,表示表中的字段名稱
??????? public string FieldName
??????? {
??????????? get
??????????? {
??????????????? return fieldname;
??????????? }
??????? }//為fieldname定義一個屬性,使得它在外部可以被訪問的到 public bool IsPrimaryKey
??????? {
??????????? get;
??????????? set;
??????? }//表示是否為表中的主鍵
??? }
??? interface IEntity
??? {
??? }
??? //第二步:使用特性類
??? [Table("blogs")]
??? class Blog : IEntity
??? {
??????? [Field("ID", IsPrimaryKey = true)]//在Blog這個類里定義了四個字段,對應(yīng)四個屬性,這四個字段分別是博客編號、博客作者、博客主題、博客的創(chuàng)建時間,分別對這四個屬性使用外部定義好的特性,例如第一個屬性表示博客編號的屬性ID,它引用了外部的特性,映射在blogs表中其存儲為ID(特性定義中的定位參數(shù))同時在特性中定義了IsPrimaryKey=true,表示該字段是blogs表中的主鍵,通俗地說要想使該類的屬性都可以存儲對應(yīng)blogs表中的字段,必須對該表所有的屬性都套用外部定義好針對屬性的特性FieldAttribute
??????? public int ID
??????? {
??????????? get;
??????????? set;
??????? }
??????? [Field("ZuoZhe")]
??????? public string Author
??????? {
??????????? get;
??????????? set;
??????? }
??????? [Field("BiaoTi")]
??????? public string Title
??????? {
??????????? get;
??????????? set;
??????? }
??????? [Field("ShiJian")]
??????? public DateTime CreateDate
??????? {
??????????? get;
??????????? set;
??????? }
??? }
?? // [Table("articles")]
??? class Article : IEntity
??? {
??????? [Field("ID", IsPrimaryKey = true)]
??????? public int ID
??????? {
??????????? get;
??????????? set;
??????? }//博客編號,主鍵
??????? [Field("TM")]
??????? public string Title
??????? {
??????????? get;
??????????? set;
??????? }//博客主題
??????? [Field("NR")]
??????? public string Content
??????? {
??????????? get;
??????????? set;
??????? }//博客內(nèi)容
??????? [Field("LX")]
??????? public string Type
??????? {
??????????? get;
??????????? set;
??????? }//博客類型 public int T
??????? {
??????????? get;
??????????? set;
??????? }//這個屬性沒有附加特性 }
}
3、序列化:將對象狀態(tài)轉(zhuǎn)換為可保持(保存)或傳輸?shù)男问降倪^程。序列化的補集是反序列化,后者將流轉(zhuǎn)換為對象,這兩個過程一起保證數(shù)據(jù)易于存儲和傳輸,本質(zhì)是存儲字段(數(shù)據(jù)),中間的方法等其他的都不去序列化。 序列化的兩種方式:
?????? 1)序列化二進制(占用空間小)
?????? 2)序列化XML(格式很好,但文件較大) 序列化的命名空間
?????????? 1)二進制序列化需要的命名空間
????????????? using System.Runtime.Serialization;
????????????? using System.Runtime.Serialization.Formatters.Binary; 2)XML序列化需要的命名空間
????????????? using System.Runtime.Serialization.Formatters.Soap;(需要添加引用) 選擇序列化:
??? [Serializable]//加了這個標(biāo)志后表示該成員可以被序列化
??? public class Person
??? {????????
?????? [NonSerialized]
??????? public? int age;//這個age將不會序列化
??????? public bool Sex
??????? {? get;? set;? }
??? } 序列化的小練習(xí):??
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Collections ;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters .Binary ;
using System.Runtime.Serialization.Formatters.Soap; namespace Client
{
??? class Program
??? { static void Main(string[] args)
??????? {
??????????? Person xihong = new Person();
??????????? xihong.Name = "小紅";
??????????? xihong.Age = "21"; 序列化為二進制文件
??????????? //IFormatter formatter = new BinaryFormatter();//格式化器
??????????? //Stream stream = new FileStream ("D:/a.bin",FileMode.Create ,FileAccess.Write );
??????????? //formatter.Serialize(stream,xihong );
??????????? //stream.Close();
序列化為XML文件
??????????? //IFormatter formatter1 = new SoapFormatter();
??????????? //Stream stream1 = new FileStream("D:/a.xml", FileMode.Create, FileAccess.Write);
??????????? //formatter1.Serialize(stream1, xihong);
??????????? //stream1.Close(); //反序列化
??????????? IFormatter formatter = new BinaryFormatter();
??????????? Stream stream = new FileStream("D:/a.bin",FileMode.Open ,FileAccess.Read );
??????????? Person xi = (Person)formatter.Deserialize(stream);//
將二進制文件中的流反序列化出來轉(zhuǎn)換成XiHong 這個類類型,并保存在一個新的對象xi中,注:這個新對象xi同原來的對象xihong只是數(shù)據(jù)一樣,本質(zhì)是不同的
??????????? Console.WriteLine(xi.Name );
??????????? Console.WriteLine(xi.Age ); Console.WriteLine(xi.Equals(xihong));//此時是返回false,因為這個新對象xi同原來的對象xihong只是數(shù)據(jù)一樣,本質(zhì)是不同的
??????? }??????
??????
??? }
??? [Serializable ]
??? class Person
??? {
??????? public string Name
??????? {
??????????? get;
??????????? set;
??????? }
??????
??????? public string Age
??????? {
??????????? get;
??????????? set;
??????? }
??? }
?

?

轉(zhuǎn)載于:https://blog.51cto.com/fayling/626665

總結(jié)

以上是生活随笔為你收集整理的.NET Framework- 反射特性序列化(Day4)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。