methodinfo

What is the most efficient way to ask a MethodInfo how many parameters it takes?

只愿长相守 提交于 2019-12-23 08:49:25
问题 What is the most efficient way to ask a MethodInfo if it accepts parameters and, if so, how many? My current solutions would be: methodInfo.GetParameters().Any() and methodInfo.GetParameters().Count() . Is this the most efficient way? Since I don't actually need any of the ParameterInfo objects, is there a way to do this without a call to GetParameters() ? 回答1: The two you listed are for LINQ. Any() returns bool - stating that there is at least one. Count() is used any on IEnumerable<T> .

System.Reflection.MethodInfo.Invoke and multiple threads

痞子三分冷 提交于 2019-12-20 01:03:50
问题 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

Traverse a c# method and anazlye the method body

北战南征 提交于 2019-12-19 16:36:49
问题 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

How to determine if ParameterInfo is of generic type?

孤人 提交于 2019-12-18 05:56:20
问题 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? 回答1: 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<>

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

时光总嘲笑我的痴心妄想 提交于 2019-12-17 21:49:20
问题 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

Finding the hosting PropertyInfo from the MethodInfo of getter/setter

无人久伴 提交于 2019-12-17 16:15:25
问题 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? 回答1: Ecma 335 specifies (but does not demand) that compilers use the get_/set_ prefixes (chapter 22.28). I don't know any language that breaks that recommendation. Making it easy: public static PropertyInfo GetPropFromMethod(Type t, MethodInfo

referencing desired overloaded generic method

依然范特西╮ 提交于 2019-12-12 09:33:32
问题 given public Class Example { public static void Foo< T>(int ID){} public static void Foo< T,U>(int ID){} } Questions: Is it correct to call this an "overloaded generic method"? How can either method be specified in creation of a MethodInfo object? Type exampleType = Type.GetType("fullyqualifiednameOfExample, namespaceOfExample"); MethodInfo mi = exampleType.GetMethod("Foo", BindingFlags.Public|BindingFlags.Static, null, new Type[] {typeof(Type), typeof(Type) }, null); argument 4 causes the

MethodInfo and Delegates

好久不见. 提交于 2019-12-11 11:13:26
问题 I am using dotnet 2.0 I know that with an EventInfo value, you can loop through an Assembly's Types and find all the methods that match the EventInfo delegate definition ( EventInfo.EventHandlerType ) Is there a way to find out what available delegates a given MethodInfo can be assigned in the Delegate.CreateDelegate() function without first looping through all the referenced assemblies to find all the Delegate definitions. Or am I stuck doing the following: public bool

Can the .NET MethodInfo cache be cleared or disabled?

强颜欢笑 提交于 2019-12-11 03:04:16
问题 Per MSDN, calling Type.GetMethods() stores reflected method information in a MemberInfo cache so the expensive operation doesn't have to be performed again. I have an application that scans assemblies/types, looking for methods that match a given specification. The problem is that memory consumption increases significantly (especially with large numbers of referenced assemblies) since .NET hangs onto the method metadata. Is there any way to clear or disable this MemberInfo cache? 回答1: I don't

Lambda expressions <T, Func<TIN,TOUT>> and MethodInfo

时光总嘲笑我的痴心妄想 提交于 2019-12-08 16:43:58
问题 While migrating a project from VS2010 to VS2012, I ran into the following problem. The project is using Reflection a lot, and in order to get the MethodInfo from an interface the following code was placed: Expression<Func<ITest, Func<ServiceRequest, ServiceResponse>>> expression = scv => scv.Get; UnaryExpression unaryExpression = expression.Body as UnaryExpression; MethodCallExpression methodCallExpression = unaryExpression.Operand as MethodCallExpression; ConstantExpression