methodbase

How to get a MethodBase object for a method?

落爺英雄遲暮 提交于 2019-12-21 11:58:29
问题 I am trying to work with the class found in this post but it needs a MethodBase to run. I read What is the fastest way to get a MethodBase object? but i couldn't get any solution to work. What i need to do is to get the MethodBase object from a function. For example getting the MethodBase for the static function WriteLine() of the class Console or getting the MethodBase for the non-static function Add() of a List<>. Thanks for your help! 回答1: Method 1 You can use reflection directly:

How to distinguish MethodBase in generics

筅森魡賤 提交于 2019-12-20 03:19:11
问题 I have a cache based on Dictionary<MethodBase, string> The key is rendered from MethodBase.GetCurrentMethod. Everything worked fine until methods were explicitly declared. But one day it is appeared that: Method1<T>(string value) Makes same entry in Dictionary when T gets absolutely different types. So my question is about better way to cache value for generic methods. (Of course I can provide wrapper that provides GetCache and equality encountered generic types, but this way doesn't look

MoveNext instead of actual method/task name

喜欢而已 提交于 2019-12-17 16:04:23
问题 Using log4net declared as: private readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType()); In an async method or task, like this one: public async void CheckSomething() { log.Info(null); //.... } logs MoveNext instead of CheckSomething . Any idea how to make it log an actual method name? 回答1: All async methods are rewritten into a state machine to satisfy potential await values within the method. The final method in which the code lives is the MoveNext method

MethodBase Object of async function

断了今生、忘了曾经 提交于 2019-12-12 01:54:40
问题 in my application i have a little action log. In there I save the last few calls of certain methods in my application like this: saveLastAction(MethodBase.GetCurrentMethod(), getCurrentStatus(), item, /*loadFresh*/ false); It allows me to navigate and refresh through my last actions public void Refresh(bool loadFresh = true) { if (!isInitialized) return; try { lastStatus = getCurrentStatus(); var parameters = lastActionsParameters.Pop(); var method = lastActions.Pop(); //Set the load Fresh

How to get a MethodBase object for a method?

北城余情 提交于 2019-12-04 03:17:27
I am trying to work with the class found in this post but it needs a MethodBase to run. I read What is the fastest way to get a MethodBase object? but i couldn't get any solution to work. What i need to do is to get the MethodBase object from a function. For example getting the MethodBase for the static function WriteLine() of the class Console or getting the MethodBase for the non-static function Add() of a List<>. Thanks for your help! Method 1 You can use reflection directly: MethodBase writeLine = typeof(Console).GetMethod( "WriteLine", // Name of the method BindingFlags.Static |

How to read a method body with reflection

喜你入骨 提交于 2019-12-04 02:21:27
Is it possible to find out anything about a Method body with reflection? How? You can use MethodInfo.GetMethodBody . That provides you access to anything you want... if you're happy to work through the IL etc yourself. It's possible that the Mono Cecil library will provide more help - I haven't used it myself. Talking of Mono.Cecil, it will give you access to the method body in a way that will look very familiar if you have ever peeked into a .NET assembly with ILDASM. 来源: https://stackoverflow.com/questions/4986563/how-to-read-a-method-body-with-reflection

How to distinguish MethodBase in generics

旧城冷巷雨未停 提交于 2019-12-02 01:34:47
I have a cache based on Dictionary<MethodBase, string> The key is rendered from MethodBase.GetCurrentMethod. Everything worked fine until methods were explicitly declared. But one day it is appeared that: Method1<T>(string value) Makes same entry in Dictionary when T gets absolutely different types. So my question is about better way to cache value for generic methods. (Of course I can provide wrapper that provides GetCache and equality encountered generic types, but this way doesn't look elegant). Update Here what I exactly want: static Dictionary<MethodBase, string> cache = new Dictionary

MoveNext instead of actual method/task name

时光怂恿深爱的人放手 提交于 2019-11-27 21:16:41
Using log4net declared as: private readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType()); In an async method or task, like this one: public async void CheckSomething() { log.Info(null); //.... } logs MoveNext instead of CheckSomething . Any idea how to make it log an actual method name? JaredPar All async methods are rewritten into a state machine to satisfy potential await values within the method. The final method in which the code lives is the MoveNext method which is what log4net is reporting. There is really no good way at runtime to transition from