/// <summary> /// Creates a proxy for this object /// </summary> /// <typeparam name="I">Interface to proxy</typeparam> /// <typeparam name="A">Will only proxy method that have this attribute</typeparam> /// <param name="objectToProxy"></param> /// <param name="interceptorDelegate">delegate to call on method invocation</param> /// <returns></returns> public static I Proxy <I, A>(this I objectToProxy, EmitProxyInterceptor <I> interceptorDelegate) where A : Attribute { return(PlatformTypeProxyCsharpHelper.Proxy <I>(objectToProxy, (info) => { //Check if hte attribute is on the interface //also check if our method info is actually a property value, if so we will check if the //attribute is on the property bool isAttributeOnInterface = typeof(I).GetMethod(info.Name, info.GetParameters().Select(p => p.ParameterType).ToArray()) != null; if (isAttributeOnInterface == false && info.IsSpecialName && (info.Name.StartsWith("get_") || info.Name.StartsWith("set_"))) { isAttributeOnInterface = typeof(I).GetProperty(info.Name.Remove(0, 4)) != null; } //Check if hte attribute is on our actual object //also check if our method info is actually a property value, if so we will check if the //attribute is on the property bool isAttributeOnMethod = info.GetCustomAttributes(typeof(A), true).Count() > 0; if (isAttributeOnMethod == false && info.IsSpecialName && (info.Name.StartsWith("get_") || info.Name.StartsWith("set_"))) { isAttributeOnMethod = objectToProxy.GetType().GetProperty(info.Name.Remove(0, 4)) != null; } //we have the attribute if it is either on the interface or method return isAttributeOnInterface || isAttributeOnMethod; }, interceptorDelegate)); }
public T Create() { return(PlatformTypeProxyCsharpHelper.Proxy <T>(Activator.CreateInstance(type, true), MyInterceptorMethod)); }
/// <summary> /// Creates a proxy for this object /// </summary> /// <typeparam name="I">Interface to proxy</typeparam> /// <param name="objectToProxy"></param> /// <param name="interceptorDelegate">delegate to call on method invocation</param> /// <returns></returns> public static I Proxy <I>(this I objectToProxy, EmitProxyInterceptor <I> interceptorDelegate) { return(PlatformTypeProxyCsharpHelper.Proxy <I>(objectToProxy, (info) => true, interceptorDelegate)); }