Ejemplo n.º 1
0
        public virtual object CreateClassProxy(Type baseClass, IInvocationHandler handler)
        {
            AssertCreateClassProxyArguments(baseClass, handler);

            Type newType = ProxyBuilder.CreateClassProxy(baseClass);
            return CreateProxyInstance( newType, handler );
        }
Ejemplo n.º 2
0
 static public Object BuildProxy(IInvocationHandler _iInvocationHandler, params Type[]   interfaces)
 {
     if (interfaces.Length == 0)
     {
         throw new ArgumentOutOfRangeException("interfaces must contain at least 1 interface type");
     }
     return(BuildProxy(new InvocationDelegate(_iInvocationHandler.Invoke), null, recurseInterfaces(interfaces)));
 }
Ejemplo n.º 3
0
		public ClassGenerator(Type type, IInvocationHandler handler, IList methodsToIgnore,
			Type superclassIfTypeIsInterface, string[] additionalReferences)
		{
			if (s_piaPath == null)
			{
				// try to find the path where the Primary Interop Assemblies are stored
				// - unfortunately this can be at quite different locations!
				RegistryKey key = Registry.LocalMachine.OpenSubKey(
					@"SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\Primary Interop Assemblies");
				if (key == null)
					key = Registry.CurrentUser.OpenSubKey(
						@"SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\Primary Interop Assemblies");
				if (key == null)
					key = Registry.LocalMachine.OpenSubKey(
						@"SOFTWARE\Microsoft\VisualStudio\8.0\AssemblyFolders\Primary Interop Assemblies");
				if (key == null)
					key = Registry.CurrentUser.OpenSubKey(
						@"SOFTWARE\Microsoft\VisualStudio\8.0\AssemblyFolders\Primary Interop Assemblies");
				if (key == null)
					key = Registry.LocalMachine.OpenSubKey(
						@"SOFTWARE\Microsoft\.NETFramework\v2.0.50727\AssemblyFoldersEx\Primary Interop Assemblies");
				if (key == null)
					key = Registry.CurrentUser.OpenSubKey(
						@"SOFTWARE\Microsoft\.NETFramework\v2.0.50727\AssemblyFoldersEx\Primary Interop Assemblies");
				if (key == null)
					key = Registry.LocalMachine.OpenSubKey(
						@"SOFTWARE\Microsoft\VisualStudio\7.1\AssemblyFolders\Primary Interop Assemblies");
				if (key == null)
					key = Registry.CurrentUser.OpenSubKey(
						@"SOFTWARE\Microsoft\VisualStudio\7.1\AssemblyFolders\Primary Interop Assemblies");
				if (key == null)
					key = Registry.LocalMachine.OpenSubKey(
						@"SOFTWARE\Microsoft\VisualStudio\7.0\AssemblyFolders\Primary Interop Assemblies");
				if (key == null)
					key = Registry.CurrentUser.OpenSubKey(
						@"SOFTWARE\Microsoft\VisualStudio\7.0\AssemblyFolders\Primary Interop Assemblies");

				if (key == null)
					System.Diagnostics.Trace.WriteLine("Can't find path to Primary Interop Assemblies - build might not work correctly");
				else
					s_piaPath = (string)key.GetValue("");
			}

			this.type = type;
			this.handler = handler;
			this.methodsToIgnore = methodsToIgnore;
			this.superclassIfTypeIsInterface = superclassIfTypeIsInterface;
			if (additionalReferences == null)
				m_additonalReferences = new string[0];
			else
				m_additonalReferences = additionalReferences;

			// the methods for which we don't generated stubs
			methodsToCompletelyIgnore = new ArrayList();
			methodsToCompletelyIgnore.Add("Equals");
			methodsToCompletelyIgnore.Add("ToString");
			methodsToCompletelyIgnore.Add("Finalize");
		}
