2011-12-01 8 views
5

Tôi đã có một phương pháp khuyến nông:Làm thế nào để sử dụng GetMethod cho phương pháp tĩnh mở rộng

public static class StringEx 
{ 
    public static bool Like(this string a, string b) 
    { 
     return a.ToLower().Contains(b.ToLower()); 
    } 
} 

Làm thế nào để phản ánh nó đúng cách qua GetMethod với các thông số của tôi? Tôi đã thử này không có thành công (Got một ngoại lệ về phương pháp tĩnh):

var like = typeof(StringEx).GetMethod("Like", new[] {typeof(string), typeof(string)}); 
comparer = Expression.Call(prop, like, value); 

Trả lời

-1

Bạn có thể truy cập vào phương pháp này là bạn sẽ làm gì với bất kỳ phương pháp tĩnh:

var like = typeof(StringEx).GetMethod("Like", new[] { typeof(string), typeof(string) }); 
+0

Vâng, tôi đang làm như thế nhưng, tôi đã có một ngoại lệ về phương pháp tĩnh ( – CodeAddicted

+0

ngoại lệ gì? Khi tôi kiểm tra mã, biến 'like' đã được khởi tạo đúng cách. –

+0

Điều này không hiệu quả đối với tôi, tôi cần bao gồm "BindingFlags.Static". – Colin

1

Bạn nên sử dụng khác tình trạng quá tải của GetMethod với BindingAttr tham số:

Type extendedType = typeof(StringEx); 
MethodInfo myMethodInfo = extendedType.GetMethod("Like", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic , null, new[] {typeof(string), typeof(string)},null); 
5

Sử dụng phương pháp mở rộng này để có được phương pháp khuyến nông khác:

public static class ReflectionExtensions 
{ 
    public static IEnumerable<MethodInfo> GetExtensionMethods(this Type type, Assembly extensionsAssembly) 
    { 
     var query = from t in extensionsAssembly.GetTypes() 
        where !t.IsGenericType && !t.IsNested 
        from m in t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) 
        where m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), false) 
        where m.GetParameters()[0].ParameterType == type 
        select m; 

     return query; 
    } 

    public static MethodInfo GetExtensionMethod(this Type type, Assembly extensionsAssembly, string name) 
    { 
     return type.GetExtensionMethods(extensionsAssembly).FirstOrDefault(m => m.Name == name); 
    } 

    public static MethodInfo GetExtensionMethod(this Type type, Assembly extensionsAssembly, string name, Type[] types) 
    { 
     var methods = (from m in type.GetExtensionMethods(extensionsAssembly) 
         where m.Name == name 
         && m.GetParameters().Count() == types.Length + 1 // + 1 because extension method parameter (this) 
         select m).ToList(); 

     if (!methods.Any()) 
     { 
      return default(MethodInfo); 
     } 

     if (methods.Count() == 1) 
     { 
      return methods.First(); 
     } 

     foreach (var methodInfo in methods) 
     { 
      var parameters = methodInfo.GetParameters(); 

      bool found = true; 
      for (byte b = 0; b < types.Length; b++) 
      { 
       found = true; 
       if (parameters[b].GetType() != types[b]) 
       { 
        found = false; 
       } 
      } 

      if (found) 
      { 
       return methodInfo; 
      } 
     } 

     return default(MethodInfo); 
    } 
} 

Sử dụng nó như thế này:

var assembly = Assembly.GetExecutingAssembly(); //change this to whatever assembly the extension method is in 

var methodInfo = typeof(string).GetExtensionMethod(assembly,"Like",new[] { typeof(string)});