Example #1
0
 public override void ClearCache()
 {
     _resolutionScope = null;
     _name            = null;
     _namespace       = null;
     _fullname        = null;
 }
Example #2
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="module">Owner module</param>
 /// <param name="namespace">Type namespace</param>
 /// <param name="name">Type name</param>
 /// <param name="resolutionScope">Resolution scope (a <see cref="ModuleDef"/>,
 /// <see cref="ModuleRef"/>, <see cref="AssemblyRef"/> or <see cref="TypeRef"/>)</param>
 public TypeRefUser(ModuleDef module, UTF8String @namespace, UTF8String name, IResolutionScope resolutionScope)
 {
     this.module          = module;
     this.resolutionScope = resolutionScope;
     this.name            = name;
     this.@namespace      = @namespace;
 }
Example #3
0
        private static TValue CollectScopedInstance <TValue>(IResolutionScope scope, Func <IResolutionScope, object> factory, object scopeId)
            where TValue : class
        {
            if (HttpContext.Current == null)
            {
                return(null);
            }

            if (HttpContext.Current.Items[scopeId] != null)
            {
                return(HttpContext.Current.Items[scopeId] as TValue);
            }

            TValue instance;

            if (HttpContext.Current.Items[StashboxPerRequestScopeProvider.ScopeKey] is IResolutionScope requestScope)
            {
                instance = factory(requestScope) as TValue;
                HttpContext.Current.Items[scopeId] = instance;
            }
            else
            {
                instance = factory(scope) as TValue;
            }

            return(instance);
        }
Example #4
0
        private Delegate ActivateFactoryDelegate(Type type, Type[] parameterTypes, IResolutionScope resolutionScope, object name, bool nullResultAllowed)
        {
            var resolutionContext = ResolutionContext.New(resolutionScope, nullResultAllowed);

            resolutionContext.AddParameterExpressions(type, parameterTypes.Select(p => p.AsParameter()).ToArray());

            var typeInfo = new TypeInformation {
                Type = type, DependencyName = name
            };
            var registration = this.containerContext.RegistrationRepository.GetRegistrationOrDefault(typeInfo, resolutionContext);

            var initExpression = registration == null?
                                 this.resolverSelector.GetResolverExpression(this.containerContext, typeInfo, resolutionContext) :
                                     registration.GetExpression(this.containerContext, resolutionContext, type);

            if (initExpression == null)
            {
                if (resolutionContext.NullResultAllowed)
                {
                    return(null);
                }
                else
                {
                    throw new ResolutionFailedException(type);
                }
            }

            var expression = initExpression.AsLambda(resolutionContext.ParameterExpressions.SelectMany(x => x));

            var factory = expression.CompileDynamicDelegate(resolutionContext);

            Swap.SwapValue(ref this.delegateCache.FactoryDelegates, (t1, t2, t3, t4, c) =>
                           c.AddOrUpdate(t1, t2), name ?? type, factory, Constants.DelegatePlaceholder, Constants.DelegatePlaceholder);
            return(factory(resolutionScope));
        }
Example #5
0
        private Delegate ActivateFactoryDelegate(Type type, Type[] parameterTypes, IResolutionScope resolutionScope, object name, bool nullResultAllowed)
        {
            var resolutionInfo = new ResolutionInfo(resolutionScope, this.rootScope, nullResultAllowed)
            {
                ParameterExpressions = parameterTypes.Length == 0 ? null : parameterTypes.Select(Expression.Parameter).ToArray()
            };

            var typeInfo = new TypeInformation {
                Type = type, DependencyName = name
            };
            var registration = this.containerContext.RegistrationRepository.GetRegistrationOrDefault(typeInfo);

            var initExpression = registration == null?
                                 this.resolverSelector.GetResolverExpression(this.containerContext, typeInfo, resolutionInfo) :
                                     registration.GetExpression(resolutionInfo, type);

            if (initExpression == null)
            {
                if (resolutionInfo.NullResultAllowed)
                {
                    return(null);
                }
                else
                {
                    throw new ResolutionFailedException(type);
                }
            }

            var expression = Expression.Lambda(initExpression, resolutionInfo.ParameterExpressions);

            var factory = expression.CompileDelegate(Constants.ScopeExpression);

            this.containerContext.DelegateRepository.AddFactoryDelegate(type, factory, name);
            return(factory(resolutionScope));
        }
