public object GenerateComponentProxy(ICallRouter callRouter, Type type, GameObject gameObject) { var interceptor = new CastleForwardingInterceptor(new CastleInvocationMapper(), callRouter); var proxyGenerationOptions = GetOptionsToMixinCallRouter(callRouter); // We can't instantiate the proxy using _proxyGenerator.CreateClassProxy // because components cannot be created using the 'new' operator. They must be added to a GameObject instead. // So instead, we use reflection to generate the proxy type from the ProxyGenerator // and then add it to the GameObect ourselves. MethodInfo method = _proxyGenerator.GetType().GetMethod("CreateClassProxyType", BindingFlags.Instance | BindingFlags.NonPublic); object[] args = new object[] { type, null, proxyGenerationOptions }; Type proxyType = (Type)method.Invoke(_proxyGenerator, args); // Add the proxy component type fo the GameObject. var proxy = gameObject.AddComponent(proxyType); // We still need to call the constructor generated for the proxy type so that the proxy has access // to the interceptors and other proxy options. // Use reflection to generate the argument list for the proxy class. MethodInfo argsMethod = _proxyGenerator.GetType().GetMethod("BuildArgumentListForClassProxy", BindingFlags.Instance | BindingFlags.NonPublic); // Get the constructor arguments for the proxy type. args = new object[] { proxyGenerationOptions, new IInterceptor[] { interceptor } }; List <object> arguments = (List <object>)argsMethod.Invoke(_proxyGenerator, args); // Now, we need to use reflection to manually find the correct constructor // to call for the proxy type and call it. ConstructorInfo[] constructors = proxyType.GetConstructors(); foreach (ConstructorInfo constructor in constructors) { if (constructor.GetParameters().Length == arguments.Count) { constructor.Invoke(proxy, arguments.ToArray()); } } interceptor.StartIntercepting(); return(proxy); }
public void On_interfaces() { ProxyGenerationOptions options = new ProxyGenerationOptions(); options.AttributesToAddToGeneratedTypes.Add(new __Protect()); object proxy = new ProxyGenerator().CreateInterfaceProxyWithoutTarget(typeof(IDisposable), new Type[0], options); Assert.IsTrue(proxy.GetType().IsDefined(typeof(__Protect), false)); }
public void On_class() { ProxyGenerationOptions options = new ProxyGenerationOptions(); options.AttributesToAddToGeneratedTypes.Add(new __Protect()); object proxy = new ProxyGenerator().CreateClassProxy(typeof(CanDefineAdditionalCustomAttributes), options); Assert.IsTrue(proxy.GetType().IsDefined(typeof(__Protect), false)); }
public void On_interfaces() { ProxyGenerationOptions options = new ProxyGenerationOptions(); options.AdditionalAttributes.Add(AttributeUtil.CreateBuilder <__Protect>()); object proxy = new ProxyGenerator().CreateInterfaceProxyWithoutTarget(typeof(IDisposable), new Type[0], options); Assert.IsTrue(proxy.GetType().IsDefined(typeof(__Protect), false)); }
public void On_class() { ProxyGenerationOptions options = new ProxyGenerationOptions(); options.AdditionalAttributes.Add(AttributeUtil.CreateBuilder <__Protect>()); object proxy = new ProxyGenerator().CreateClassProxy(typeof(CanDefineAdditionalCustomAttributes), options); Assert.IsTrue(proxy.GetType().IsDefined(typeof(__Protect), false)); }
public void DynamicProxy_includes_namespace_and_type_name_in_names_of_explicitly_implemented_methods() { var a = typeof(ISharedNameFromA); var b = typeof(ISharedNameFromB); var c = typeof(ISharedNameFromC); var proxy = new ProxyGenerator().CreateInterfaceProxyWithoutTarget( interfaceToProxy: a, additionalInterfacesToProxy: new[] { b, c }, interceptors: new StandardInterceptor()); var implementingType = proxy.GetType(); AssertNamingSchemeOfExplicitlyImplementedMethods(b, c, implementingType); }