methodinfo

Getting a return value from a methodInfo.invoke

会有一股神秘感。 提交于 2019-12-08 14:47:01
问题 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) { m = k + 1; MethodInfo methodInfo = typeof(frmDetails).GetMethod("Issue" + m); methodInfo.Invoke(this, Parameters); } What can I do? Any help would be appreciated. 回答1: When I read this you get the result of the method back from the Invoke-call. It is

Parsing to primitive types, based on user input in c#

安稳与你 提交于 2019-12-08 03:39:50
问题 My code to do this uses reflection and strings that I give it, instead of user input. Ultimately I would like the user to be able to say "float" "2.0" and have the computer say, yeah, that's a float, or "bool" "abc" to which the computer would say, that's no boolean it's heard of. It would be simple enough to take the user input and convert it to a primitive type name, like "string" to "System.String", "float" to "System.Single", etc. (although if you know of a function to do that, that would

How to find an overloaded method by reflection

女生的网名这么多〃 提交于 2019-12-07 06:34:58
问题 This is a question associated with another question I asked before. I have an overloaded method: public void Add<T>(SomeType<T> some) { } public void Add<T>(AnotherType<T> another) { } How can I find each method by reflection? e.g. How can I get the Add<T>(SomeType<T> some) method by reflection? Can you help me please? Thanks in advance. 回答1: The trick here is describing that you want the parameter to be SomeType<T> , where T is the generic type of the Add method. Other than that, it's just

Why does VS2010 always break on exception from MethodInfo.Invoke?

一个人想着一个人 提交于 2019-12-05 08:54:54
I have a try/catch around a MethodInfo.Invoke(o,null), and VS2010 is set to never break on Exceptions, but unfortunately the debugger continues to break inside the Invoked method. The method is static, and I've got the Phone Developer Beta installed. Is this a bug or developer error? Thx!! exsulto Yes, with every exception check-box is un-checked it breaks on only these Invoke exceptions. All the other exceptions work fine. The great news is that an anonymous genius gave me a work-around: delegate void VoidTest(); VoidTest test = (VoidTest)Delegate.CreateDelegate(typeof(VoidTest), o, method

How to find an overloaded method by reflection

北城余情 提交于 2019-12-05 08:51:43
This is a question associated with another question I asked before . I have an overloaded method: public void Add<T>(SomeType<T> some) { } public void Add<T>(AnotherType<T> another) { } How can I find each method by reflection? e.g. How can I get the Add<T>(SomeType<T> some) method by reflection? Can you help me please? Thanks in advance. The trick here is describing that you want the parameter to be SomeType<T> , where T is the generic type of the Add method. Other than that, it's just about using standard reflection, like CastroXXL suggested in his answer. Here's how I did it: var

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

The uncatchable exception, pt 2

随声附和 提交于 2019-12-03 16:11:13
问题 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 upvote the bug so it can be fixed! Ok I've done some testing and I've reduced the problem to something very simple: i. Create a method in a new class that throws an exception: public class Class1 { public void CallMe() { string blah = null; blah

The uncatchable exception, pt 2

风流意气都作罢 提交于 2019-12-03 05:38:11
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 upvote the bug so it can be fixed! Ok I've done some testing and I've reduced the problem to something very simple: i. Create a method in a new class that throws an exception: public class Class1 { public void CallMe() { string blah = null; blah.ToLower(); } } ii. Create a MethodInfo that points to this method somewhere else: Type class1 = typeof(

Why is a member of base class different from the same member in derived class?

不羁岁月 提交于 2019-12-01 20:54:20
问题 This is a followup to this question: Lambda expression not returning expected MemberInfo class Human { public string name { get; set; } } class Man : Human { } var m1 = typeof(Human).GetProperty("name"); var m2 = typeof(Man).GetProperty("name"); //m1 != m2 why? The same holds for MethodInfo s. I can understand there has to be a difference when Human is an interface, or when name of Human is abstract/virtual. But why is it so for sealed types? Isn't name of Man exactly name of Human ?

System.Reflection.MethodInfo.Invoke and multiple threads

陌路散爱 提交于 2019-12-01 19:33:09
Hi how do i call System.Reflection.MethodInfo.Invoke() with paramters with threads. For instance.. Say I have a method that allows you to pass in a string that represents a class name and calls corresponding class method dynamically , now i want to call this Methodinfo.invoke with threads ,I have no idea how to do this since i am calling invoke with paramter . Code snippet given meblow . Thank you for your help Type classType = objAssembly.GetType("MyClassName"); object obj = Activator.CreateInstance(classType) bject[] _objval = new object[3]; object[] parameters = new object[] { _objval };