Example #6
0
        public MetadataToken GetResolutionScopeToken(IResolutionScope scope)
        {
            var assemblyRef = scope as AssemblyReference;

            if (assemblyRef != null)
            {
                return(GetAssemblyReferenceToken(assemblyRef));
            }

            var moduleDef = scope as ModuleDefinition;

            if (moduleDef != null)
            {
                return(GetNewToken(moduleDef));
            }

            var moduleRef = scope as ModuleReference;

            if (moduleRef != null)
            {
                return(GetModuleReferenceToken(moduleRef));
            }

            var typeRef = scope as TypeReference;

            if (typeRef != null)
            {
                return(GetTypeReferenceToken(typeRef));
            }

            throw new NotSupportedException("Invalid or unsupported ResolutionScope reference " + scope + ".");
        }
Example #7
0
 /// <summary>
 /// Creates a new reference to a type.
 /// </summary>
 /// <param name="scope">The scope that defines the type.</param>
 /// <param name="ns">The namespace the type resides in.</param>
 /// <param name="name">The name of the type.</param>
 public TypeReference(IResolutionScope scope, string ns, string name)
     : this(new MetadataToken(TableIndex.TypeRef, 0))
 {
     _scope.Value = scope;
     Namespace    = ns;
     Name         = name;
 }
Example #8
0
        /// <summary>
        /// Determines whether two scope descriptors are considered equal according to their signature.
        /// </summary>
        /// <param name="scope1">The first scope to compare.</param>
        /// <param name="scope2">The second scope to compare.</param>
        /// <returns><c>True</c> if the scope are considered equal, <c>False</c> otherwise.</returns>
        public bool Equals(IResolutionScope scope1, IResolutionScope scope2)
        {
            if (scope1 == null && scope2 == null)
            {
                return(true);
            }
            if (scope1 == null || scope2 == null)
            {
                return(false);
            }

            switch (scope1)
            {
            case ModuleDefinition module:
                return(Equals(module, scope2 as ModuleDefinition));

            case ModuleReference moduleRef:
                return(Equals(moduleRef, scope2 as ModuleReference));

            case AssemblyReference assemblyRef:
                return(Equals((IAssemblyDescriptor)assemblyRef, scope2 as AssemblyReference));

            case TypeReference typeRef:
                return(Equals((ITypeDefOrRef)typeRef, scope2 as TypeReference));
            }

            return(false);
        }
Example #9
0
 private ResolutionScope(IResolverSelector resolverSelector, IExpressionBuilder expressionBuilder,
                         IContainerContext containerContext, IResolutionScope rootScope, IResolutionScope parent, DelegateCache delegateCache, object name = null)
     : this(resolverSelector, expressionBuilder, containerContext, delegateCache, name)
 {
     this.RootScope   = rootScope;
     this.ParentScope = parent;
 }
Example #10
0
 public override void ClearCache()
 {
     resolutionScope = null;
     name            = null;
     @namespace      = null;
     fullname        = null;
 }
Example #11
0
 public TypeReference(string @namespace, string name, IResolutionScope resolutionScope)
     : base(new MetaDataRow(0U, 0U, 0U))
 {
     this._name = name;
     this._namespace = @namespace;
     this._resolutionScope = resolutionScope;
 }
Example #12
0
 public TypeReference(string @namespace, string name, IResolutionScope resolutionScope)
     : base(new MetaDataRow(0U, 0U, 0U))
 {
     this._name            = name;
     this._namespace       = @namespace;
     this._resolutionScope = resolutionScope;
 }
Example #13
0
        public int GetHashCode(IResolutionScope obj)
        {
            var module = obj as ModuleDefinition;

            if (module != null)
            {
                return(GetHashCode(module));
            }

            var moduleRef = obj as ModuleReference;

            if (moduleRef != null)
            {
                return(GetHashCode(moduleRef));
            }

            var assemblyRef = obj as AssemblyReference;

            if (assemblyRef != null)
            {
                return(GetHashCode((IAssemblyDescriptor)assemblyRef));
            }

            var typeRef = obj as TypeReference;

            if (typeRef != null)
            {
                return(GetHashCode((ITypeDescriptor)typeRef));
            }

            return(obj.GetHashCode());
        }
