示例#1
0
        public Type GetEnhancedType(Type typeToEnhance, String newTypeNamePrefix, IEnhancementHint hint)
        {
            Type extendedType = GetEnhancedTypeIntern(typeToEnhance, hint);

            if (extendedType != null)
            {
                return(extendedType);
            }
            lock (writeLock)
            {
                // Concurrent thread may have been faster
                extendedType = GetEnhancedTypeIntern(typeToEnhance, hint);
                if (extendedType != null)
                {
                    return(extendedType);
                }
                if (Log.InfoEnabled)
                {
                    Log.Info("Enhancing " + typeToEnhance + " with hint: " + hint);
                }
                ValueType valueType = DictionaryExtension.ValueOrDefault(typeToExtendedType, typeToEnhance);
                if (valueType == null)
                {
                    valueType = new ValueType();
                    typeToExtendedType.Add(typeToEnhance, valueType);
                }
                else
                {
                    valueType.AddChangeCount();
                    newTypeNamePrefix += "_O" + valueType.ChangeCount;
                }

                List <IBytecodeBehavior> pendingBehaviors = new List <IBytecodeBehavior>();

                IBytecodeBehavior[] extensions = bytecodeBehaviorExtensions.GetExtensions();
                pendingBehaviors.AddRange(extensions);

                Type enhancedType;
                if (pendingBehaviors.Count > 0)
                {
                    enhancedType = EnhanceTypeIntern(typeToEnhance, newTypeNamePrefix, pendingBehaviors, hint);
                }
                else
                {
                    enhancedType = typeToEnhance;
                }
                WeakReference entityTypeR = typeToExtendedType.GetWeakReferenceEntry(typeToEnhance);
                if (entityTypeR == null)
                {
                    throw new Exception("Must never happen");
                }
                hardRefToTypes.Add(enhancedType);
                hardRefToTypes.Add(typeToEnhance);

                if (TraceDir != null)
                {
                    LogBytecodeOutput(enhancedType.FullName, BytecodeClassLoader.ToPrintableBytecode(enhancedType));
                }
                else if (Log.DebugEnabled)
                {
                    // note that this intentionally will only be logged to the console if the traceDir is NOT specified already
                    Log.Debug(BytecodeClassLoader.ToPrintableBytecode(enhancedType));
                }
                try
                {
                    CheckEnhancedTypeConsistency(enhancedType);
                }
                catch (Exception e)
                {
                    if (Log.ErrorEnabled)
                    {
                        Log.Error(BytecodeClassLoader.ToPrintableBytecode(enhancedType), e);
                    }
                    BytecodeClassLoader.Save();
                    throw;
                }
                WeakReference enhancedEntityTypeR = new WeakReference(enhancedType);
                valueType.Put(hint, enhancedEntityTypeR);
                extendedTypeToType.Add(enhancedType, entityTypeR);
                if (Log.InfoEnabled)
                {
                    Log.Info("Enhancement finished successfully with type: " + enhancedType);
                }
                return(enhancedType);
            }
        }