首页 > 其他分享 >CLR Resolve a referenced type

CLR Resolve a referenced type

时间:2023-01-31 17:35:06浏览次数:57  
标签:... Resolve Point cs Program referenced type public exe

CLR如何解析引用的类型

CLR寻找类型只有三种可能地方

CLR Resolve a referenced type_System

  1. 相同文件(相同程序集)
  2. 不同文件, 相同程序集
  3. 不同文件, 不同程序集
相同文件

案例一:

以下案例是执行Point_1.exe时, 运行到statement ①时, CLR寻
找Point类型在相同的程序集中相同模块文件Point_1.exe的情形

  1. 源程序:
/* File: Point.cs*/
public class Program {
public static void Main() {
System.Console.WriteLine(new Point(){ x = 1, y = 2});// statement ①
}
}

public class Point
{
public int x { set; get; }
public int y { set; get; }
public override string ToString() { return $"Point({this.x}, {this.y})"}
}
  1. 编译:
...> csc Point.cs /out:Point_1.exe
  1. 执行:
...> Point_1.exe // Output: Point(1, 2)

案例二:

以下案例虽然类型Point被定义在不同的C#源文件中, 但编译时生成
一个单文件程序集Point_2.exe. 该案例用来说明这种情况仍然是情
形一, 即相同文件指的是相同程序集的相同模块文件

  1. 源文件
/* File: Main_Point.cs */
public class Program {
public static void Main() {
System.Console.WriteLine(new Point(){ x = 1, y = 2});
}
}
/* File: Point.cs */
public class Point
{
public int x { set; get; }
public int y { set; get; }
public override string ToString() { return $"Point({this.x}, {this.y})"; }
}
  1. 编译
...> csc Point.cs Main_Point.cs /out:Point_2.exe
  1. 运行
...> Point_2.exe // Output: Point(1, 2)

目录结构

CLR Resolve a referenced type_Assembly_02

不同文件, 相同程序集

案例三:
以下案例将实现了乘法功能的源码MultiplyUtility.cs编译成一个必须依附其它Managed Module的
Module即MultiplyUtility.netmodule, 然后将Program.cs编译成一个Assembly Manifest
宿主Managed Module. 至此, 一个Multifile Assembly产生, 它由两个File构成.

  1. 源码

CLR Resolve a referenced type_程序集_03

  1. 编译(成多文件程序集)
...> csc /t:module MultiplyUtility.cs // file generated: MultiplyUtility.netmodule
...> csc /t:exe /addmodule:MultiplyUtility.netmodule Program.cs // file generated: Program.exe

注: 此时程序集为多文件程序集, 结构如下

CLR Resolve a referenced type_System_04

  1. 运行
...> Program.exe
10
20
200
不同文件, 不同程序集

案例四:
注: 如果CLR类型搜索时, 发现它在另一个程序集, 那么CLR loads 包含Referenced Assembly's Manifest的File,
Reference Assembly可能是多文件程序集, 所以Referenced Type可能在Referenced Assembly的Main Managed Module
file中, 亦可能在其他模块文件中.

  1. 源码(与 "不同文件, 相同程序集"一样)

CLR Resolve a referenced type_System_05

  1. 编译(成多个程序集)