Example #14
0
 public TypeReference(IResolutionScope resolutionScope, string @namespace, string name)
     : base(null, new MetadataToken(MetadataTokenType.TypeRef), new MetadataRow <uint, uint, uint>())
 {
     ResolutionScope = resolutionScope;
     _namespace      = new LazyValue <string>(@namespace);
     _name           = new LazyValue <string>(name);
 }
Example #15
0
        public IResolutionScope ImportScope(IResolutionScope scope)
        {
            var assemblyRef = scope as AssemblyReference;

            if (assemblyRef != null)
            {
                return(ImportAssembly(assemblyRef));
            }

            var typeRef = scope as TypeReference;

            if (typeRef != null)
            {
                return(ImportType(typeRef));
            }

            var moduleRef = scope as ModuleReference;

            if (moduleRef != null)
            {
                return(ImportModule(moduleRef));
            }

            throw new NotSupportedException();
        }
Example #16
0
 public ResolutionScope(IActivationContext activationContext, IServiceRegistrator serviceRegistrator,
                        IExpressionBuilder expressionBuilder, IResolutionScope rootScope)
 {
     this.activationContext  = activationContext;
     this.serviceRegistrator = serviceRegistrator;
     this.expressionBuilder  = expressionBuilder;
     this.rootScope          = rootScope;
 }
Example #17
0
 public override void LoadCache()
 {
     base.LoadCache();
     _resolutionScope = ResolutionScope;
     _name            = Name;
     _namespace       = Namespace;
     _fullname        = FullName;
 }
Example #18
0
 public ResolutionScope(IResolverSelector resolverSelector, IServiceRegistrator serviceRegistrator, IExpressionBuilder expressionBuilder,
                        IContainerContext containerContext, IResolutionScope rootScope, IResolutionScope parent, AvlTreeKeyValue <object, Func <IResolutionScope, object> >[] serviceDelegateCache,
                        AvlTreeKeyValue <object, Func <IResolutionScope, Delegate> >[] factoryDelegates, object name = null)
     : this(resolverSelector, serviceRegistrator, expressionBuilder, containerContext, serviceDelegateCache, factoryDelegates, name)
 {
     this.RootScope   = rootScope;
     this.ParentScope = parent;
 }
Example #19
0
        private static TypeReference CreateTypeReference(IResolutionScope scope, string fullName)
        {
            var dotIndex = fullName.LastIndexOf('.');

            return(dotIndex == -1
                ? new TypeReference(scope, string.Empty, fullName)
                : new TypeReference(scope, fullName.Remove(dotIndex), fullName.Substring(dotIndex + 1)));
        }
Example #20
0
        public TypeReference(IResolutionScope resolutionScope, string @namespace, string name)
            : base(new MetadataToken(MetadataTokenType.TypeRef))
        {
            _resolutionScope = new LazyValue <IResolutionScope>(resolutionScope);
            _namespace       = new LazyValue <string>(@namespace);
            _name            = new LazyValue <string>(name);

            CustomAttributes = new CustomAttributeCollection(this);
        }
Example #21
0
 internal ResolutionInfo(IResolutionScope scope, IResolutionScope rootScope, bool nullResultAllowed = false)
 {
     this.circularDependencyBarrier = AvlTree <Type> .Empty;
     this.expressionOverrides       = AvlTree <Expression> .Empty;
     this.currentlyDecoratingTypes  = AvlTree <Type> .Empty;
     this.NullResultAllowed         = nullResultAllowed;
     this.ResolutionScope           = scope;
     this.RootScope = rootScope;
 }
Example #22
0
        private static TypeSignature ReadTypeSignature(IResolutionScope scope, string name, ref int position)
        {
            TypeSignature type = null;

            while (position < name.Length && name[position] != ',')
            {
                type = ReadTypeSignature(ReadTypeDefOrRefSignature(scope, name, ref position), name, ref position);
            }
            return(type);
        }
Example #23
0
 private static TypeReference CreateTypeReference(IResolutionScope scope, string fullName)
 {
     var dotIndex = fullName.LastIndexOf('.');
     var type = dotIndex == -1
         ? new TypeReference(scope, string.Empty, fullName)
         : new TypeReference(scope, fullName.Remove(dotIndex), fullName.Substring(dotIndex + 1));
     if (scope != null)
         type.Header = scope.Header;
     return type;
 }
