Exemple #1
0
        /// <summary>
        /// This method adds all depencencies of a typeof()-argument of an attribute to the dependencies of the type.
        /// </summary>
        /// <param name="dependencies">The collection of dependencies into which all found dependencies are inserted. (Can be regared as an out-parameter.)</param>
        /// <param name="sourceFile">The corresdonding source code location.</param>
        /// <param name="parameterType">A string representing the type of the parameter.</param>
        /// <param name="parameterValue">The argument that is passed for this attribute parameter.</param>
        private static void VisitAttributeParameters(DependencyCollection dependencies, SourceFile sourceFile, string parameterType, object parameterValue)
        {
            if (parameterType.Equals("System.Type"))
            {
                // In the case of external types IL code contains the informations on the assembly defining the type
                String[] typeParts = parameterValue.ToString().Split(',');
                string cleanedDependency = typeParts[0].Replace("+", "/");

                // Remove terminating "\0"
                // Bug ID 2268: Cecil sometimes terminates constructor parameters in attributes that use typeof() with "\0". The reason therefore is unclear. 
                if (cleanedDependency.EndsWith("\0"))
                {
                    cleanedDependency = cleanedDependency.Remove(cleanedDependency.Length - 2);
                }

                if (!cleanedDependency.Equals(String.Empty))
                {
                    dependencies.AddDependency(cleanedDependency, DirectTypeDependency, sourceFile);
                }
            }
            else if (parameterType.Equals("System.Type[]"))
            {
                foreach (CustomAttributeArgument arrayMember in (CustomAttributeArgument[])parameterValue)
                {
                    string memberType = arrayMember.Value.ToString();
                    // In the case of external types IL code contains the informations on the assembly defining the type
                    string[] typeParts = memberType.Split(',');
                    string cleanedDependency = typeParts[0].Replace("+", "/");
                    if (!cleanedDependency.Equals(string.Empty))
                    {
                        dependencies.AddDependency(cleanedDependency, DirectTypeDependency, sourceFile);
                    }
                }
            }
        }
Exemple #2
0
        public void AddDependency(DependencyLifetime lifetime)
        {
            _dependencies.AddDependency(_contract, _implementation, lifetime);

            var dependency = _dependencies.GetRequiredDependency(_contract);

            dependency.Contracts.Should().Contain(_contract);
            dependency.Implementation.Should().Be(_implementation);
            dependency.Lifetime.Should().Be(lifetime);
        }
 public static DependencyCollection AddHttpHandler(this DependencyCollection collection,
                                                   Func <IDependencyScope, IHttpRequestHandler> builder,
                                                   DependencyLifetime lifetime = DependencyLifetime.Singleton)
 {
     collection.AddDependency(builder, lifetime);
     return(collection);
 }
Exemple #4
0
 public void TryRegister(DependencyCollection collection, Type implementation)
 {
     if (ECSUtils.TryGetImplementedSystemInterfaces(implementation, out var contracts))
     {
         collection.AddDependency(contracts, implementation, _lifetime);
     }
 }
Exemple #5
0
        /// <summary>
        /// Collects the dependencies from an instruction that has a MemberReference as operand.
        /// </summary>
        /// <param name="dependencies">The collection of dependencies into which all found dependencies are inserted. (Can be regared as an out-parameter.)</param>
        /// <param name="sourceFile">The corresdonding source code location.</param>
        /// <param name="memberRef">The MemberReference to investigate.</param>
        private static void VisitMemberReference(DependencyCollection dependencies, SourceFile sourceFile, MemberReference memberRef)
        {
            if (memberRef.DeclaringType != null)
            {
                string memberName = memberRef.Name;
                if (memberRef is MethodReference)
                {
                    memberName = Formatter.FormatMethodReference(((MethodReference)memberRef));
                }

                dependencies.AddDependency(memberRef.DeclaringType.FullName, memberName, sourceFile);
            }

            if (memberRef.DeclaringType is GenericInstanceType)
            {
                GenericInstanceType declaringType = (GenericInstanceType)memberRef.DeclaringType;
                VisitGenericArguments(declaringType.GenericArguments, dependencies, sourceFile);
            }

            if (memberRef is MethodReference)
            {
                VisitMethodReference(dependencies, sourceFile, (MethodReference)memberRef);
            }
            else if (memberRef is FieldReference)
            {
                VisitFieldReference(dependencies, sourceFile, (FieldReference)memberRef);
            }
        }
 public static DependencyCollection AddHttpHandler <THandler>(this DependencyCollection collection,
                                                              DependencyLifetime lifetime = DependencyLifetime.Singleton)
     where THandler : class, IHttpRequestHandler
 {
     collection.AddDependency(Typeof <IHttpRequestHandler> .Raw, typeof(THandler), lifetime);
     return(collection);
 }
