Questions tagged [methodinfo]

154 questions
68
votes
6 answers

Can you get a Func (or similar) from a MethodInfo object?

I realize that, generally speaking, there are performance implications of using reflection. (I myself am not a fan of reflection at all, actually; this is a purely academic question.) Suppose there exists some class that looks like this: public…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
61
votes
3 answers

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…
Dead account
  • 19,587
  • 13
  • 52
  • 82
47
votes
2 answers

How to pass a parameter as a reference with MethodInfo.Invoke

How can I pass a parameter as a reference with MethodInfo.Invoke? This is the method I want to call: private static bool test(string str, out byte[] byt) I tried this but I failed: byte[] rawAsm = new byte[]{}; MethodInfo _lf =…
method
  • 1,369
  • 3
  • 16
  • 29
45
votes
2 answers

How can I create an Action delegate from MethodInfo?

I want to get an action delegate from a MethodInfo object. Is this possible?
mokaymakci
  • 1,373
  • 1
  • 18
  • 30
45
votes
3 answers

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

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…
Zakharia Stanley
  • 1,196
  • 1
  • 9
  • 10
36
votes
7 answers

get methodinfo from a method reference C#

We can use a C# typeof keyword when we want to get Type instance for specified type. But what can I use if I want to get MethodInfo of a method by it's reference? For example I have a simple console app. It contains Program.Main method. I want to…
Bogdan Verbenets
  • 25,686
  • 13
  • 66
  • 119
34
votes
3 answers

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

I have a MethodInfo of an interface method and Type of a class that implements the interface. I want to find the MethodInfo of the class method that implements the interface method. The simple method.GetBaseDefinition() does not work with interface…
Krzysztof Kozmic
  • 27,267
  • 12
  • 73
  • 115
32
votes
3 answers

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…
chakrit
  • 61,017
  • 25
  • 133
  • 162
32
votes
2 answers

Creating delegate from MethodInfo

I am currently running into an issue trying to create delegates from MethodInfo. My overall goal is to look through the methods in a class and create delegates for ones marked with a certain attribute. I am trying to use CreateDelegate but I am…
thecaptain0220
  • 2,098
  • 5
  • 30
  • 51
26
votes
6 answers

Finding the hosting PropertyInfo from the MethodInfo of getter/setter

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…
Mr. Lame
  • 1,247
  • 1
  • 17
  • 27
26
votes
1 answer

Getting a return value from a methodInfo.invoke

How do I get a return value (int) from a methodInfo.invoke? What makes it difficult for me is the fact that I use a string variable to call the method. Check the example below: if (Convert.ToBoolean(getParameterFromXML("issue", k, 1)) == true) { …
user1688175
22
votes
2 answers

The uncatchable exception, pt 2

Update: I've filed a bug report on Microsoft Connect: https://connect.microsoft.com/VisualStudio/feedback/details/568271/debugger-halting-on-exception-thrown-inside-methodinfo-invoke#details If you can reproduce this problem on your machine, please…
devios1
  • 36,899
  • 45
  • 162
  • 260
19
votes
3 answers

How to call a generic extension method with reflection?

I wrote the extension method GenericExtension. Now I want to call the extension method Extension. But the value of methodInfo is always null. public static class MyClass { public static void GenericExtension(this Form a, string b) where T :…
David
  • 4,027
  • 10
  • 50
  • 102
18
votes
4 answers

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 =…
Ralph Shillington
  • 20,718
  • 23
  • 91
  • 154
18
votes
5 answers

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…
Dror Helper
  • 30,292
  • 15
  • 80
  • 129
1
2 3
10 11