Exemplo n.º 1
0
        private static GrainReferenceCaster MakeCaster(Type interfaceType)
        {
            CodeGeneratorManager.GenerateAndCacheCodeForAssembly(interfaceType.Assembly);
            var genericInterfaceType = interfaceType.IsConstructedGenericType
                                           ? interfaceType.GetGenericTypeDefinition()
                                           : interfaceType;

            // Try to find the correct GrainReference type for this interface.
            Type grainReferenceType;

            if (!GrainToReferenceMapping.TryGetValue(genericInterfaceType, out grainReferenceType))
            {
                throw new InvalidOperationException(
                          string.Format("Cannot find generated GrainReference class for interface '{0}'", interfaceType));
            }

            if (interfaceType.IsConstructedGenericType)
            {
                grainReferenceType = grainReferenceType.MakeGenericType(interfaceType.GenericTypeArguments);
            }

            // Get the grain reference constructor.
            var constructor =
                grainReferenceType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
                .Where(
                    _ =>
            {
                var parameters = _.GetParameters();
                return(parameters.Length == 1 && parameters[0].ParameterType == typeof(GrainReference));
            }).FirstOrDefault();

            if (constructor == null)
            {
                throw new InvalidOperationException(
                          string.Format(
                              "Cannot find suitable constructor on generated reference type for interface '{0}'",
                              interfaceType));
            }

            // Construct an expression to construct a new instance of this grain reference when given another grain
            // reference.
            var createLambdaParameter = Expression.Parameter(typeof(GrainReference), "gr");
            var createLambda          =
                Expression.Lambda <Func <GrainReference, IAddressable> >(
                    Expression.New(constructor, createLambdaParameter),
                    createLambdaParameter);
            var grainRefParameter = Expression.Parameter(typeof(IAddressable), "grainRef");
            var body =
                Expression.Call(
                    GrainReferenceCastInternalMethodInfo,
                    Expression.Constant(interfaceType),
                    createLambda,
                    grainRefParameter,
                    Expression.Constant(GrainInterfaceData.GetGrainInterfaceId(interfaceType)));

            // Compile and return the reference casting lambda.
            var lambda = Expression.Lambda <GrainReferenceCaster>(body, grainRefParameter);

            return(lambda.Compile());
        }