Exemple #7
0
        /// <summary>
        /// Collects the dependency information contained in a single IL instruction.
        /// </summary>
        /// <param name="instruction">The Instruction to investigate.</param>
        /// <param name="dependencies">The collection of dependencies into which all found dependencies are inserted. (Can be regared as an out-parameter.)</param>
        private static void VisitInstruction(Instruction instruction, DependencyCollection dependencies)
        {
            if (!IsValidOperandType(instruction.OpCode.OperandType))
            {
                return;
            }

            if (instruction.Operand != null)
            {
                SourceFile sourceFile = new SourceFile(instruction.SequencePoint);
                if (instruction.Operand is GenericParameter)
                {
                    return;
                }
                else if (instruction.Operand is GenericInstanceType)
                {
                    GenericInstanceType instType = (GenericInstanceType)instruction.Operand;
                    VisitGenericArguments(instType.GenericArguments, dependencies, sourceFile);
                    dependencies.AddDependency(instType.FullName, DirectTypeDependency, sourceFile);
                }
                else if (instruction.Operand is TypeReference)
                {
                    TypeReference typeRef = (TypeReference)instruction.Operand;
                    VisitGenericParameters(typeRef.GenericParameters, dependencies, sourceFile);
                    if (!IsGenericTypeVariable(typeRef))
                    {
                        dependencies.AddDependency(typeRef.FullName, DirectTypeDependency, sourceFile);
                    }
                }
                else if (instruction.Operand is MemberReference)
                {
                    MemberReference memberRef = (MemberReference)instruction.Operand;
                    VisitMemberReference(dependencies, sourceFile, memberRef);
                }
                else if (instruction.Operand is CallSite)
                {
                    // Calls to non-managed code - currently not supported
                }
                else
                {   // This should never happen:
                    Console.WriteLine("\n\n Strange Operand found: {0}\n", instruction.Operand);
                }
            }
            else return;
        }
        public void TryRegister(DependencyCollection collection, Type implementation)
        {
            if (!_contract.IsAssignableFrom(implementation))
            {
                return;
            }

            collection.AddDependency(_contract, implementation, _lifetime);
        }
Exemple #9
0
        public static DependencyCollection AddLogWriter <TWriter>(this DependencyCollection collection,
                                                                  DependencyLifetime lifetime = DependencyLifetime.Singleton)
            where TWriter : ILogWriter
        {
            var implementation = Typeof <TWriter> .Raw;
            var contracts      = new[] { Typeof <ILogWriter> .Raw, implementation };

            collection.AddDependency(contracts, implementation, lifetime);

            return(collection);
        }
Exemple #10
0
        public void TryRegister(DependencyCollection collection, Type implementation)
        {
            var contracts = new LocalList <Type>();

            TryAdd(ref contracts, _beginUpdateSystem, implementation);
            TryAdd(ref contracts, _endUpdateSystem, implementation);
            TryAdd(ref contracts, _initializeSystem, implementation);
            TryAdd(ref contracts, _updateSystem, implementation);

            collection.AddDependency(contracts.ToArray(), implementation, DependencyLifetime.Scoped);
        }
Exemple #11
0
        private static void InsertDependentTypes(TypeReference typeRef, DependencyCollection dependencies, SourceFile sourceFile)
        {
            if (!IsGenericTypeVariable(typeRef))
            {
                dependencies.AddDependency(typeRef.FullName, DirectTypeDependency, sourceFile);
            }

            if (typeRef is GenericInstanceType)
            {
                VisitGenericArguments(((GenericInstanceType)typeRef).GenericArguments, dependencies, sourceFile);
            }
        }