Example #24
0
        private static TValue GetScopedValue <TValue>(IResolutionScope scope, Func <IResolutionScope, object> factory, object scopeId)
        {
            var value = scope.GetScopedItemOrDefault(scopeId);

            if (value == null)
            {
                value = factory(scope);
                scope.AddScopedItem(scopeId, value);
            }

            return((TValue)value);
        }
Example #25
0
        public ITypeDefOrRef ImportType(Type type)
        {
            // TODO: support more complex type references.

            IResolutionScope resolutionScope = type.IsNested
                ? (IResolutionScope)ImportType(type.DeclaringType)
                : ImportAssembly(type.Assembly.GetName());

            return(ImportType(new TypeReference(
                                  resolutionScope,
                                  type.IsNested ? string.Empty : type.Namespace,
                                  type.Name)));
        }
Example #26
0
        private static TypeReference CreateTypeReference(IResolutionScope scope, string fullName)
        {
            var dotIndex = fullName.LastIndexOf('.');
            var type     = dotIndex == -1
                ? new TypeReference(scope, string.Empty, fullName)
                : new TypeReference(scope, fullName.Remove(dotIndex), fullName.Substring(dotIndex + 1));

            if (scope != null)
            {
                type.Header = scope.Header;
            }
            return(type);
        }
