首頁  >  文章  >  後端開發  >  C# 中的元數據

C# 中的元數據

WBOY
WBOY原創
2024-09-03 15:30:18433瀏覽

元資料中的 C# 被定義為描述我們的程式的二進位訊息,該資訊要么存儲在公共語言運行時可移植可執行檔中,要么存儲在內存中。如果您從可移植執行檔編譯程式碼,則元資料將插入檔案的另一個區域部分,所有這些程式碼現在將轉換為 MSIL 格式(Microsoft 中間語言),然後程式碼移至檔案的另一個分割部分。程式集中定義和引用的所有資料類型和資料成員都放在元資料中。當我們在運行時執行 C# 程式碼時,它會從記憶體中載入元資料。 C#元資料的主要目的是了解類別的類別、資料成員、繼承、資料類型等資訊。文件中的元資料由表和堆資料結構組成。

元資料的使用

以下是元資料的用途:

  • 它提供了有關程序集資料類型的描述,如名稱、可見性、基類和介面等
  • 它提供了方法、欄位、屬性、事件和巢狀類型等資料成員。
  • 它也提供了修改類型和成員的元素的附加描述。
  • 它具有名稱、版本、公鑰等身分。
  • 它是簡單程式設計模型的關鍵,它將消除 IDL(介面定義語言)檔案、頭檔的必要性。

元資料型別

元資料型別如下圖:

C# 中的元數據

元資料的作用

元資料的作用如下:

C# 中的元數據

元資料在 C# 中如何運作?

C# 元資料可以了解有關資料的資料。

文法:

using packageName;//used for insert the packages in C#
public class MyApp
{
public static int Main()
{
//data types
Console.WriteLine("Required Message");
}
//user defined methods for other logics
}

C# 中的元資料範例

下面給出了 C# 中元資料的範例:

範例#1

3 個數字的乘法

代碼:乘法.cs

using System; //Used for declaring the package or used for importing existed packege
public class Multiplication//declaring the class
{
public static int Main ()// main method for displaying the output
{
//declaring and defining the varaiables
int x = 50;
int y = 20;
int z=30;
//Printing the output of the multiplication of 2 numbers
Console.WriteLine ("Multiplication of {0},{1} and {2} is {3}",x,y,z,multiplication(x,y,z));
return 0;
}
public static int multiplication(int x, int y, int z)// multiplication() method implemention
{
return (x * y*z);// return multiplication of 3 numbers
}
}

輸出:

C# 中的元數據

說明:

  • 正如您在「關於」中看到的那樣,您可以看到實際數據,如果我們想要元數據或二進位數據,我們可以在機器生成的程式碼中看到編譯器,這些程式碼始終是加密的,人類無法理解它。

範例#2

正方形面積

代碼:SquareOfArea.cs

using System; //Used for declaring the package or used for importing existed packege
public class SquareArea//declaring the class
{
public static int Main ()// main method for displaying the output
{
//declaring and defining the varaiables
int x = 50;
//Printing the output of the areaOfSquare
Console.WriteLine ("Area of Square is {0}",areaOfSquare(x));
return 0;
}
public static int areaOfSquare(int x)// multiplication() method implemention
{
return (x*x);// return area Of Square
}
}

輸出:

C# 中的元數據

說明:

  • 正如您在「關於」中看到的那樣,您可以看到實際數據,如果我們想要元數據或二進位數據,我們可以在機器生成的程式碼中看到編譯器,這些程式碼始終是加密的,人類無法理解它。

範例#3

多個帶有資料的類別

程式碼:MultiData.net

using System; //Used for declaring the package or used for importing existed packege
using System.Collections.Generic; //Used for declaring the package or used for importing existed packege
public class Entity {//declaring the class
//setters and getters for set and get the data
public string Name {get;set;}
public string Uses {get;set;}
//toString method to overide predefined String data
public override string ToString() {
string output1=string.Format("My Name is {0}", Name);
string output2=string.Format(" He is: {0}", Uses);
return output1+output2;
}
}
//declaring interface with reference class extention
public interface IMeta<T> where T: class {
//setters and getter for set and get the data
T Inner {get;set;}
stringMetaData {get;set;}
}
//declaring interface with reference class extention
public interface IStorage<T> where T: class {
//method definition for save the data
T Save();
}
//declaring the class by extending Imeta and IStorage interfaces
public class Meta<T> : IMeta<T>, IStorage<T>
where T: class
{
//creating a generic dictionary variable
private static Dictionary<T, Meta<T>> _stash = new Dictionary<T, Meta<T>>();
//constructor for the class
public Meta(T item) {
Inner = item;
}
//setters and getters for set and get the data
public T Inner {get;set;}
public string MetaData {get;set;}
//method implementation for operator
public static implicit operator T(Meta<T> meta) {
if (! _stash.ContainsKey(meta.Inner))
_stash.Add(meta.Inner, meta);
returnmeta.Inner;
}
public static implicit operator Meta<T>(T item) {
try {
return _stash[item];
} catch {
return null;
}
}
//save the data to repository
public T Save() {
return this;
}
}
//declaring the class
public static class MetaHelper {
//method definition for return the data
public static IMeta<T>GetMeta<T>(T item) where T: class {
return (Meta<T>)item;
}
//method definition for store the data
public static IStorage<T>GetStorage<T>(T item) where T: class {
return (Meta<T>)item;
}
}
//declaring the class
public class Program
{
//Entity type for createEntity method definition with 2 arguments
public static Entity CreateEntity(string name, string uses) {
//creating a variable
var result = new Meta<Entity>(new Entity(){ Name = name, Uses = uses });
//adding data to the variable that is metadata
result.MetaData = "Paramesh";
return  result;
}
//test method to test the data
public static void Main()
{
//Passing the values to createEntity method
varent = CreateEntity("Amardeep", "Good Person");
//types casting ent into Meta class
Meta<Entity> meta = (Meta<Entity>)ent;
//creating variables
varimeta = MetaHelper.GetMeta<Entity>(ent);
varistore = MetaHelper.GetStorage<Entity>(ent);
var stored = istore.Save();
//Displaying output
Console.WriteLine("MetaData: {0} {1} {2} {3}", imeta.MetaData, imeta.Inner.Name, stored.Name, stored.Uses);
Console.WriteLine(ent);
if (meta != null) Console.WriteLine(meta.MetaData);
elseConsole.WriteLine("This is not a meta type");
}
}

輸出:

C# 中的元數據

說明:

  • 正如您在「關於」中看到的那樣,您可以看到實際數據,如果我們想要元數據或二進位數據,我們可以在機器生成的程式碼中看到編譯器,這些程式碼始終是加密的,人類無法理解它。

結論

C#中的元資料用於了解資料的相關資訊。這些都是加密成二進位格式的,這是人類無法理解的,這就是為什麼我們將二進位代碼轉換為正常程式碼來分析邏輯。

以上是C# 中的元數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 序列化下一篇:C# 序列化