...> csc /t:library /out:Multiply.dll MultiplyUtility.cs // file generated: Multiply.dll
...> csc /t:exe /out:Program.exe /reference:Multiply.dll Program.cs // file generated: Program.exe
  1. 运行((与 "不同文件, 相同程序集"一样)
...> Program.exe
10
20
200

案例五:
程序集"mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"​​是.NET​​ Framework中核心
的程序集之一. 执行到Statement ②时, Resolve Type - Console 时, 需要Load上一句提到的Assembly中Module File -
MSCorLib.dll

  1. 代码
public class Program {
public static void Main() {
System.Console.WriteLine("Ni Hao!");// Statement ②
}
}
  1. 编译
...> csc /out:Program.exe /r:MSCorLib.dll Program.cs
  1. 执行
...> Program.exe
Ni Hao!
Early Binding versus Late Binding
Early Binding(Static Binding)

早期绑定是 During Compile Time 就确定应用程序要使用的类型和方法

以上的所有案例均是 Early Binding

Late Binding(Dynamic Binding)

During Compile Time, Compiler 不知道变量是什么类型的对象以及它所持有的方法或属性是什么,这
里的对象被称为Dynamic Object, 而该对象的具体类型取决于右侧数据 DuringRun Time. 这种情况就
是晚期绑定.

案例六:
以下案例与案例三一样实现对输入数据执行乘法运算的功能. 该案例是在Run Time, 通过Reflection机制
Load Assembly, 绑定obj到另一个程序集的Type - Myclass并调用Multiply方法.

  1. 源程序
    注: 以下为源程序文件图片

    注: 以下为源程序, 与图片内容无区别, 方便复制
// 3. *不同程序集, 不同文件*, 利用反射在运行时加载程序集
// The Assembly Manifest Module - Program.exe. It's AssemblyRef MetaData Table doesn't have Assembly Reference Entry about Multiply
// File: Program.cs
using System.Reflection;
using System;
public class Program {
public static void Main() {
int a = int.Parse(System.Console.ReadLine());
int b = int.Parse(System.Console.ReadLine());
Type tp = Assembly
.LoadFile(@"C:\Users\Administrator\Desktop\abcdef\Multiply.dll") // 引用类型所在程序集的所在模块文件
.GetType("MyClass");
object obj = Activator.CreateInstance(tp);//实例化
MethodInfo meth = tp.GetMethod("Multiply");//加载方法
int? res = meth.Invoke(obj, new Object[] {a, b}) as int?;//执行
System.Console.WriteLine(res.Value);
System.Console.ReadKey();
}
}
/* MultiplyUtility.cs */
public class MyClass
{
public static int Multiply(int a, int b){
return a * b;
}
}
  1. 编译(成两个程序集)
...> csc /t:library /out:Multiply.dll MultiplyUtility.cs
...> csc /t:exe /out:Program.exe Program.cs
  1. 执行
...> Program.exe
10
20
200

标签:...,Resolve,Point,cs,Program,referenced,type,public,exe
From: https://blog.51cto.com/u_15949546/6029805

相关文章

  • MIMETYPE
    使用如下代码下载文件,在IE中,后缀名会自动变为.txt(IE浏览器上有这种情况,Chrome没有)。cl_wd_runtime_services=>attach_file_to_response(EXPORTING......
  • react native启动时报错Could not resolve com.facebook.react:react-native:+
    启动项目是报错大致如下:解决这个issue已经给出了解决方法https://github.com/facebook/react-native/issues/35210rn>=0.63rn官方为大于0.63的所有主要版本都准......
  • Python 错误:TypeError: range() takes no keyword arguments
    问题描述:for循环时使用range()出错:forpageinrange(start=1,stop=8+1,step=1):print(page)结果报错TypeError:range()takesnokeywordargument......
  • TypeDB Forces 2023 C-D
    C.RemovetheBracket题链首先这个xy不能为负数并且s一定的情况下一定是有一种分法的肯定我们最喜欢的看到的就是x=aiy=0这种有0的分法我们不妨猜测对于每个ai......
  • TypeDB Forces 2023 C. Remove the Bracket
    链接:https://codeforces.com/contest/1787/problem/C题意:给定数组a数n和s,再由\(x_i+y_i=a_i\)且\((x_i-s)\cdot(y_i-s)\geq0\)一个式子令其值最小$F=a_1\cdotx_......
  • TypeDB Forces 2023 E. The Harmonization of XOR
    链接:https://codeforces.com/contest/1787/problem/E题意:给定n,有一个数组a,满足其所有元素均为1~n,给定k,问能否将数组拆为k个,其每一组的xor为x。我们发现任意三个xor为k的......
  • TypeDB Forces 2023 (Div. 1 + Div. 2, Rated, Prizes!)(持续更新)
    Preface猜结论场,很久没打比赛了然后赛前和ztc吹牛说每次隔一段时间来打CF就会有强运加持结果好家伙BCE全部秒出结论(而且我比赛时都证不来),而且写的A~E都是一发入魂凭借这......
  • (补12月19)ORM查询优化、ORM事物操作、ORM常用字段参数、Ajax请求、Content-Type
    ORM查询优化、ORM事物操作、ORM常用字段参数、Ajax请求、Content-TypeQ查询进阶操作先产生一个对象q_obj=Q()修改默认对象q_obj.connector='or'添加查询条件,可......
  • TypeDB Forces 2023 (Div. 1 + Div. 2)
    题目链接A核心思路直接把其中一个数置为1就好了。//Problem:A.ExponentialEquation//Contest:Codeforces-TypeDBForces2023(Div.1+Div.2,Rated,Priz......
  • typescript入门
    js数据类型:7种原始类型:BooleanNullUndefinedNumberBigintStringSymbolObject类型letisDone:boolean=falseletnum:number=10letstr:string=`num=......