Exemple #12
0
        /// <summary>
        /// Collects the dependency information contained in parameter lists (e.g. the parameters of a method).
        /// </summary>
        /// <param name="parameters">The ParameterDefinitionCollection to investigate.</param>
        /// <param name="dependencies">The collection of dependencies into which all found dependencies are inserted. (Can be regared as an out-parameter.)</param>
        /// <param name="sourceFile">The corresdonding source code location.</param>
        private static void VisitParameters(Collection<ParameterDefinition> parameters, DependencyCollection dependencies, SourceFile sourceFile)
        {
            foreach (ParameterDefinition paraDef in parameters)
            {
                if (!IsGenericTypeVariable(paraDef.ParameterType))
                {
                    dependencies.AddDependency(paraDef.ParameterType.FullName, DirectTypeDependency, sourceFile);
                }

                VisitGenericParameters(paraDef.ParameterType.GenericParameters, dependencies, sourceFile);
            }
        }
Exemple #13
0
        /// <summary>
        /// Collects the dependency information for a given GenericParameterCollection.
        /// The parameter definition itself does not contain any depencencies, only the constraints for generic parameters may contain dependencies.
        /// </summary>
        /// <param name="parameters">The GenericParameterCollection to investigate.</param>
        /// <param name="dependencies">The collection of dependencies into which all found dependencies are inserted. (Can be regared as an out-parameter.)</param>
        /// <param name="sourceFile">The corresdonding source code location.</param>
        private static void VisitGenericParameters(Collection<GenericParameter> parameters, DependencyCollection dependencies, SourceFile sourceFile)
        {
            // only the types metioned in the constraints are of importance
            foreach (GenericParameter param in parameters)
            {
                foreach (TypeReference constraint in param.Constraints)
                {
                    dependencies.AddDependency(constraint.FullName, DirectTypeDependency, sourceFile);
                    VisitGenericParameters(constraint.GenericParameters, dependencies, sourceFile);

                }
            }
        }
Exemple #14
0
        // ReSharper disable once InconsistentNaming
        public static DependencyCollection AddECSSystem <TSystem>(this DependencyCollection collection,
                                                                  DependencyLifetime lifetime = DependencyLifetime.Singleton)
            where TSystem : class, ISystem
        {
            var type = typeof(TSystem);
            var systemsInterfaces = ReflectionUtils.GetInterfaceImplementations(type, typeof(ISystem));

            systemsInterfaces.Add(type);

            collection.AddDependency(systemsInterfaces.ToArray(), type, lifetime);

            return(collection);
        }
        private static void AddTransient(DependencyCollection dependencies, ServiceDescriptor descriptor)
        {
            if (descriptor.ImplementationFactory == null)
            {
                dependencies.AddTransient(descriptor.ServiceType, descriptor.ImplementationType);
            }
            else
            {
                var contracts = new[] { descriptor.ServiceType };
                var resolver  = BuildDelegateResolver(descriptor);

                dependencies.AddDependency(new TransientDependency(contracts, resolver));
            }
        }
        public void TryRegister(DependencyCollection collection, Type implementation)
        {
            var interfaces = implementation.GetInterfaces();

            foreach (var interfaceType in interfaces)
            {
                if (!ReflectionUtils.IsGenericTypeImplementation(interfaceType, _genericContract))
                {
                    continue;
                }

                collection.AddDependency(interfaceType, implementation, _lifetime);
                return;
            }
        }
        private static bool AddDependencies(
            DependencyCollection collection,
            Type implementation,
            Type[] genericInterfaces,
            DependencyLifetime lifetime)
        {
            var contracts = ReflectionUtils.GetGenericInterfaceImplementations(implementation, genericInterfaces);

            if (contracts.Length == 0)
            {
                return(false);
            }

            contracts.Add(implementation);
            collection.AddDependency(contracts.ToArray(), implementation, lifetime);
            return(true);
        }
        private static void AddSingleton(DependencyCollection dependencies, ServiceDescriptor descriptor)
        {
            if (descriptor.ImplementationInstance != null)
            {
                dependencies.AddInstance(descriptor.ServiceType, descriptor.ImplementationInstance);
            }
            else if (descriptor.ImplementationFactory == null)
            {
                dependencies.AddSingleton(descriptor.ServiceType, descriptor.ImplementationType);
            }
            else
            {
                var contracts = new[] { descriptor.ServiceType };
                var resolver  = BuildDelegateResolver(descriptor);

                dependencies.AddDependency(new SingletonDependency(contracts, resolver));
            }
        }
        public void TryRegister(DependencyCollection collection, Type implementation)
        {
            var contracts = ReflectionUtils.GetGenericInterfaceImplementations(implementation, _processorTypes);

            if (contracts.Length == 0)
            {
                return;
            }

            if (implementation.IsGenericTypeDefinition)
            {
                var contract = contracts[0];
                collection.AddFactory(new GenericFactory(contract, implementation, _lifetime));
            }
            else
            {
                contracts.Add(implementation);
                collection.AddDependency(contracts.ToArray(), implementation, _lifetime);
            }
        }
