Advertisement
Guest User

SIGSEGVmono-reflection

a guest
Jan 19th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3. using System.Linq.Expressions;
  4. using System.Linq;
  5.  
  6. namespace ReporSigSegVmono
  7. {
  8.     class MainClass
  9.  
  10.     {
  11.         private static readonly BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
  12.  
  13.         public static void Main (string[] args)
  14.         {
  15.  
  16.             Expression<Func<IFoo,DateTime?>> e2 = foo => (foo.unboxed);
  17.  
  18.             var me2 = e2.Body as MemberExpression;
  19.  
  20.             var result2 = getPropertyInfo (me2.Member as PropertyInfo, typeof(FooImpl));
  21.  
  22.             Console.WriteLine (result2.Name);
  23.  
  24.         }
  25.            
  26.  
  27.         private static MethodInfo getMethodInfo(MethodInfo interfaceMethod, Type implementingType) {
  28.             var interfaceType = interfaceMethod.DeclaringType;
  29.  
  30.             var interfaceMap = implementingType.GetInterfaceMap (interfaceType);
  31.  
  32.             for (int i = 0; i < interfaceMap.InterfaceMethods.Length; i++)
  33.                 if (interfaceMap.InterfaceMethods [i] == interfaceMethod)
  34.                     return interfaceMap.TargetMethods [i];
  35.             return null;
  36.         }
  37.  
  38.         private static PropertyInfo getPropertyInfo (PropertyInfo interfaceProperty, Type implementingType) {
  39.             var interfaceGetter = interfaceProperty.GetGetMethod();
  40.  
  41.             if (interfaceGetter != null) {
  42.                 var getterImpl = getMethodInfo(interfaceGetter, implementingType);
  43.  
  44.                 return implementingType.GetProperties(bindingFlags | BindingFlags.GetProperty).First(p => p.GetGetMethod(true) == getterImpl);
  45.             }
  46.  
  47.             return null;
  48.         }
  49.  
  50.     }
  51.  
  52.     abstract class FooImpl : FooBase, IFoo {
  53.  
  54.         public abstract void doBarAction();
  55.  
  56.         public abstract DateTime? unboxed {
  57.             get;
  58.             set;
  59.         }
  60.  
  61.  
  62.     }
  63.  
  64.  
  65.     interface IFoo {
  66.  
  67.         void doBarAction();
  68.  
  69.         DateTime? unboxed { get; set; }
  70.  
  71.     }
  72.  
  73.     abstract class FooBase {
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement