MethodBase.GetCurrentMethod 方法
如果当前正在执行的方法定义泛型类型上MethodInfo返回GetCurrentMethod通过泛型类型定义 (即,MethodInfo.ContainsGenericParameters返回true)。 因此,它不反映时调用该方法时所使用的类型自变量。 例如,如果方法M()泛型类型上定义C<T>(C(Of T)在 Visual Basic 中),和GetCurrentMethod从调用C<string>.M(),然后GetCurrentMethod返回C<T>.M()(C(Of T).M()在 Visual Basic 中)。
如果当前正在执行的方法是泛型方法,GetCurrentMethod返回泛型方法定义。 如果在泛型类型上定义的泛型方法MethodInfo从泛型类型定义中获取。
下面的示例定义两种类型。 第一种是一个非泛型类, TestClass,包括构造函数,一个名为方法GetValue,和一个名为的读写属性GetValue。 第二个是名为一个泛型类TestClass<T>,包含一个构造函数,GetValue方法和泛型方法, ConvertValue<Y>。 每个构造函数、 方法和属性访问器包括对的调用GetCurrentMethod方法。
------------------------------------------------------
using System;
using System.Reflection; public class Example
{
public static void Main()
{
var t = new TestClass();
Console.WriteLine(t.GetValue());
t.Value = ;
Console.WriteLine(t.Value);
Console.WriteLine(); var tg =new Test<int>();
Console.WriteLine(tg.GetValue());
var b = tg.ConvertValue<Byte>();
Console.WriteLine("{0} -> {1} ({2})", tg.GetValue().GetType().Name,
b, b.GetType().Name);
}
} public class TestClass
{
private Nullable<int> _value; public TestClass()
{
MethodBase m = MethodBase.GetCurrentMethod();
Console.WriteLine("Executing {0}.{1}",
m.ReflectedType.Name, m.Name);
} public TestClass(int value)
{
MethodBase m = MethodBase.GetCurrentMethod();
Console.WriteLine("Executing {0}.{1}",
m.ReflectedType.Name, m.Name);
_value = value;
} public int Value
{
get {
MethodBase m = MethodBase.GetCurrentMethod();
Console.WriteLine("Executing {0}.{1}",
m.ReflectedType.Name, m.Name);
return _value.GetValueOrDefault();
}
set {
MethodBase m = MethodBase.GetCurrentMethod();
Console.WriteLine("Executing {0}.{1}",
m.ReflectedType.Name, m.Name);
_value = value;
}
} public int GetValue()
{
MethodBase m = MethodBase.GetCurrentMethod();
Console.WriteLine("Executing {0}.{1}",
m.ReflectedType.Name, m.Name);
return this.Value;
}
} public class Test<T>
{
private T value; public Test(T value)
{
MethodBase m = MethodBase.GetCurrentMethod();
Console.WriteLine("Executing {0}.{1}",
m.ReflectedType.Name, m.Name);
this.value = value;
} public T GetValue()
{
MethodBase m = MethodBase.GetCurrentMethod();
Console.WriteLine("Executing {0}.{1}",
m.ReflectedType.Name, m.Name);
return value;
} public Y ConvertValue<Y>()
{
MethodBase m = MethodBase.GetCurrentMethod();
Console.WriteLine("Executing {0}.{1}",
m.ReflectedType.Name, m.Name);
Console.Write(" Generic method: {0}, definition: {1}, Args: ",
m.IsGenericMethod, m.IsGenericMethodDefinition);
if (m.IsGenericMethod) {
foreach (var arg in m.GetGenericArguments())
Console.Write("{0} ", arg.Name);
}
Console.WriteLine();
try {
return (Y) Convert.ChangeType(value, typeof(Y));
}
catch (OverflowException) {
throw;
}
catch (InvalidCastException) {
throw;
}
}
}
// The example displays the following output:
// Executing TestClass..ctor
// Executing TestClass.GetValue
// Executing TestClass.get_Value
// 0
// Executing TestClass.set_Value
// Executing TestClass.get_Value
// 10
//
// Executing Test`1..ctor
// Executing Test`1.GetValue
// 200
// Executing Test`1.ConvertValue
// Generic method: True, definition: True, Args: Y
// Executing Test`1.GetValue
// Int32 -> 200 (Byte)
MethodBase.GetCurrentMethod 方法的更多相关文章
- msdn 中MethodBase.Invoke 方法 介绍中的坑
模块开发总结: c#动态调用webservices 来自网络及使用心得. msdn: MethodBase.Invoke 方法 (Object, Object[]) 使用指定的参数调用当前实例所表示的 ...
- 日志系统实战(一)—AOP静态注入
背景 近期在写日志系统,需要在运行时在函数内注入日志记录,并附带函数信息,这时就想到用Aop注入的方式. AOP分动态注入和静态注入两种注入的方式. 动态注入方式 利用Remoting的Context ...
- .NET 中获取调用方法名
在写记录日志功能时,需要记录日志调用方所在的模块名.命名空间名.类名以及方法名,想到使用的是反射(涉及到反射请注意性能),但具体是哪一块儿还不了解,于是搜索,整理如下: 需要添加相应的命名空间: us ...
- C#的扩展方法解析
在使用面向对象的语言进行项目开发的过程中,较多的会使用到“继承”的特性,但是并非所有的场景都适合使用“继承”特性,在设计模式的一些基本原则中也有较多的提到. 继承的有关特性的使用所带来的问题:对象的继 ...
- log4net位置与使用方法
<log4net> <appender name="RollingLogFileAppender" type="log4net.Appender.Rol ...
- [No000085]C#反射Demo,通过类名(String)创建类实例,通过方法名(String)调用方法
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...
- Log4Net使用方法
项目里都会用到日志记录..特别是在本地跑的欢畅..一上服务器就嗝屁的时候..日志会帮你大忙.. Log4net算是一种应用的最广泛的日志记录方式了..下面来简略的说下他的用法.. 先下载log4net ...
- C#中log4net使用方法(一)
Log4net是一个第三方开源组件,它设计的主要目的是组合,生成日志信息,同时将配置保存到各种存储介质或者展现平台中,在实际项目中,Log4net可以保存系统运行情况,可以在系统出现异常时,根据保存的 ...
- Asp.net .net(C#) 获取当前命名空间,类名,方法名的方法
public static string GetMethodInfo() { string str = ""; //取得当前方法命名空间 str += & ...
随机推荐
- 一、JsonTree
一.JsonTree [ {"id":"4","pid":"1","name":"大家电& ...
- Nginx优化_数据包头部信息过大问题
如果客户端发出请求的URL头部信息过大,网站将不能及时响应,并通过状态码414报错. <center><h1>414 Request-URI Too Large</h1& ...
- 企业微信的corpsecret在哪里?
问题: 查看“企业微信”的官方开发文档,在“获取access_token”部分提到,使用GET请求方法,访问 https://qyapi.weixin.qq.com/cgi-bin/gettoke ...
- JavaScript设计模式 样例三 —— 装饰模式
装饰模式(Decorator Pattern): 定义:在不改变原对象的情况下,动态的给对象添加一些额外的职责.就功能而言,装饰模式相比生成子类更为灵活. 目的:把类的核心职责和装饰功能区分开.可以去 ...
- Python核心技术与实战——三|字符串
一.字符串基础 Python的字符串支持单引号('').双引号("")和三引号之中('''....'''和"""...""&quo ...
- pandas处理字符串
# pandas 字符串的处理 # 前面已经学习了字符串的处理函数 # df["bWendu"].str.replace("℃","").a ...
- eclipse把函数内容折叠的方法
eclipse 将方法折叠要先启动折叠功能启用方法:Ctrl+ / (小键盘) 或者:右键点击行号左边的空白,弹出的选项中,选择“Folding”下的“Enable Folding”这样启动foldi ...
- Python---常用的内置模块
#fsum() 对整个序列求和 返回浮点数 print(math.fsum([1,4.5,5,7])) #sum() python内置求和 print(sum([1,4,5,7])) print( ...
- heroinfo_set.all 函数
如果是 一对多 关系 即使用 heroinfo_set.all 此时关联字段类型通用,即上边的字段通用,但是需要添加many=True的参数heroinfo_set = serializers.Pr ...
- echart--如何将echart的配置项,放到webpack中(CHARTTEMPLATE时)
1.假如,我们已经写好了组件,我们需要把它放入到一个环境中去 2.首先在index.html中,我们需要写一个dom结构 3.新建一个,chart.js文件(这个里面放组件的代码) 1>开始创建 ...