示例#1
0
 public static MethodSource From(MethodInfo method)
 {
     if (method == null)
     {
         return(null);
     }
     lock (_syncobj)
     {
         MethodSource source;
         if (!_sources.TryGetValue(method, out source))
         {
             source = new MethodSource(method);
             _sources.Add(method, source);
         }
         return(source);
     }
 }
示例#2
0
        private void AddHook()
        {
            lock (this)
            {
                if (_properties == null)
                {
                    _properties = new Dictionary <PropertyInfo, PropertyHookContext>();
                    Func <MethodInfo, MethodSource> addHook = (mi) =>
                    {
                        if (mi == null || !mi.IsPublic)
                        {
                            return(null);
                        }
                        MethodSource source   = MethodSource.From(mi);
                        MethodInfo   pointcut = VirtualProxyHooker.Create(mi);
                        source.AddHook(pointcut);
                        return(source);
                    };
                    foreach (PropertyInfo property in this.DeclaringType.GetProperties())
                    {
                        PropertyHookContext context = new PropertyHookContext();
                        context.PropertyInfo  = property;
                        context.GetMethodInfo = property.GetGetMethod();
                        context.SetMethodInfo = property.GetSetMethod();

                        MethodSource source = addHook(context.GetMethodInfo);
                        context.GetMethodSource = source;

                        source = addHook(context.SetMethodInfo);
                        context.SetMethodSource = source;

                        if (context.GetMethodSource != null)
                        {
                            _map.Add(context.GetMethodInfo, context);
                        }
                        if (context.SetMethodSource != null)
                        {
                            _map.Add(context.SetMethodInfo, context);
                        }
                        if (context.GetMethodSource != null || context.SetMethodSource != null)
                        {
                            _properties.Add(property, context);
                        }
                    }
                }
                else
                {
                    Action <MethodSource> resume = (source) =>
                    {
                        if (source != null)
                        {
                            source.Resume();
                        }
                    };
                    foreach (PropertyHookContext context in _properties.Values)
                    {
                        resume(context.SetMethodSource);
                        resume(context.GetMethodSource);
                    }
                }
            }
        }