Exemple #20
0
        private static void InsertDependentTypes(TypeReference typeRef, DependencyCollection dependencies, SourceFile sourceFile)
        {
            if (!IsGenericTypeVariable(typeRef))
            {
                dependencies.AddDependency(typeRef.FullName, DirectTypeDependency, sourceFile);
            }

            if (typeRef is GenericInstanceType)
            {
                VisitGenericArguments(((GenericInstanceType)typeRef).GenericArguments, dependencies, sourceFile);
            }
        }
Exemple #21
0
        /// <summary>
        /// This method adds all depencencies of a typeof()-argument of an attribute to the dependencies of the type.
        /// </summary>
        /// <param name="dependencies">The collection of dependencies into which all found dependencies are inserted. (Can be regared as an out-parameter.)</param>
        /// <param name="sourceFile">The corresdonding source code location.</param>
        /// <param name="parameterType">A string representing the type of the parameter.</param>
        /// <param name="parameterValue">The argument that is passed for this attribute parameter.</param>
        private static void VisitAttributeParameters(DependencyCollection dependencies, SourceFile sourceFile, string parameterType, object parameterValue)
        {
            if (parameterType.Equals("System.Type"))
            {
                // In the case of external types IL code contains the informations on the assembly defining the type
                String[] typeParts = parameterValue.ToString().Split(',');
                string cleanedDependency = typeParts[0].Replace("+", "/");

                // Remove terminating "\0"
                // Bug ID 2268: Cecil sometimes terminates constructor parameters in attributes that use typeof() with "\0". The reason therefore is unclear.
                if (cleanedDependency.EndsWith("\0"))
                {
                    cleanedDependency = cleanedDependency.Remove(cleanedDependency.Length - 2);
                }

                if (!cleanedDependency.Equals(String.Empty))
                {
                    dependencies.AddDependency(cleanedDependency, DirectTypeDependency, sourceFile);
                }
            }
            else if (parameterType.Equals("System.Type[]"))
            {
                foreach (CustomAttributeArgument arrayMember in (CustomAttributeArgument[])parameterValue)
                {
                    string memberType = arrayMember.Value.ToString();
                    // In the case of external types IL code contains the informations on the assembly defining the type
                    string[] typeParts = memberType.Split(',');
                    string cleanedDependency = typeParts[0].Replace("+", "/");
                    if (!cleanedDependency.Equals(string.Empty))
                    {
                        dependencies.AddDependency(cleanedDependency, DirectTypeDependency, sourceFile);
                    }
                }
            }
        }
Exemple #22
0
        /// <summary>
        /// Collects the dependency information for a given GenericParameterCollection.
        /// The parameter definition itself does not contain any depencencies, only the constraints for generic parameters may contain dependencies.
        /// </summary>
        /// <param name="parameters">The GenericParameterCollection to investigate.</param>
        /// <param name="dependencies">The collection of dependencies into which all found dependencies are inserted. (Can be regared as an out-parameter.)</param>
        /// <param name="sourceFile">The corresdonding source code location.</param>
        private static void VisitGenericParameters(Collection<GenericParameter> parameters, DependencyCollection dependencies, SourceFile sourceFile)
        {
            // only the types metioned in the constraints are of importance
            foreach (GenericParameter param in parameters)
            {
                foreach (TypeReference constraint in param.Constraints)
                {
                    dependencies.AddDependency(constraint.FullName, DirectTypeDependency, sourceFile);
                    VisitGenericParameters(constraint.GenericParameters, dependencies, sourceFile);

                }
            }
        }
