C# - 反射与编译
编译
首先了解下,如何区分编译生成的 .dll的版本
方法1:ILSpy反编译工具
通过 assembly属性,release版本没有或仅有如下一种属性
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
而 debug版本,属性较多,示例
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations |
DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
具体参见:https://blog.csdn.net/WPwalter/article/details/80933080
方法2:代码检测
public static class Utils
{
//新增扩展方法
public static T GetCustomAttribute<T>(this ICustomAttributeProvider provider)
where T : Attribute
{
var attributes = provider.GetCustomAttributes(typeof(T), false);
return attributes.Length > 0 ? attributes[0] as T : default(T);
} public enum DllMode { Debug = 0, Release = 1 }; public static DllMode CheckDllMode_1(string _filePath)
{
var assembly = Assembly.LoadFile(_filePath);
var attributes = assembly.GetCustomAttributes(typeof(DebuggableAttribute), false);
if (attributes.Length > 0)
{
var debuggable = attributes[0] as DebuggableAttribute;
if (debuggable != null) {
return ((debuggable.DebuggingFlags & DebuggableAttribute.DebuggingModes.Default)
== DebuggableAttribute.DebuggingModes.Default)
? DllMode.Debug : DllMode.Release;
} else { return DllMode.Release; }
} else { return DllMode.Release; }
} public static DllMode CheckDllMode_2(string _filePath)
{
Assembly ass = Assembly.LoadFile(_filePath);
DebuggableAttribute att = ass.GetCustomAttribute<DebuggableAttribute>();
return (att != null && att.IsJITTrackingEnabled) ? DllMode.Debug : DllMode.Release;
} }
具体参见:https://www.oschina.net/code/snippet_12_8459
新增扩展方法时,若提示: 缺少编译器要求的成员“system.Runtime.CompilerServices.ExtensionAttribute..ctor”
解决方法,在当前.cs中添加
namespace System.Runtime.CompilerServices {
public class ExtensionAttribute : Attribute { }
}
反射
类Assembly中Load, LoadFrom, LoadFile方法比较
- http://www.cnblogs.com/liuzhendong/archive/2011/08/15/2139506.html
- http://www.cnblogs.com/xuqingfeng/archive/2012/05/22/assembly-load-loadfrom-loadfile-details.html
下面给出2种程序集加载方法
//方法1:直接从DLL路径加载 ok
assembly = Assembly.LoadFrom(assemblyPath); //方法2:先把DLL加载到内存,再从内存中加载 ok
using (FileStream fs = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read))
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bFile = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
assembly = Assembly.Load(bFile);
}
}
可以将程序集中定义的所有类型暂存备用,调用时指定程序集和方法名即可
// htTypes是Hashtable
foreach (Type tp in assembly.GetTypes()) {
htTypes.Add(tp.Name, tp);
} string className = "Calculator.Calculator"; //程序集.类名
string funName = "Add"; //方法名
if (TypeTest.htTypes.ContainsKey(className))
{
var tp = (Type)TypeTest.htTypes[className];
var func = tp.GetMethod(funName);
if (null != func)
{
func.Invoke(null, new object[] { ... });
}
}
对于设置或获取字段或属性的值,注意区分静态/非静态
非静态的实例字段或属性,GetValue和SetValue时,第一个参数务必传入实例对象
object obj = Activator.CreateInstance(type, true);
而静态的,直接送null即可。设置时,保险起见可以作类型转换
var v = Convert.ChangeType(value, tp.GetField/GetProperty(_name).FieldType/PropertyType);
注,待加载的程序集可以在配置文件中配置。
<configSections>
<section name="assembly" type="System.Configuration.NameValueSectionHandler"/>
</configSections> <!-- key为程序集名称,value表示是否要加载 -->
<assembly>
<add key="Calculator.dll" value="1"/>
<add key="crudHelper.dll" value="1"/>
</assembly>
代码中动态加载即可
NameValueCollection assemblyList = ConfigurationManager.GetSection("assembly") as NameValueCollection;
.Net框架提供了一个综合性方法:Type.InvokeMember,但是参数较多,慎用。
应用
[1]. 获取当前执行的方法的信息:2种
MethodBase method = MethodBase.GetCurrentMethod();
string tag = method.ReflectedType.FullName + "." + method.Name; //类名.方法名 StackTrace stackTrace = new StackTrace(true);
MethodBase method = stackTrace.GetFrame(0).GetMethod();
string codeDestination = method.DeclaringType.Name + "-" + method.Name; //类名.方法名
若要获取父方法的信息,使用 GetFrame(1) 即可。
[2]. 提取类实例字段名和字段值
public static string GetAllKeyValue<T>(T t, IDictionary<string, object> dic, bool removeEmptyVal)
{
if (t == null || dic == null) { return "t||dic null"; } try {
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
if (properties.Length <= 0) { return "properties 0"; } foreach (System.Reflection.PropertyInfo item in properties) {
string name = item.Name;
object value = item.GetValue(t, null); if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) {
if (removeEmptyVal && (value == null ||
(value is string && string.IsNullOrWhiteSpace(value.ToString())))) { /* not save empty value */ }
else { dic.Add(name, value); }
} else {
GetAllKeyValue(value, dic, removeEmptyVal);
}
}
} catch (Exception ex) {
return ex.Message + "||" + ex.StackTrace;
}
return string.Empty;
}
其中GetProperties()中的参数可以按需控制。
C# - 反射与编译的更多相关文章
- Java学习:注解,反射,动态编译
狂神声明 : 文章均为自己的学习笔记 , 转载一定注明出处 ; 编辑不易 , 防君子不防小人~共勉 ! Java学习:注解,反射,动态编译 Annotation 注解 什么是注解 ? Annotat ...
- 【UE4】基础概念——文件结构、类型、反射、编译、接口、垃圾回收、序列化
新标签打开或者下载看大图 思维导图 Engine Structure Pipeline Programming Pipeline Blueprint Pipeline
- 初识Scala反射
我们知道,scala编译器会将scala代码编译成JVM字节码,编译过程中会擦除scala特有的一些类型信息,在scala-2.10以前,只能在scala中利用java的反射机制,但是通过java反射 ...
- Java中的反射和注解
前言 在Java中,反射机制和注解机制一直是一个很重要的概念,那么他们其中的原理是怎么样呢,我们不仅仅需要会使用,更要知其然而之所以然. 目录 反射机制 反射如何使用 注解定义 注解机制原理 注解如何 ...
- 【C#进阶系列】23 程序集加载和反射
程序集加载 程序集加载,CLR使用System.Reflection.Assembly.Load静态方法,当然这个方法我们自己也可以显式调用. 还有一个Assembly.LoadFrom方法加载指定路 ...
- java 面向对象编程-- 第十三章 反射、类加载与垃圾回收
1.狭义JavaBean规范 Javabean必须包含一个无参数的public构造方法,方便通过反射的方式产生对象. 属性必须都是私有的. Javabean必须包含符合命名规范的get和set方法,以 ...
- 浅说Java中的反射机制(二)
写过一篇Java中的反射机制,不算是写,应该是抄了,因为那是别人写的,这一篇也是别人写的,摘抄如下: 引自于Java基础--反射机制的知识点梳理,作者醉眼识朦胧.(()为我手记) 什么是反射? 正常编 ...
- 初识Java反射
要详细的了解Java反射,就得要了解Java的类加载以及何为运行时动态加载等等概念.本文抛开其余概念,简单介绍Java反射,详细介绍会在以后有一个系统而全面的认识过后展开. 反射是Java被视为动态语 ...
- .NET Core单文件发布静态编译AOT CoreRT
.NET Core单文件发布静态编译AOT CoreRT,将.NET Core应用打包成一个可执行文件并包含运行时. 支持Windows, MacOS and Linux x64 w/ RyuJIT ...
随机推荐
- 2018上IEC计算机高级语言(C)作业 第2次作业
2018上IEC计算机高级语言(C)作业 第2次作业 一.例程调试(20分) 调试下面1个例程,各位同学调试用自己的学号模4加1序号的题,写清错误提示(截小图)及修改内容(10分); 说明:有可能没有 ...
- 2018.06.30 BZOJ1857: [Scoi2010]传送带(三分套三分)
1857: [Scoi2010]传送带 Time Limit: 1 Sec Memory Limit: 64 MB Description 在一个2维平面上有两条传送带,每一条传送带可以看成是一条线段 ...
- IntelliJ IDEA 2017版 开发SpringBoot的全局配置文件使用
一.全局配置文件 描述: Spring Boot项目使用一个全局的配置文件application.properties或者是application.yml,在resources目录下或者类路径 ...
- Linux各个版本资源下载
Linux系统各发行版镜像下载(持续更新) == Linux系统各发行版镜像下载(2014年10月更新),如果直接下载不了,请使用迅雷下载.并且注意,我的下载地址,在 迅雷 里才起作用. Linux ...
- 整理iOS9适配中出现的坑
一.NSAppTransportSecurity iOS9让所有的HTTP默认使用了HTTPS,原来的HTTP协议传输都改成TLS1.2协议进行传输.直接造成的情况就是App发请求的时候弹出网络无法连 ...
- day15(mysql 的多表查询,事务)
mysql之多表查询 1.合并结果集 作用:合并结果集就是把两个select语句查询的结果连接到一起! /*创建表t1*/ CREATE TABLE t1( a INT PRIMARY KEY , b ...
- day04(权限修饰符,内部类,局部内部类,匿名内部类)
权限修饰符, Public >protected >default > private public 公共权限 随便都可以访问 protected 子类可以访问权限 (子类 ...
- java学习之路-分享笔记20150327
---恢复内容开始--- 2个月间,断断续续学习了一段时间java平台相关知识,慢慢梳理出来一些枝枝叶叶,和大家交流下.3年前用java边看边做写了一个项目,所以对语法不是很关注.需要原文档的留邮箱吧 ...
- Android Sqlite 增删改查
模拟 查询所有数据,增加一条数据,修改某一条数据,删除某一条数据: SQLiteOpenHelper 帮助类的介绍: import android.content.Context; import an ...
- 基于tinyproxy搭建代理服务器
在我们实际的工作当中,经常会遇到这种情况,我们对线上服务器进行操作时是通过跳板机来进行的,出于安全性及投入资金来考虑非必要情况下除跳板机以外的服务器是没有内网ip的,所以当我们位于内网的服务器需要使用 ...