methodinfo

Finding the hosting PropertyInfo from the MethodInfo of getter/setter

Hi, I do some type analysis in runtime using Reflection. If I have a MethodInfo instance, how can I figure out if this is a "real" method or is a getter/setter method of a property? And if it is a property, how can I find the its hosting PropertyInfo back? ...

C# referencing desired overloaded generic method

given public Class Example { public static void Foo< T>(int ID){} public static void Foo< T,U>(int ID){} } Questions: 1) Is it correct to call this an "overloaded generic method"? 2) How can either method be specified in creation of a MethodInfo object? Type exampleType = Type.GetType("fullyqualifiednameOfExample, namespaceOfE...

Using reflection to check if a method is "Extension Method"

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 m...

Invoke method by MethodInfo

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 (Assemb...

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

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 t...

C# MethodInfo getReturnType

I created an instance of MethodInfo: MethodInfo theMethod = typeof(Reciever).GetMethod("methodName", parameterTypes); Now I want to know if theMethod's return type is void. How? ...

How to get MethodInfo of interface method, having method info of class method implementing the interface?

I have a MethodInto of interface method and type of a class that implements the interface. I want to find MethodInfo of the class method that implements the interface method. The simple method.GetBaseDefinition() does not work with interface methods. Lookup by name won't work either, because when implementing interface method explicitly...

Builds a Delegate from MethodInfo ?

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 ...

Retrieving the name of the the invoked method executed in a Func

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 p...

MethodInfo.Invoke sometimes returns null and sometimes returns value

Hi all, I'm working on an asp.net MVC application. I have a class that wraps a repository that fetches data from a db using simple linq statement. I've written a decorator class to add caching logic (using caching application block). since I have several methods that I want to decorate, and the logic is all the same for each one (chec...

get expression of method in Expression tree

i want to create the following query in expression trees: var test = from datarow in tempResults where datarow.Field<String>("ColumnName") == "Column" select datarow; How do i create the expression : datarow.Field("ColumnName")? i tried everything, i even got stuck on getting the MethodInfo of Fi...

Fast Access to the type/method/... that holds an Attribute in C#

I have made a custom Attribute here named AAtribute, and for instance a class called B where one or more methods use the attribute. Is it possible to get the MethodInfo of the method that holds the attribute (in this case BMethod1) as (one of) it's attributes without walking through the whole assembly and looking at all defined methods f...

AccessViolationException on MethodInfo.ToString()

Getting an AccessViolationException when calling ToString() on my MemberInfo. I suspect something is holding onto the Type information on one of my methods parameters and causing the error, could this be possible? What usually causes the AccessViolationExpection? I'm getting looping through the Public Static methods a Type and am gettin...

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

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 varia...

Translating a MethodInfo object obtained from an interface type, to the corresponding MethodInfo object on an implementing type in C#?

The question I have is this: If I have the MethodInfo object, for a method, obtained from an interface type, and I also have the Type object for a class that implements this interface, but it implements the said method with an explicit implementation, how do I correctly obtain the corresponding MethodInfo object for the implementing meth...

How to get MethodInfo of a generic method on a non generic .NET type?

Dear ladies and sirs. I have this little problem, that I cannot figure out which arguments to pass to Type.GetMethod in order to get back the MethodInfo of a generic method on a non generic type. Specifically, I have this type definition: public static class A { public static B F<T>(bool dummy) { } public static B F<T>(IEnumera...

Asynchronous runtime method invocation

I am loading some assemblies at run time and invoking methods on them using Reflections (MethodInfo.Invoke). Now I want to make these calls asynchronous. So I am thinking of using Delegate.BeginInvoke(). But I am not sure how to create delegate instance by providing function name at run-time. (All examples I see have delegate instance t...

How to test if MethodInfo.ReturnType is type of System.Void?

Using reflection to obtain a MethodInfo, I want to test if the type returned is typeof System.Void. Testing if it is System.Int32 works fine myMethodInfo.ReturnType == typeof(System.Int32) but myMethodInfo.ReturnType == typeof(System.Void) does not compile? At present Im testing if the string representation of the name is "Syste...

How do I determine which interface is referenced by an explicitly-implemented MethodInfo object?

I have a MethodInfo object that represents an explicitly-implemented interface method, as follows. MethodInfo GetMethod() { return typeof(List<>).GetMethod( "System.Collections.IEnumerable.GetEnumerator", BindingFlags.Instance | BindingFlags.NonPublic); } How do I query this MethodInfo object to obtain the interfac...

MethodInfo, CreateDelegate and Generic Methods

Thanks to Jon Skeet's answer in this question I have the following working: public delegate BaseItem GetItemDelegate(Guid itemID); public static class Lists { public static GetItemDelegate GetItemDelegateForType(Type derivedType) { MethodInfo method = typeof(Lists).GetMethod("GetItem"); method = method.MakeGenericMethod(new...