methodinfo

Traverse a c# method and anazlye the method body

半城伤御伤魂 提交于 2019-12-01 16:08:02
Whats the easiest way to traverse a methodinfo in c#? I want to traverse the method body and find field-references and such and retrieves the types. In System.Reflection there is: mi.GetMethodBody().GetILAsByteArray() which is kinda low-level and would require "some" work before I would be able to traverse the body. I know Cecil exists, but there's a problem in loading an in memory assembly with cecil. The assembly i'm working with is't always "on disk" it can be an in memory assembly compiled from eg. Boo, and I wan't a clean solution without writing the assembly temporary to disk. What other

How to determine if the MethodInfo is an override of the base method

走远了吗. 提交于 2019-11-29 13:15:55
I'm trying to determine if the MethodInfo object that I get from a GetMethod call on a type instance is implemented by the type or by it's base. For example: Foo foo = new Foo(); MethodInfo methodInfo = foo.GetType().GetMethod("ToString",BindingFlags|Instance); the ToString method may be implemented in the Foo class or not. I want to know if I'm getting the foo implementation? Related question Is it possible to tell if a .NET virtual method has been overriden in a derived class? Check its DeclaringType property. if (methodInfo.DeclaringType == typeof(Foo)) { // ... } Instead of using

How to determine if ParameterInfo is of generic type?

偶尔善良 提交于 2019-11-29 10:25:41
I have a MethodInfo of a GenericMethodDefinition. Such as: CallMethod<T>(T arg, string arg2) . The GetParameters() method will give me two ParameterInfo objects, the first of which is generic, the second of which is not. How can I get ParameterInfo to tell me it is generic? What about if it has constraints? Check ParameterType.IsGenericParameter . You may also want to check ContainsGenericParameters , which will be true for something like MyMethod<T>(List<T> param) . (Even though List<> isn't a generic parameter) If IsGenericParameter is true, you can also call GetGenericParameterConstraints()

Retrieving the name of the invoked method executed in a Func

Deadly 提交于 2019-11-29 08:44:18
I would like to get the name of the method that is being delegated as a Func. Func<MyObject, object> func = x => x.DoSomeMethod(); string name = ExtractMethodName(func); // should equal "DoSomeMethod" How can I achieve this? -- For bragging rights -- Make ExtractMethodName also work with a property invocation, having it return the property name in that instance. eg. Func<MyObject, object> func = x => x.Property; string name = ExtractMethodName(func); // should equal "Property" Look Ma! No expression trees! Here's a quick, dirty and implementation-specific version that grabs the metadata token

Invoke method by MethodInfo

纵然是瞬间 提交于 2019-11-29 07:03:04
I want to invoke methods with a certain attribute. So I'm cycling through all the assemblies and all methods to find the methods with my attribute. Works fine, but how do I invoke a certain method when I only got it's MethodInfo. AppDomain app = AppDomain.CurrentDomain; Assembly[] ass = app.GetAssemblies(); Type[] types; foreach (Assembly a in ass) { types = a.GetTypes(); foreach (Type t in types) { MethodInfo[] methods = t.GetMethods(); foreach (MethodInfo method in methods) { // Invoke a certain method } } } The problem is that I don't know the instance of the class that contains that

How can I get fields used in a method (.NET)?

Deadly 提交于 2019-11-29 02:34:14
In .NET, using reflection how can I get class variables that are used in a method? Ex: class A { UltraClass B = new(..); SupaClass C = new(..); void M1() { B.xyz(); // it can be a method call int a = C.a; // a variable access } } Note: GetClassVariablesInMethod(M1 MethodInfo) returns B and C variables. By variables I mean Value and/or Type and Constructor Parameters of that specific variable. There's a lot of different answers, but as not a single one appeals to me, here's mine. It's using my Reflection based IL reader . Here's a method retrieving all the fields used by a method: static

Builds a Delegate from MethodInfo?

谁说胖子不能爱 提交于 2019-11-28 18:53:58
问题 After googling and landing on SO and having read this other question Is it possible to build a correct Delegate from a MethodInfo if you didn't know the number or types of parameters at compile time? More on this: can this be done elegantly without the use of Reflection.Emit or type builders? This is sorta a bummer for me because Delegate.CreateDelegate requires me to specify the correct Delegate type as the first parameter or else it would throw exceptions or invoke an incorrect method. I'm

How to create a delegate from a MethodInfo when method signature cannot be known beforehand?

拥有回忆 提交于 2019-11-28 16:40:11
I need a method that takes a MethodInfo instance representing a non-generic static method with arbitrary signature and returns a delegate bound to that method that could later be invoked using Delegate.DynamicInvoke method. My first naïve try looked like this: using System; using System.Reflection; class Program { static void Main() { var method = CreateDelegate(typeof (Console).GetMethod("WriteLine", new[] {typeof (string)})); method.DynamicInvoke("Hello world"); } static Delegate CreateDelegate(MethodInfo method) { if (method == null) { throw new ArgumentNullException("method"); } if (

Checking a MethodInfo against a delegate

蹲街弑〆低调 提交于 2019-11-28 09:22:12
How can I determine if a MethodInfo fits a distinct Delegate Type? bool IsMyDelegate(MethodInfo method); Edit: I'm given a MethodInfo object and want to know if it fits the delegate interface. Apart from the obvious private bool IsValidationDelegate(MethodInfo method) { var result = false; var parameters = method.GetParameters(); if (parameters.Length == 2 && parameters[0].ParameterType == typeof(MyObject1) && parameters[1].ParameterType == typeof(MyObject2) && method.ReturnType == typeof(bool)) { result = true; } else { m_Log.Error("Validator:IsValidationDelegate", "Method [...] is not a

Using reflection to check if a method is “Extension Method”

家住魔仙堡 提交于 2019-11-28 09:06:21
As part of my application I have a function that receives a MethodInfo and need to do specific operations on it depending if that method is "Extension Method". I've checked the MethodInfo class and I could not find any IsExtension property or flag that shows that the method is extension. Does anyone knows how can I find that from the method's MethodInfo? Brian Based on F# extension methods in C# it seems there is an attribute on the compiled form. So see if the method has this attribute: http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.extensionattribute.aspx You can