Ejemplo n.º 4
0
        public object CreateProxyInstance(Type proxyType, IInvocationHandler handler)
        {
            object result = Activator.CreateInstance(proxyType);

            FieldInfo handlerField = proxyType.GetField(INVOCATION_HANDLER_FIELD_NAME, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

            handlerField.SetValue(result, handler);

            return(result);
        }
Ejemplo n.º 5
0
        private ReactContext CreateReactContext(IInvocationHandler handler)
        {
            var eventEmitter = new RCTDeviceEventEmitter();

            eventEmitter.InvocationHandler = handler;
            var reactInstance = new TestReactInstance(eventEmitter);
            var reactContext  = new ReactContext();

            reactContext.InitializeWithInstance(reactInstance);
            return(reactContext);
        }
 public PersonBeanProxy(IPersonBean person, bool owner)
 {
     if (owner)
     {
         invocationHandler = new OwnerInvocationHandler(person);
     }
     else
     {
         invocationHandler = new NonOwnerInvocationHandler(person);
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MixinInvocationHandler"/> class.
        /// </summary>
        /// <param name="mixins">The mixin objects.</param>
        /// <param name="invocationHandler">The next invocation handler.</param>
        public MixinInvocationHandler(IDictionary<Type, object> mixins, IInvocationHandler invocationHandler)
        {
            if (mixins == null)
                throw new ArgumentNullException("mixins");

            if (invocationHandler == null)
                throw new ArgumentNullException("invocationHandler");

            _invocationHandler = invocationHandler;

            _mixins = new Dictionary<Type, object>(mixins);
        }
Ejemplo n.º 8
0
        private static ReactContext CreateReactContext(IInvocationHandler handler)
        {
            var context  = new ReactContext();
            var jsTimers = new JSTimersExecution
            {
                InvocationHandler = handler,
            };

            var reactInstance = new TestReactInstance(jsTimers);

            context.InitializeWithInstance(reactInstance);
            return(context);
        }
Ejemplo n.º 9
0
        static public Object BuildProxy(IInvocationHandler _iInvocationHandler, Object data, params Type[]       interfaces)
        {
            if (interfaces.Length == 0)
            {
                throw new ArgumentOutOfRangeException("interfaces must contain at least 1 interface type");
            }
            Object p = createInstance(new InvocationDelegate(_iInvocationHandler.Invoke), data, recurseInterfaces(interfaces));

            if (p is Proxy)
            {
                ((Proxy)p)._iInvocationHandler = _iInvocationHandler;
            }
            return(p);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Creates a new proxy.
        /// </summary>
        /// <typeparam name="T">The declaring type.</typeparam>
        /// <param name="proxyFactory">The proxy factory.</param>
        /// <param name="interfaceTypes">The additional interface types.</param>
        /// <param name="invocationHandler">The invocation handler.</param>
        /// <param name="arguments">The constructor arguments.</param>
        /// <returns>The new proxy object.</returns>
        public static T CreateProxy <T>(this IProxyFactory proxyFactory,
                                        IEnumerable <Type> interfaceTypes,
                                        IInvocationHandler invocationHandler,
                                        params object[] arguments) where T : class
        {
            if (proxyFactory == null)
            {
                throw new ArgumentNullException("proxyFactory");
            }

            var proxyTemplate = proxyFactory.GetProxyTemplate <T>(interfaceTypes);

            return(proxyTemplate.CreateProxy(invocationHandler, arguments));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Creates a new proxy.
        /// </summary>
        /// <param name="proxyFactory">The proxy factory.</param>
        /// <param name="declaringType">The declaring type.</param>
        /// <param name="interfaceTypes">The additional interface types.</param>
        /// <param name="invocationHandler">The invocation handler.</param>
        /// <param name="arguments">The constructor arguments.</param>
        /// <returns>The new proxy object.</returns>
        public static object CreateProxy(this IProxyFactory proxyFactory,
                                         Type declaringType,
                                         IEnumerable <Type> interfaceTypes,
                                         IInvocationHandler invocationHandler,
                                         params object[] arguments)
        {
            if (proxyFactory == null)
            {
                throw new ArgumentNullException("proxyFactory");
            }

            var proxyTemplate = proxyFactory.GetProxyTemplate(declaringType, interfaceTypes);

            return(proxyTemplate.CreateProxy(invocationHandler, arguments));
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MixinInvocationHandler"/> class.
        /// </summary>
        /// <param name="mixins">The mixin objects.</param>
        /// <param name="invocationHandler">The next invocation handler.</param>
        public MixinInvocationHandler(IDictionary <Type, object> mixins, IInvocationHandler invocationHandler)
        {
            if (mixins == null)
            {
                throw new ArgumentNullException("mixins");
            }

            if (invocationHandler == null)
            {
                throw new ArgumentNullException("invocationHandler");
            }

            _invocationHandler = invocationHandler;

            _mixins = new Dictionary <Type, object>(mixins);
        }
Ejemplo n.º 13
0
        public object Create(IInvocationHandler handler, Type objType, bool isObjInterface)
        {
            var typeName = objType.FullName + ProxySuffix;
            var type     = (Type)_typeMap[typeName];

            // check to see if the type was in the cache.  If the type was not cached, then
            // create a new instance of the dynamic type and add it to the cache.
            if (type != null)
            {
                return(Activator.CreateInstance(type, handler));
            }
            type = CreateType(handler, isObjInterface ? new[] { objType } : objType.GetInterfaces(), typeName);

            _typeMap.Add(typeName, type);

            // return a new instance of the type.
            return(Activator.CreateInstance(type, handler));
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 构造Class Proxy实例
        /// </summary>
        /// <param name="declaringType">代表declaringType</param>
        /// <param name="interfaceTypes">代表代理所实现的接口列表</param>
        /// <param name="h">调用处理器</param>
        /// <param name="mixins"></param>
        /// <param name="arguments">代理构造器参数</param>
        /// <returns></returns>
        public static object NewProxyInstance(Type declaringType,
            Type[] interfaceTypes, 
            IInvocationHandler h,
            object[] mixins = null ,
            params object[] arguments)
        {
            Guard.NotNull(declaringType, "parentType");
            Guard.NotNull(h, "h");

            if (mixins != null && mixins.Length != 0)
            {
                var mixinDictionary = GetMixinDictionary(mixins, interfaceTypes);

                h = new MixinInvocationHandler(mixinDictionary, h);
            }

            return ProxyFactory.CreateProxy(declaringType, interfaceTypes, h, arguments);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// 构造Class Proxy实例
        /// </summary>
        /// <param name="declaringType">代表declaringType</param>
        /// <param name="interfaceTypes">代表代理所实现的接口列表</param>
        /// <param name="h">调用处理器</param>
        /// <param name="mixins"></param>
        /// <param name="arguments">代理构造器参数</param>
        /// <returns></returns>
        public static object NewProxyInstance(Type declaringType,
                                              Type[] interfaceTypes,
                                              IInvocationHandler h,
                                              object[] mixins = null,
                                              params object[] arguments)
        {
            Guard.NotNull(declaringType, "parentType");
            Guard.NotNull(h, "h");

            if (mixins != null && mixins.Length != 0)
            {
                var mixinDictionary = GetMixinDictionary(mixins, interfaceTypes);

                h = new MixinInvocationHandler(mixinDictionary, h);
            }

            return(ProxyFactory.CreateProxy(declaringType, interfaceTypes, h, arguments));
        }
Ejemplo n.º 16
0
        /// <inheritdoc/>
        public object CreateProxy(IInvocationHandler invocationHandler, params object[] arguments)
        {
            if (invocationHandler == null)
            {
                throw new ArgumentNullException("invocationHandler");
            }

            var constructorArguments = new List <object> {
                invocationHandler
            };

            if (arguments != null && arguments.Length > 0)
            {
                constructorArguments.AddRange(arguments);
            }

            return(_proxyDefinition.CreateProxy(_implementationType, constructorArguments.ToArray()));
        }
Ejemplo n.º 17
0
        private static ReactContext CreateReactContext(IInvocationHandler handler)
        {
            var context = new ReactContext();

            var ids            = new List <int>();
            var appStateModule = new AppStateModule(context);

            appStateModule.Initialize();

            var eventEmitter = new RCTDeviceEventEmitter
            {
                InvocationHandler = handler,
            };

            var reactInstance = new TestReactInstance(appStateModule, eventEmitter);

            context.InitializeWithInstance(reactInstance);

            return(context);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// 构造Interface Proxy实例
        /// </summary>
        /// <param name="interfaceTypes">代表代理所实现的接口列表,不运行null</param>
        /// <param name="h"></param>
        /// <param name="mixins"></param>
        /// <returns></returns>
        public static object NewProxyInstance(Type[] interfaceTypes,
                                              IInvocationHandler h,
                                              object[] mixins = null)
        {
            Guard.NotNull(h, "h");

            if (interfaceTypes == null || interfaceTypes.Length == 0)
            {
                throw new ArgumentNullException("interfaceTypes");
            }

            if (mixins != null && mixins.Length != 0)
            {
                var mixinDictionary = GetMixinDictionary(mixins, interfaceTypes);

                h = new MixinInvocationHandler(mixinDictionary, h);
            }

            return(ProxyFactory.CreateProxy(typeof(object), interfaceTypes, h));
        }
Ejemplo n.º 19
0
 public static PrintDocumentAdapter.LayoutResultCallback GetLayoutResultCallback(IInvocationHandler invocationHandler)
 {
     return((PrintDocumentAdapter.LayoutResultCallback)ProxyBuilder.ForClass(Java.Lang.Class.FromType(typeof(Android.Print.PrintDocumentAdapter.LayoutResultCallback)))
            .Handler(invocationHandler)
            .Build());
 }
Ejemplo n.º 20
0
 protected Proxy(IInvocationHandler handler)
 {
     this.handler = handler;
 }
 public WaitForResultWithTimeoutInArguments(IInvocationHandler @delegate, int timeoutIndex, int timeUnitIndex)
 {
     _delegate      = @delegate;
     _timeoutIndex  = timeoutIndex;
     _timeUnitIndex = timeUnitIndex;
 }
Ejemplo n.º 22
0
Archivo: Proxy.cs Proyecto: jasom/foil
 public static Object BuildProxy(IInvocationHandler _iInvocationHandler,Object data,params Type[]	interfaces)
 {
     if(interfaces.Length==0)
         throw new ArgumentOutOfRangeException("interfaces must contain at least 1 interface type");
     Object	p				=	createInstance(new InvocationDelegate(_iInvocationHandler.Invoke),data,recurseInterfaces(interfaces));
     if(p is Proxy)
         ((Proxy)p)._iInvocationHandler	=	_iInvocationHandler;
     return	p;
 }
 public WaitForResult(IInvocationHandler @delegate)
 {
     _delegate = @delegate;
 }
Ejemplo n.º 24
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="interfaces"></param>
 /// <param name="handler"></param>
 /// <param name="context"></param>
 /// <returns></returns>
 public virtual object CreateCustomProxy(Type[] interfaces, 
     IInvocationHandler handler, GeneratorContext context)
 {
     AssertCreateProxyArguments( interfaces, handler, context );
     Type newType = ProxyBuilder.CreateCustomInterfaceProxy(interfaces, context);
     return CreateProxyInstance( newType, handler, context );
 }
        private static NetworkingModule CreateNetworkingModule(IHttpClient httpClient, IInvocationHandler handler)
        {
            var context = new ReactContext();

            var eventEmitter = new RCTDeviceEventEmitter();

            eventEmitter.InvocationHandler = handler;

            var reactInstance = new TestReactInstance(eventEmitter);

            context.InitializeWithInstance(reactInstance);
            return(new NetworkingModule(httpClient, context));
        }
Ejemplo n.º 26
0
 protected virtual object CreateProxyInstance(Type type, IInvocationHandler handler)
 {
     return Activator.CreateInstance(type, new object[] {handler});
 }
Ejemplo n.º 27
0
        protected static void AssertCreateProxyArguments(Type[] interfaces, IInvocationHandler handler, GeneratorContext context)
        {
            AssertCreateProxyArguments(interfaces, handler);

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
        }
Ejemplo n.º 28
0
 protected static void AssertCreateProxyArguments(Type[] interfaces, IInvocationHandler handler)
 {
     if (interfaces == null)
     {
         throw new ArgumentNullException("interfaces");
     }
     if (handler == null)
     {
         throw new ArgumentNullException("handler");
     }
     if (interfaces.Length == 0)
     {
         throw new ArgumentException("Can't handle an empty interface array");
     }
 }
Ejemplo n.º 29
0
 protected static void AssertCreateClassProxyArguments(Type baseClass, IInvocationHandler handler)
 {
     if (baseClass == null)
     {
         throw new ArgumentNullException("theClass");
     }
     if (baseClass.IsInterface)
     {
         throw new ArgumentException("'baseClass' must be a class, not an interface");
     }
     if (handler == null)
     {
         throw new ArgumentNullException("handler");
     }
 }
Ejemplo n.º 30
0
        /// <summary>
        /// Generates a proxy implementing all the specified interfaces and
        /// redirecting method invocations to the specifed handler.
        /// </summary>
        /// <param name="interfaces">Array of interfaces to be implemented</param>
        /// <param name="handler">instance of <see cref="IInvocationHandler"/></param>
        /// <returns>Proxy instance</returns>
        public virtual object CreateProxy(Type[] interfaces, IInvocationHandler handler)
        {
            AssertCreateProxyArguments(interfaces, handler);

            Type newType = ProxyBuilder.CreateInterfaceProxy(interfaces);
            return CreateProxyInstance( newType, handler );
        }
Ejemplo n.º 31
0
 /// <summary>
 /// Generates a proxy implementing all the specified interfaces and
 /// redirecting method invocations to the specifed handler.
 /// </summary>
 /// <param name="theInterface">Interface to be implemented</param>
 /// <param name="handler">instance of <see cref="IInvocationHandler"/></param>
 /// <returns>Proxy instance</returns>
 public virtual object CreateProxy(Type theInterface, IInvocationHandler handler)
 {
     return CreateProxy(new Type[] {theInterface}, handler);
 }
Ejemplo n.º 32
0
            public MixinInvocationHandler(Dictionary <Type, object> mixins, IInvocationHandler invocationHandler)
            {
                _invocationHandler = invocationHandler;

                _mixins = mixins;
            }
Ejemplo n.º 33
0
 protected virtual object CreateProxyInstance(Type type, IInvocationHandler handler, GeneratorContext context)
 {
     return CreateProxyInstance( type, handler );
 }
Ejemplo n.º 34
0
 /// <summary>
 /// 根据当前参数类型和接口建立代理类
 /// </summary>
 /// <param name="proxyFactory"></param>
 /// <param name="declaringType"></param>
 /// <param name="invocationHandler"></param>
 /// <param name="arguments"></param>
 /// <returns></returns>
 public static object CreateProxy(this IProxyFactory proxyFactory, Type declaringType, IInvocationHandler invocationHandler, params object[] arguments)
 {
     return(proxyFactory.CreateProxy(declaringType, Type.EmptyTypes, invocationHandler, arguments));
 }
 public virtual IInterceptor CreateForwardingInterceptor(IInvocationHandler forwardToInvocationHandler)
 {
     return new CastleForwardingInterceptor(new CastleInvocationMapper(), forwardToInvocationHandler);
 }
Ejemplo n.º 36
0
        public virtual object CreateCustomClassProxy(Type baseClass, 
            IInvocationHandler handler, GeneratorContext context)
        {
            AssertCreateClassProxyArguments(baseClass, handler, context);

            Type newType = ProxyBuilder.CreateCustomClassProxy(baseClass, context);
            return CreateProxyInstance( newType, handler, context );
        }
Ejemplo n.º 37
0
        public ClassGenerator(Type type, IInvocationHandler handler, IList methodsToIgnore,
                              Type superclassIfTypeIsInterface, string[] additionalReferences)
        {
            if (s_referenceAssembliesPath == null)
            {
                var basePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
                                            "Reference Assemblies", "Microsoft", "Framework", ".NETFramework");
                var clrVersion = Environment.Version;
                var path       = Path.Combine(basePath,
                                              string.Format("v{0}.{1}", clrVersion.Major, clrVersion.Minor));
                if (!Directory.Exists(path) && Directory.Exists(basePath))
                {
                    path = Directory.EnumerateDirectories(basePath).OrderByDescending(f => f).First();
                }
                s_referenceAssembliesPath = path;
            }

            if (s_piaPath35 == null)
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey(
                    @"SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\Microsoft .NET Framework 3.5 Reference Assemblies");
                if (key == null)
                {
                    System.Diagnostics.Trace.WriteLineIf(Environment.OSVersion.Platform != PlatformID.Unix,
                                                         "Can't find path to .Net 3.5 Primary Interop Assemblies - build might not work correctly");
                }
                else
                {
                    s_piaPath35 = (string)key.GetValue("");
                }
            }

            if (s_piaPath == null)
            {
                // try to find the path where the Primary Interop Assemblies are stored
                // - unfortunately this can be at quite different locations!
                RegistryKey key = Registry.CurrentUser.OpenSubKey(
                    @"SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\Primary Interop Assemblies");
                if (key == null)
                {
                    key = Registry.LocalMachine.OpenSubKey(
                        @"SOFTWARE\Microsoft\VisualStudio\8.0\AssemblyFolders\Primary Interop Assemblies");
                }
                if (key == null)
                {
                    key = Registry.CurrentUser.OpenSubKey(
                        @"SOFTWARE\Microsoft\VisualStudio\8.0\AssemblyFolders\Primary Interop Assemblies");
                }
                if (key == null)
                {
                    key = Registry.LocalMachine.OpenSubKey(
                        @"SOFTWARE\Microsoft\.NETFramework\v2.0.50727\AssemblyFoldersEx\Primary Interop Assemblies");
                }
                if (key == null)
                {
                    key = Registry.CurrentUser.OpenSubKey(
                        @"SOFTWARE\Microsoft\.NETFramework\v2.0.50727\AssemblyFoldersEx\Primary Interop Assemblies");
                }
                if (key == null)
                {
                    key = Registry.LocalMachine.OpenSubKey(
                        @"SOFTWARE\Microsoft\VisualStudio\7.1\AssemblyFolders\Primary Interop Assemblies");
                }
                if (key == null)
                {
                    key = Registry.CurrentUser.OpenSubKey(
                        @"SOFTWARE\Microsoft\VisualStudio\7.1\AssemblyFolders\Primary Interop Assemblies");
                }
                if (key == null)
                {
                    key = Registry.LocalMachine.OpenSubKey(
                        @"SOFTWARE\Microsoft\VisualStudio\7.0\AssemblyFolders\Primary Interop Assemblies");
                }
                if (key == null)
                {
                    key = Registry.CurrentUser.OpenSubKey(
                        @"SOFTWARE\Microsoft\VisualStudio\7.0\AssemblyFolders\Primary Interop Assemblies");
                }

                if (key == null)
                {
                    System.Diagnostics.Trace.WriteLineIf(Environment.OSVersion.Platform != PlatformID.Unix,
                                                         "Can't find path to legacy Primary Interop Assemblies - build might not work correctly");
                }
                else
                {
                    s_piaPath = (string)key.GetValue("");
                }
            }

            if (s_wcfPath30 == null)
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey(
                    @"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Communication Foundation");
                if (key == null)
                {
                    key = Registry.LocalMachine.OpenSubKey(
                        @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Communication Foundation");
                }
                if (key == null)
                {
                    System.Diagnostics.Trace.WriteLineIf(Environment.OSVersion.Platform != PlatformID.Unix,
                                                         "Can't find path to WCF - build might not work correctly");
                }
                else
                {
                    s_wcfPath30 = (string)key.GetValue("RuntimeInstallPath");
                }
            }

            this.type                        = type;
            this.handler                     = handler;
            this.methodsToIgnore             = methodsToIgnore;
            this.superclassIfTypeIsInterface = superclassIfTypeIsInterface;
            if (additionalReferences == null)
            {
                m_additonalReferences = new string[0];
            }
            else
            {
                m_additonalReferences = additionalReferences;
            }

            // the methods for which we don't generated stubs
            methodsToCompletelyIgnore = new ArrayList();
            methodsToCompletelyIgnore.Add("Equals");
            methodsToCompletelyIgnore.Add("ToString");
            methodsToCompletelyIgnore.Add("Finalize");
        }
Ejemplo n.º 38
0
        public static T NewProxyInstance <T>(IInvocationHandler handler)
        {
            Type   targetInterface = typeof(T);
            Random random          = new Random();

            var assemblyName    = new AssemblyName("ProxyAssembly_" + targetInterface.Name + "_" + random.RandomString(6));
            var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
            var moduleBuilder   = assemblyBuilder.DefineDynamicModule(assemblyName.Name);

            // create a new public, sealed type that inherits from object and implements the given interface
            string proxyTypeName       = "_Proxy_" + targetInterface.Name + "_" + random.RandomString(6);
            var    proxyTypeAttributes = TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed;
            var    typeBuilder         = moduleBuilder.DefineType(proxyTypeName, proxyTypeAttributes, typeof(object), new[] { targetInterface });

            // create an instance field for the invocation handler
            typeBuilder.AddInterfaceImplementation(targetInterface);
            var handlerField = typeBuilder.DefineField("_handler", typeof(IInvocationHandler), FieldAttributes.Private);

            // build a constructor that takes the invocation handler object as the only argument
            // public _Proxy_Type(IInvocationHandler handler);
            var         constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new[] { typeof(IInvocationHandler) });
            ILGenerator constructorIl      = constructorBuilder.GetILGenerator();

            // base();
            var objectConstructor = typeof(object).GetConstructor(new Type[0]);

            constructorIl.Emit(OpCodes.Ldarg_0);                 // load this
            constructorIl.Emit(OpCodes.Call, objectConstructor); // call base constructor

            // this._handler = handler
            constructorIl.Emit(OpCodes.Ldarg_0);             // load this
            constructorIl.Emit(OpCodes.Ldarg_1);             // load first argument (handler)
            constructorIl.Emit(OpCodes.Stfld, handlerField); // initialize handler field

            constructorIl.Emit(OpCodes.Ret);                 // return

            var interfaceTypes = new[] { targetInterface }.Concat(targetInterface.GetInterfaces()).ToArray();

            foreach (var interfaceType in interfaceTypes)
            {
                RegisterType(interfaceType);
                // for each method of the target interface, generate an implementation in the proxy type that passes the call to this._handler.Invoke()
                var interfaceMethods = interfaceType.GetMethods();
                for (int methodIndex = 0; methodIndex < interfaceMethods.Length; ++methodIndex)
                {
                    var interfaceMethod = interfaceMethods[methodIndex];
                    int methodIndexI    = methodIndex;
                    GenerateMethod(typeBuilder, handlerField, interfaceMethod, (methodIl) =>
                    {
                        methodIl.Emit(OpCodes.Ldstr, interfaceType.AssemblyQualifiedName); // load type name of interface (for GetMethodByIndex)
                        methodIl.Emit(OpCodes.Ldc_I4, methodIndexI);                       // load index of method

                        MethodInfo getMethodByIndex = typeof(DynamicProxy).GetMethod("GetMethodByIndex");
                        methodIl.Emit(OpCodes.Call, getMethodByIndex);  // call GetMethodByIndex - results in MethodInfo for second parameter of Invoke
                    });
                }

                // also pass all property accessors through Invoke in the same way
                var interfaceProperties = interfaceType.GetProperties();
                for (int propertyIndex = 0; propertyIndex < interfaceProperties.Length; ++propertyIndex)
                {
                    int propertyIndexI    = propertyIndex;
                    var propertyAccessors = interfaceProperties[propertyIndex].GetAccessors();
                    for (int accessorIndex = 0; accessorIndex < propertyAccessors.Length; ++accessorIndex)
                    {
                        var accessorMethod = propertyAccessors[accessorIndex];
                        int accessorIndexI = accessorIndex;
                        GenerateMethod(typeBuilder, handlerField, accessorMethod, (methodIl) =>
                        {
                            methodIl.Emit(OpCodes.Ldstr, interfaceType.AssemblyQualifiedName); // load type name of interface (for GetMethodByIndex)
                            methodIl.Emit(OpCodes.Ldc_I4, propertyIndexI);                     // load index of property
                            methodIl.Emit(OpCodes.Ldc_I4, accessorIndexI);                     // load index of accessor in property

                            MethodInfo getPropertyAccesorByIndex = typeof(DynamicProxy).GetMethod("GetPropertyAccessorByIndex");
                            methodIl.Emit(OpCodes.Call, getPropertyAccesorByIndex);  // call GetPropertyAccessorByIndex - results in MethodInfo for second parameter of Invoke
                        });
                    }
                }
            }

            var proxyType        = typeBuilder.CreateTypeInfo();
            var proxyConstructor = proxyType.GetConstructor(new[] { typeof(IInvocationHandler) });

            return((T)proxyConstructor.Invoke(new object[] { handler }));
        }
Ejemplo n.º 39
0
Archivo: Proxy.cs Proyecto: jasom/foil
 public static Object BuildProxy(IInvocationHandler _iInvocationHandler,params Type[]	interfaces)
 {
     if(interfaces.Length==0)
         throw new ArgumentOutOfRangeException("interfaces must contain at least 1 interface type");
     return	BuildProxy(new InvocationDelegate(_iInvocationHandler.Invoke),null,recurseInterfaces(interfaces));
 }
Ejemplo n.º 40
0
		public ClassGenerator(Type type, IInvocationHandler handler)
			: this(type, handler, new ArrayList()) {}
Ejemplo n.º 41
0
 /// <summary>
 /// 设置代理的拦截器
 /// </summary>
 /// <param name="invocationHandler"></param>
 /// <returns></returns>
 public Proxy SetInvocationHandler(IInvocationHandler invocationHandler)
 {
     this.invocationHandler = invocationHandler;
     return(this);
 }
Ejemplo n.º 42
0
 public static PrintDocumentAdapter.WriteResultCallback GetWriteResultCallback(IInvocationHandler invocationHandler)
 {
     return((PrintDocumentAdapter.WriteResultCallback)ProxyBuilder.ForClass(Class.FromType(typeof(PrintDocumentAdapter.WriteResultCallback)))
            .Handler(invocationHandler)
            .Build());
 }
Ejemplo n.º 43
0
 public static object NewInstance(AppDomain domain, Type[] interfaces, IInvocationHandler handler)
 {
     return(GetProxy(domain, interfaces)
            .GetConstructor(Types_InvocationHandler)
            .Invoke(new object[] { handler }));
 }
Ejemplo n.º 44
0
        /// <inheritdoc/>
        public object CreateProxy(IInvocationHandler invocationHandler, params object[] arguments)
        {
            if (invocationHandler == null)
                throw new ArgumentNullException("invocationHandler");

            var constructorArguments = new List<object> {invocationHandler};

            if (arguments != null && arguments.Length > 0)
            {
                constructorArguments.AddRange(arguments);
            }

            return _proxyDefinition.CreateProxy(_implementationType, constructorArguments.ToArray());
        }
Ejemplo n.º 45
0
            public MixinInvocationHandler(Dictionary<Type, object> mixins, IInvocationHandler invocationHandler)
            {

                _invocationHandler = invocationHandler;

                _mixins = mixins;
            }
Ejemplo n.º 46
0
 public static Object NewInstance(AppDomain domain, Type[] interfaces, IInvocationHandler handler)
 {
     return GetProxy(domain, interfaces)
         .GetConstructor(Types_InvocationHandler)
         .Invoke(new Object[] { handler });
 }
Ejemplo n.º 47
0
        /// <summary>
        /// 构造Interface Proxy实例
        /// </summary>
        /// <param name="interfaceTypes">代表代理所实现的接口列表,不运行null</param>
        /// <param name="h"></param>
        /// <param name="mixins"></param>
        /// <returns></returns>
        public static object NewProxyInstance(Type[] interfaceTypes,
            IInvocationHandler h,
            object[] mixins = null)
        {
            Guard.NotNull(h, "h");

            if (interfaceTypes == null || interfaceTypes.Length == 0)
                throw new ArgumentNullException("interfaceTypes");

            if (mixins != null && mixins.Length != 0)
            {
                var mixinDictionary = GetMixinDictionary(mixins, interfaceTypes);

                h = new MixinInvocationHandler(mixinDictionary, h);
            }

            return ProxyFactory.CreateProxy(typeof(object),interfaceTypes, h);
        }
Ejemplo n.º 48
0
		public ClassGenerator(Type type, IInvocationHandler handler, IList methodsToIgnore,
			Type superclassIfTypeIsInterface)
			: this(type, handler, methodsToIgnore, superclassIfTypeIsInterface, null)
		{
		}
Ejemplo n.º 49
0
		public ClassGenerator(Type type, IInvocationHandler handler, IList methodsToIgnore,
			Type superclassIfTypeIsInterface, string[] additionalReferences)
		{
			if (s_piaPath35 == null)
			{
				RegistryKey key = Registry.LocalMachine.OpenSubKey(
						@"SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\Microsoft .NET Framework 3.5 Reference Assemblies");
				if (key == null)
					System.Diagnostics.Trace.WriteLineIf(Environment.OSVersion.Platform != PlatformID.Unix,
						"Can't find path to .Net 3.5 Primary Interop Assemblies - build might not work correctly");
				else
					s_piaPath35 = (string)key.GetValue("");
			}

			if (s_piaPath == null)
			{
				// try to find the path where the Primary Interop Assemblies are stored
				// - unfortunately this can be at quite different locations!
				RegistryKey key = Registry.CurrentUser.OpenSubKey(
						@"SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\Primary Interop Assemblies");
				if (key == null)
					key = Registry.LocalMachine.OpenSubKey(
						@"SOFTWARE\Microsoft\VisualStudio\8.0\AssemblyFolders\Primary Interop Assemblies");
				if (key == null)
					key = Registry.CurrentUser.OpenSubKey(
						@"SOFTWARE\Microsoft\VisualStudio\8.0\AssemblyFolders\Primary Interop Assemblies");
				if (key == null)
					key = Registry.LocalMachine.OpenSubKey(
						@"SOFTWARE\Microsoft\.NETFramework\v2.0.50727\AssemblyFoldersEx\Primary Interop Assemblies");
				if (key == null)
					key = Registry.CurrentUser.OpenSubKey(
						@"SOFTWARE\Microsoft\.NETFramework\v2.0.50727\AssemblyFoldersEx\Primary Interop Assemblies");
				if (key == null)
					key = Registry.LocalMachine.OpenSubKey(
						@"SOFTWARE\Microsoft\VisualStudio\7.1\AssemblyFolders\Primary Interop Assemblies");
				if (key == null)
					key = Registry.CurrentUser.OpenSubKey(
						@"SOFTWARE\Microsoft\VisualStudio\7.1\AssemblyFolders\Primary Interop Assemblies");
				if (key == null)
					key = Registry.LocalMachine.OpenSubKey(
						@"SOFTWARE\Microsoft\VisualStudio\7.0\AssemblyFolders\Primary Interop Assemblies");
				if (key == null)
					key = Registry.CurrentUser.OpenSubKey(
						@"SOFTWARE\Microsoft\VisualStudio\7.0\AssemblyFolders\Primary Interop Assemblies");

				if (key == null)
				{
					System.Diagnostics.Trace.WriteLineIf(Environment.OSVersion.Platform != PlatformID.Unix,
						"Can't find path to legacy Primary Interop Assemblies - build might not work correctly");
				}
				else
					s_piaPath = (string)key.GetValue("");
			}

			if(s_wcfPath30 == null)
			{
				RegistryKey key = Registry.LocalMachine.OpenSubKey(
						@"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Communication Foundation");
				if (key == null)
					key = Registry.LocalMachine.OpenSubKey(
						@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Communication Foundation");
				if (key == null)
				{
					System.Diagnostics.Trace.WriteLineIf(Environment.OSVersion.Platform != PlatformID.Unix,
						"Can't find path to WCF - build might not work correctly");
				}
				else
					s_wcfPath30 = (string)key.GetValue("RuntimeInstallPath");
			}

			this.type = type;
			this.handler = handler;
			this.methodsToIgnore = methodsToIgnore;
			this.superclassIfTypeIsInterface = superclassIfTypeIsInterface;
			if (additionalReferences == null)
				m_additonalReferences = new string[0];
			else
				m_additonalReferences = additionalReferences;

			// the methods for which we don't generated stubs
			methodsToCompletelyIgnore = new ArrayList();
			methodsToCompletelyIgnore.Add("Equals");
			methodsToCompletelyIgnore.Add("ToString");
			methodsToCompletelyIgnore.Add("Finalize");
		}
Ejemplo n.º 50
0
 public ClassGenerator(Type type, IInvocationHandler handler)
     : this(type, handler, new ArrayList())
 {
 }
 public CastleForwardingInterceptor(CastleInvocationMapper invocationMapper, IInvocationHandler invocationHandler)
 {
     _invocationMapper = invocationMapper;
     _invocationHandler = invocationHandler;
 }
Ejemplo n.º 52
0
 public ClassGenerator(Type type, IInvocationHandler handler, IList methodsToIgnore)
     : this(type, handler, methodsToIgnore, null)
 {
 }
Ejemplo n.º 53
0
        public object CreateProxyInstance(Type proxyType, IInvocationHandler handler)
        {
            object result = Activator.CreateInstance(proxyType);

            FieldInfo handlerField = proxyType.GetField(INVOCATION_HANDLER_FIELD_NAME, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            handlerField.SetValue(result, handler);

            return result;
        }
Ejemplo n.º 54
0
 public ClassGenerator(Type type, IInvocationHandler handler, IList methodsToIgnore,
                       Type superclassIfTypeIsInterface)
     : this(type, handler, methodsToIgnore, superclassIfTypeIsInterface, null)
 {
 }
 public FireAndForget(IInvocationHandler @delegate)
 {
     _delegate = @delegate;
 }
Ejemplo n.º 56
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="theInterface"></param>
 /// <param name="handler"></param>
 /// <param name="context"></param>
 /// <returns></returns>
 public virtual object CreateCustomProxy(Type theInterface, 
     IInvocationHandler handler,
     GeneratorContext context)
 {
     return CreateCustomProxy( new Type[] { theInterface }, handler, context );
 }
Ejemplo n.º 57
0
 protected DynamicProxy(IInvocationHandler handler)
 {
     this.handler = handler;
 }
Ejemplo n.º 58
0
		public ClassGenerator(Type type, IInvocationHandler handler, IList methodsToIgnore)
			: this(type, handler, methodsToIgnore, null) {}