methodinfo

Handle Generic methods in a dictionary according to type

落花浮王杯 提交于 2021-02-07 18:27:26
问题 I have a generic method public delegate void Handler<T>(T val); I enable users to register to events and provide this delegate. I need to save the list of delegate according to their types. I tried saving in a dictionary of Type and object. when adding the method I cast it to a List<Handler<T>> according to the T. but then when an event occurred I do not have the T so cannot cast to the relevant list of generic handlers (I do have the Type but not the T) I solved this by saving methodInfo

How to get the correct MethodInfo for “Where” extension method

纵饮孤独 提交于 2021-01-28 08:00:56
问题 I am trying to get return the correct "Where" extension method using reflection, in order to build a custom Expression. I have tried several ways but the closest I get, throws an exception: "An unhandled exception of type 'System.Reflection.AmbiguousMatchException' occurred in mscorlib.dll" I know this is because there are two Where methods defined in the Enumrable class - but how can I return the Where method which using only just a predicate of Func<T, bool>. What I have at the moment is:

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

本小妞迷上赌 提交于 2021-01-27 17:05:22
问题 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 method in that class? The reason I need to do this is that implementing methods can have some attributes applied to them, and I need to find these through

One method to read parameters, properties and return types at runtime using C#

 ̄綄美尐妖づ 提交于 2020-01-17 06:55:56
问题 With continutation to my earlier thread Using reflection read properties of an object containing array of another object. I am hoping to make this wonderful method from EvgK a generic method that can be used in multiple places in my code base. public static void GetMyProperties(object obj) { List<MyPropertyInfo> oMyProp = new List<MyPropertyInfo>(); foreach (PropertyInfo pinfo in obj.GetType().GetProperties()) { if (!Helper.IsCustomType(pinfo.PropertyType)) { //add properties - name, value,

One method to read parameters, properties and return types at runtime using C#

耗尽温柔 提交于 2020-01-17 06:55:15
问题 With continutation to my earlier thread Using reflection read properties of an object containing array of another object. I am hoping to make this wonderful method from EvgK a generic method that can be used in multiple places in my code base. public static void GetMyProperties(object obj) { List<MyPropertyInfo> oMyProp = new List<MyPropertyInfo>(); foreach (PropertyInfo pinfo in obj.GetType().GetProperties()) { if (!Helper.IsCustomType(pinfo.PropertyType)) { //add properties - name, value,

Get methodinfo for Enumerable.DefaultIfEmpty

て烟熏妆下的殇ゞ 提交于 2020-01-14 10:33:12
问题 I'm building some Linq Expression and trying to get hold of MethodInfo for IEnumerable.DefaultIfEmpty (http://msdn.microsoft.com/en-us/library/bb360179.aspx). What seemed to be an easy task but I'm clueless to why it's not working. typeof(Enumerable).GetMethod("DefaultIfEmpty", new[] { typeof(IEnumerable<>) }); typeof(Enumerable).GetMethod("DefaultIfEmpty", new[] { typeof(IEnumerable<>).MakeGenericType(typeof(WorkitemListModel)) }); 回答1: Getting generic methods is a pain, to be honest. I don

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

半世苍凉 提交于 2020-01-13 09:59:28
问题 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!! 回答1: 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:

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

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-03 08:50:12
问题 This question already has answers here : Is there a good way of getting MethodInfo of open generic method? (3 answers) Closed 6 years ago . 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>(IEnumerable<T> arg) { ... } } I have tried several takes

get methodinfo from a method reference C#

为君一笑 提交于 2019-12-28 16:36:37
问题 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 get MethodInfo by using something like methodinfoof(Program.Main) . I have this problem because the method names might change, so I cannot just use Type.GetMethodInfo(string MethodName) for that. I have about 10 000 methods for which I would like to

Use Reflection to call generic method on object instance with signature: SomeObject.SomeGenericInstanceMethod<T>(T argument)

元气小坏坏 提交于 2019-12-28 07:03:11
问题 How do I call SomeObject.SomeGenericInstanceMethod<T>(T arg) ? There are a few posts about calling generic methods, but not quite like this one. The problem is that the method argument parameter is constrained to the generic parameter. I know that if the signature were instead SomeObject.SomeGenericInstanceMethod<T>(string arg) then I could get the MethodInfo with typeof (SomeObject).GetMethod("SomeGenericInstanceMethod", new Type[]{typeof (string)}).MakeGenericMethod(typeof(GenericParameter)