Example #27
0
            TypeRef Import2(TypeRef type, IResolutionScope scope)
            {
                if (type is null)
                {
                    return(null);
                }
                TypeRef result;

                var declaringType = type.DeclaringType;

                if (!(declaringType is null))
                {
                    result = new TypeRefUser(TargetModule, type.Namespace, type.Name, Import2(declaringType, scope));
                }
Example #28
0
        internal CorLibTypeSignature(IResolutionScope corlibScope, ElementType elementType, string name)
        {
            Scope       = corlibScope;
            ElementType = elementType;
            Name        = name;

            if (corlibScope is ModuleDefinition module)
            {
                Type = module.TopLevelTypes.First(t => t.IsTypeOf(Namespace, Name));
            }
            else
            {
                Type = new TypeReference(corlibScope.Module, corlibScope, Namespace, Name);
            }
        }
Example #29
0
            public object Evaluate(IResolutionScope scope, Func <IResolutionScope, object> factory, Type serviceType)
            {
                if (!ReferenceEquals(this.evaluatedObject, Default))
                {
                    return(this.evaluatedObject);
                }

                if (Interlocked.CompareExchange(ref this.evaluated, 1, 0) != 0)
                {
                    return(this.WaitForEvaluation(serviceType));
                }

                this.evaluatedObject = factory(scope);
                return(this.evaluatedObject);
            }
        public ClassOrValueTypeSig Type(bool isValueType, string ns, string name, IResolutionScope resolutionScope)
        {
            var typeRef = module.UpdateRowId(new TypeRefUser(module, ns, name, resolutionScope));
            ClassOrValueTypeSig type;

            if (isValueType)
            {
                type = new ValueTypeSig(typeRef);
            }
            else
            {
                type = new ClassSig(typeRef);
            }
            return((ClassOrValueTypeSig)Add(type));
        }
Example #31
0
        private static TValue GetScopedValue <TValue>(IResolutionScope currentScope, Func <IResolutionScope, object> factory, int scopeId, object sync, object scopeName)
        {
            var scope = currentScope;

            while (scope != null && scope.Name != scopeName)
            {
                scope = scope.ParentScope;
            }

            if (scope == null)
            {
                throw new InvalidOperationException($"The scope '{scopeName}' not found.");
            }

            return((TValue)scope.GetOrAddScopedItem(scopeId, sync, factory));
        }
Example #32
0
        void InitializeResolutionScope()
        {
#if THREAD_SAFE
            theLock.EnterWriteLock(); try {
#endif
            if (resolutionScope_isInitialized)
            {
                return;
            }
            resolutionScope = GetResolutionScope_NoLock();
            resolutionScope_isInitialized = true;
#if THREAD_SAFE
        }
        finally { theLock.ExitWriteLock(); }
#endif
        }
		internal void SetParent(IResolutionScope par) 
		{
			parent = par;
		}
Example #34
0
        /// <summary>
        /// Determines whether two scope descriptors are considered equal according to their signature.
        /// </summary>
        /// <param name="scope1">The first scope to compare.</param>
        /// <param name="scope2">The second scope to compare.</param>
        /// <returns><c>True</c> if the scope are considered equal, <c>False</c> otherwise.</returns>
        public bool MatchScopes(IResolutionScope scope1, IResolutionScope scope2)
        {
            if (scope1 == null && scope2 == null)
                return true;
            if (scope1 == null || scope2 == null)
                return false;

            var module = scope1 as ModuleDefinition;
            if (module != null)
                return MatchModules(module, scope2 as ModuleDefinition);

            var moduleRef = scope1 as ModuleReference;
            if (moduleRef != null)
                return MatchModules(moduleRef, scope2 as ModuleReference);

            var assemblyRef = scope1 as AssemblyReference;
            if (assemblyRef != null)
                return MatchAssemblies(assemblyRef, scope2 as AssemblyReference);

            var typeRef = scope1 as TypeReference;
            if (typeRef != null)
                return MatchTypes(typeRef, scope2 as TypeReference);

            return false;
        }
Example #35
0
 public override void ClearCache()
 {
     _resolutionScope = null;
     _name = null;
     _namespace = null;
     _fullname = null;
 }
Example #36
0
 public override void LoadCache()
 {
     base.LoadCache();
     _resolutionScope = ResolutionScope;
     _name = Name;
     _namespace = Namespace;
     _fullname = FullName;
 }
Example #37
0
        public IResolutionScope ImportScope(IResolutionScope scope)
        {
            var assemblyRef = scope as AssemblyReference;
            if (assemblyRef != null)
                return ImportAssembly(assemblyRef);

            var typeRef = scope as TypeReference;
            if (typeRef != null)
                return ImportType(typeRef);

            var moduleRef = scope as ModuleReference;
            if (moduleRef != null)
                return ImportModule(moduleRef);

            throw new NotSupportedException();
        }
Example #38
0
        private static TypeDefOrRefSignature ReadTypeDefOrRefSignature(IResolutionScope scope, string name, ref int position)
        {
            TypeReference type = null;

            while (position < name.Length)
            {
                var typeName = ReadTypeName(name, ref position);
                type = CreateTypeReference(type ?? scope, typeName);

                if (position < name.Length && name[position] == '+')
                    position++;
                else
                    break;
            }

            return new TypeDefOrRefSignature(type);
        }
Example #39
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="module">Owner module</param>
		/// <param name="namespace">Type namespace</param>
		/// <param name="name">Type name</param>
		/// <param name="resolutionScope">Resolution scope (a <see cref="ModuleDef"/>,
		/// <see cref="ModuleRef"/>, <see cref="AssemblyRef"/> or <see cref="TypeRef"/>)</param>
		public TypeRefUser(ModuleDef module, UTF8String @namespace, UTF8String name, IResolutionScope resolutionScope) {
			this.module = module;
			this.resolutionScope = resolutionScope;
			this.resolutionScope_isInitialized = true;
			this.name = name;
			this.@namespace = @namespace;
		}
Example #40
0
		void InitializeResolutionScope() {
#if THREAD_SAFE
			theLock.EnterWriteLock(); try {
#endif
			if (resolutionScope_isInitialized)
				return;
			resolutionScope = GetResolutionScope_NoLock();
			resolutionScope_isInitialized = true;
#if THREAD_SAFE
			} finally { theLock.ExitWriteLock(); }
#endif
		}
Example #41
0
 private static TypeSignature ReadTypeSignature(IResolutionScope scope, string name, ref int position)
 {
     TypeSignature type = null;
     while (position < name.Length && name[position] != ',')
     {
         type = ReadTypeSignature(ReadTypeDefOrRefSignature(scope, name, ref position), name, ref position);
     }
     return type;
 }
		public ClassOrValueTypeSig Type(bool isValueType, string ns, string name, IResolutionScope resolutionScope) {
			var typeRef = module.UpdateRowId(new TypeRefUser(module, ns, name, resolutionScope));
			ClassOrValueTypeSig type;
			if (isValueType)
				type = new ValueTypeSig(typeRef);
			else
				type = new ClassSig(typeRef);
			return (ClassOrValueTypeSig)Add(type);
		}