C# 中的元数据

WBOY
发布: 2024-09-03 15:30:18
原创
431 人浏览过

元数据中的 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中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!