Exemplo n.º 2
0
        public Type GetGrainMethodInvokerType(Type interfaceType)
        {
            var typeInfo = interfaceType.GetTypeInfo();

            CodeGeneratorManager.GenerateAndCacheCodeForAssembly(typeInfo.Assembly);
            var genericInterfaceType = interfaceType.IsConstructedGenericType
                                           ? typeInfo.GetGenericTypeDefinition()
                                           : interfaceType;

            // Try to find the correct IGrainMethodInvoker type for this interface.
            Type invokerType;

            if (!this.grainToInvokerMapping.TryGetValue(genericInterfaceType, out invokerType))
            {
                throw new InvalidOperationException(
                          $"Cannot find generated {nameof(IGrainMethodInvoker)} implementation for interface '{interfaceType}'");
            }

            if (interfaceType.IsConstructedGenericType)
            {
                invokerType = invokerType.MakeGenericType(typeInfo.GenericTypeArguments);
            }

            return(invokerType);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Processes the provided assembly.
        /// </summary>
        /// <param name="assembly">The assembly to process.</param>
        private static void ProcessAssembly(Assembly assembly)
        {
            // If the assembly is loaded for reflection only avoid processing it.
            if (assembly.ReflectionOnly)
            {
                return;
            }

            // Don't bother re-processing an assembly we've already scanned
            lock (ProcessedAssemblies)
            {
                if (!ProcessedAssemblies.Add(assembly))
                {
                    return;
                }
            }

            // If the assembly does not reference Orleans, avoid generating code for it.
            if (TypeUtils.IsOrleansOrReferencesOrleans(assembly))
            {
                // Code generation occurs in a self-contained assembly, so invoke it separately.
                CodeGeneratorManager.GenerateAndCacheCodeForAssembly(assembly);
            }

            // Process each type in the assembly.
            var shouldProcessSerialization = SerializationManager.ShouldFindSerializationInfo(assembly);

            TypeInfo[] assemblyTypes;
            try
            {
                assemblyTypes = assembly.DefinedTypes.ToArray();
            }
            catch (Exception exception)
            {
                if (Logger.IsWarning)
                {
                    var message =
                        string.Format(
                            "AssemblyLoader encountered an exception loading types from assembly '{0}': {1}",
                            assembly.FullName,
                            exception);
                    Logger.Warn(ErrorCode.Loader_TypeLoadError_5, message, exception);
                }

                return;
            }

            // Process each type in the assembly.
            foreach (var type in assemblyTypes)
            {
                if (shouldProcessSerialization)
                {
                    SerializationManager.FindSerializationInfo(type);
                }

                GrainFactory.FindSupportClasses(type);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Processes the provided assembly.
        /// </summary>
        /// <param name="assembly">The assembly to process.</param>
        private void ProcessAssembly(Assembly assembly)
        {
            string assemblyName = assembly.GetName().Name;

            if (this.logger.IsVerbose3)
            {
                this.logger.Verbose3("Processing assembly {0}", assemblyName);
            }

#if !NETSTANDARD
            // If the assembly is loaded for reflection only avoid processing it.
            if (assembly.ReflectionOnly)
            {
                return;
            }
#endif

            // Don't bother re-processing an assembly we've already scanned
            lock (this.processedAssemblies)
            {
                if (!this.processedAssemblies.Add(assembly))
                {
                    return;
                }
            }

            // If the assembly does not reference Orleans, avoid generating code for it.
            if (TypeUtils.IsOrleansOrReferencesOrleans(assembly))
            {
                // Code generation occurs in a self-contained assembly, so invoke it separately.
                CodeGeneratorManager.GenerateAndCacheCodeForAssembly(assembly);
            }

            // Process each type in the assembly.
            var assemblyTypes = TypeUtils.GetDefinedTypes(assembly, this.logger).ToArray();

            // Process each type in the assembly.
            foreach (TypeInfo typeInfo in assemblyTypes)
            {
                try
                {
                    var    type     = typeInfo.AsType();
                    string typeName = typeInfo.FullName;
                    if (this.logger.IsVerbose3)
                    {
                        this.logger.Verbose3("Processing type {0}", typeName);
                    }

                    SerializationManager.FindSerializationInfo(type);

                    this.typeCache.FindSupportClasses(type);
                }
                catch (Exception exception)
                {
                    this.logger.Error(ErrorCode.SerMgr_TypeRegistrationFailure, "Failed to load type " + typeInfo.FullName + " in assembly " + assembly.FullName + ".", exception);
                }
            }
        }
Exemplo n.º 5
0
        public Type GetGrainReferenceType(Type interfaceType)
        {
            var typeInfo = interfaceType.GetTypeInfo();

            codeGeneratorManager.GenerateAndCacheCodeForAssembly(typeInfo.Assembly);
            var genericInterfaceType = interfaceType.IsConstructedGenericType
                                           ? typeInfo.GetGenericTypeDefinition()
                                           : interfaceType;

            if (!typeof(IAddressable).IsAssignableFrom(interfaceType))
            {
                throw new InvalidCastException(
                          $"Target interface must be derived from {typeof(IAddressable).FullName} - cannot handle {interfaceType}");
            }

            // Try to find the correct GrainReference type for this interface.
            Type grainReferenceType;

            if (!this.grainToReferenceMapping.TryGetValue(genericInterfaceType, out grainReferenceType))
            {
                throw new InvalidOperationException(
                          $"Cannot find generated {nameof(GrainReference)} class for interface '{interfaceType}'");
            }

            if (interfaceType.IsConstructedGenericType)
            {
                grainReferenceType = grainReferenceType.MakeGenericType(typeInfo.GenericTypeArguments);
            }

            if (!typeof(IAddressable).IsAssignableFrom(grainReferenceType))
            {
                // This represents an internal programming error.
                throw new InvalidCastException(
                          $"Target reference type must be derived from {typeof(IAddressable).FullName}- cannot handle {grainReferenceType}");
            }

            return(grainReferenceType);
        }
Exemplo n.º 6
0
        private static IGrainMethodInvoker MakeInvoker(Type interfaceType)
        {
            CodeGeneratorManager.GenerateAndCacheCodeForAssembly(interfaceType.Assembly);
            var genericInterfaceType = interfaceType.IsConstructedGenericType
                                           ? interfaceType.GetGenericTypeDefinition()
                                           : interfaceType;

            // Try to find the correct IGrainMethodInvoker type for this interface.
            Type invokerType;

            if (!GrainToInvokerMapping.TryGetValue(genericInterfaceType, out invokerType))
            {
                return(null);
            }

            if (interfaceType.IsConstructedGenericType)
            {
                invokerType = invokerType.MakeGenericType(interfaceType.GenericTypeArguments);
            }

            return((IGrainMethodInvoker)Activator.CreateInstance(invokerType));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Processes the provided assembly.
        /// </summary>
        /// <param name="assembly">The assembly to process.</param>
        private static void ProcessAssembly(Assembly assembly)
        {
            string assemblyName = assembly.GetName().Name;

            if (Logger.IsVerbose3)
            {
                Logger.Verbose3("Processing assembly {0}", assemblyName);
            }
            // If the assembly is loaded for reflection only avoid processing it.
            if (assembly.ReflectionOnly)
            {
                return;
            }

            // Don't bother re-processing an assembly we've already scanned
            lock (ProcessedAssemblies)
            {
                if (!ProcessedAssemblies.Add(assembly))
                {
                    return;
                }
            }

            // If the assembly does not reference Orleans, avoid generating code for it.
            if (TypeUtils.IsOrleansOrReferencesOrleans(assembly))
            {
                // Code generation occurs in a self-contained assembly, so invoke it separately.
                CodeGeneratorManager.GenerateAndCacheCodeForAssembly(assembly);
            }

            // Process each type in the assembly.
            var shouldProcessSerialization = SerializationManager.ShouldFindSerializationInfo(assembly);
            var assemblyTypes = TypeUtils.GetDefinedTypes(assembly, Logger).ToArray();

            // Process each type in the assembly.
            List <Type> typesToFind = new List <Type>(assemblyTypes.Length);

            foreach (TypeInfo typeInfo in assemblyTypes)
            {
                try
                {
                    var    type     = typeInfo.AsType();
                    string typeName = typeInfo.FullName;
                    if (Logger.IsVerbose3)
                    {
                        Logger.Verbose3("Processing type {0}", typeName);
                    }
                    if (shouldProcessSerialization)
                    {
                        typesToFind.Add(type);
                    }

                    GrainFactory.FindSupportClasses(type);
                }
                catch (Exception exception)
                {
                    Logger.Error(ErrorCode.SerMgr_TypeRegistrationFailure, "Failed to load type " + typeInfo.FullName + " in assembly " + assembly.FullName + ".", exception);
                }
            }
            SerializationManager.FindSerializationInfo(typesToFind);
        }