Exemple #23
0
        /// <summary>
        /// Collects the dependency information contained in a single IL instruction.
        /// </summary>
        /// <param name="instruction">The Instruction to investigate.</param>
        /// <param name="dependencies">The collection of dependencies into which all found dependencies are inserted. (Can be regared as an out-parameter.)</param>
        private static void VisitInstruction(Instruction instruction, DependencyCollection dependencies)
        {
            if (!IsValidOperandType(instruction.OpCode.OperandType))
            {
                return;
            }

            if (instruction.Operand != null)
            {
                SourceFile sourceFile = new SourceFile(instruction.SequencePoint);
                if (instruction.Operand is GenericParameter)
                {
                    return;
                }
                else if (instruction.Operand is GenericInstanceType)
                {
                    GenericInstanceType instType = (GenericInstanceType)instruction.Operand;
                    VisitGenericArguments(instType.GenericArguments, dependencies, sourceFile);
                    dependencies.AddDependency(instType.FullName, DirectTypeDependency, sourceFile);
                }
                else if (instruction.Operand is TypeReference)
                {
                    TypeReference typeRef = (TypeReference)instruction.Operand;
                    VisitGenericParameters(typeRef.GenericParameters, dependencies, sourceFile);
                    if (!IsGenericTypeVariable(typeRef))
                    {
                        dependencies.AddDependency(typeRef.FullName, DirectTypeDependency, sourceFile);
                    }
                }
                else if (instruction.Operand is MemberReference)
                {
                    MemberReference memberRef = (MemberReference)instruction.Operand;
                    VisitMemberReference(dependencies, sourceFile, memberRef);
                }
                else if (instruction.Operand is CallSite)
                {
                    // Calls to non-managed code - currently not supported
                }
                else
                {   // This should never happen:
                    Console.WriteLine("\n\n Strange Operand found: {0}\n", instruction.Operand);
                }
            }
            else return;
        }
Exemple #24
0
        /// <summary>
        /// Collects the dependencies from an instruction that has a MemberReference as operand.
        /// </summary>
        /// <param name="dependencies">The collection of dependencies into which all found dependencies are inserted. (Can be regared as an out-parameter.)</param>
        /// <param name="sourceFile">The corresdonding source code location.</param>
        /// <param name="memberRef">The MemberReference to investigate.</param>
        private static void VisitMemberReference(DependencyCollection dependencies, SourceFile sourceFile, MemberReference memberRef)
        {
            if (memberRef.DeclaringType != null)
            {
                string memberName = memberRef.Name;
                if (memberRef is MethodReference)
                {
                    memberName = Formatter.FormatMethodReference(((MethodReference)memberRef));
                }

                dependencies.AddDependency(memberRef.DeclaringType.FullName, memberName, sourceFile);
            }

            if (memberRef.DeclaringType is GenericInstanceType)
            {
                GenericInstanceType declaringType = (GenericInstanceType)memberRef.DeclaringType;
                VisitGenericArguments(declaringType.GenericArguments, dependencies, sourceFile);
            }

            if (memberRef is MethodReference)
            {
                VisitMethodReference(dependencies, sourceFile, (MethodReference)memberRef);
            }
            else if (memberRef is FieldReference)
            {
                VisitFieldReference(dependencies, sourceFile, (FieldReference)memberRef);
            }
        }
Exemple #25
0
        /// <summary>
        /// Collects the dependency information contained in parameter lists (e.g. the parameters of a method).
        /// </summary>
        /// <param name="parameters">The ParameterDefinitionCollection to investigate.</param>
        /// <param name="dependencies">The collection of dependencies into which all found dependencies are inserted. (Can be regared as an out-parameter.)</param>
        /// <param name="sourceFile">The corresdonding source code location.</param>
        private static void VisitParameters(Collection<ParameterDefinition> parameters, DependencyCollection dependencies, SourceFile sourceFile)
        {
            foreach (ParameterDefinition paraDef in parameters)
            {
                if (!IsGenericTypeVariable(paraDef.ParameterType))
                {
                    dependencies.AddDependency(paraDef.ParameterType.FullName, DirectTypeDependency, sourceFile);
                }

                VisitGenericParameters(paraDef.ParameterType.GenericParameters, dependencies, sourceFile);
            }
        }