Esempio n. 1
0
        /// <summary>
        /// Creates a new type in the target assembly, based on yourType.
        /// </summary>
        /// <param name="yourType">Your type, which describes what kind of type to create.</param>
        /// <param name="actionAttribute">The action attribute ordering the creation.</param>
        /// <returns></returns>
        /// <exception cref="PatchDeclerationException">Thrown if this member collides with another member, and the error cannot be resolved.</exception>
        private TypeDefinition CreateNewType(TypeDefinition yourType, NewTypeAttribute actionAttribute)
        {
            if (actionAttribute.IsImplicit)
            {
                Log_implicitly_creating_member("type", yourType);
            }
            else
            {
                Log_creating_member("type", yourType);
            }

            string targetNamespace = yourType.Namespace, targetName = yourType.Name;

            if (actionAttribute.NewTypeName != null)
            {
                targetName = actionAttribute.NewTypeName;
            }
            if (actionAttribute.NewNamespace != null)
            {
                targetNamespace = actionAttribute.NewNamespace;
            }

            //sometimes, compilers generate short names with dots in them. This terribly confuses Cecil.
            if (targetName.Contains("."))
            {
                var prevName = targetName;
                targetName = targetName.Replace('.', '_');
                Log_name_changed("type", yourType, prevName, targetName);
            }
            var maybeDuplicate = TargetAssembly.MainModule.GetType(targetNamespace, targetName);

            if (maybeDuplicate != null)
            {
                var prevName = targetName;
                Log_duplicate_member("type", yourType, maybeDuplicate);
                targetName = GetNameAfterCollision(targetName);
                Log_name_changed("type", yourType, prevName, targetName);
            }

            var targetTypeDef = CopyType(yourType, targetNamespace, targetName);

            if (yourType.DeclaringType != null)
            {
                targetTypeDef.DeclaringType.NestedTypes.Add(targetTypeDef);
            }
            else
            {
                TargetAssembly.MainModule.Types.Add(targetTypeDef);
            }
            return(targetTypeDef);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a new type in the target assembly, based on yourType.
        /// </summary>
        /// <param name="yourType">Your type, which describes what kind of type to create.</param>
        /// <param name="actionAttribute">The action attribute ordering the creation.</param>
        /// <returns></returns>
        /// <exception cref="PatchDeclerationException">Thrown if this member collides with another member, and the error cannot be resolved.</exception>
        private TypeDefinition CreateNewType(TypeDefinition yourType, NewTypeAttribute actionAttribute)
        {
            if (actionAttribute.IsImplicit) {
                Log_implicitly_creating_member("type", yourType);
            } else {
                Log_creating_member("type", yourType);
            }

            string targetNamespace = yourType.Namespace, targetName = yourType.Name;
            if (actionAttribute.NewTypeName != null) {
                targetName = actionAttribute.NewTypeName;
            }
            if (actionAttribute.NewNamespace != null) {
                targetNamespace = actionAttribute.NewNamespace;
            }

            //sometimes, compilers generate short names with dots in them. This terribly confuses Cecil.
            if (targetName.Contains(".")) {
                var prevName = targetName;
                targetName = targetName.Replace('.', '_');
                Log_name_changed("type", yourType, prevName, targetName);
            }
            var maybeDuplicate = TargetAssembly.MainModule.GetType(targetNamespace, targetName);
            if (maybeDuplicate != null) {
                var prevName = targetName;
                Log_duplicate_member("type", yourType, maybeDuplicate);
                targetName = GetNameAfterCollision(targetName);
                Log_name_changed("type", yourType, prevName, targetName);
            }

            var targetTypeDef = CopyType(yourType, targetNamespace, targetName);
            if (yourType.DeclaringType != null) {
                targetTypeDef.DeclaringType.NestedTypes.Add(targetTypeDef);
            } else {
                TargetAssembly.MainModule.Types.Add(targetTypeDef);
            }
            return targetTypeDef;
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a new type in the target assembly, based on yourType.
        /// </summary>
        /// <param name="yourType">Your type, which describes what kind of type to create.</param>
        /// <param name="actionAttribute">The action attribute ordering the creation.</param>
        /// <returns></returns>
        /// <exception cref="PatchDeclerationException">Thrown if this member collides with another member, and the error cannot be resolved.</exception>
        private NewMemberStatus CreateNewType(TypeDefinition yourType, NewTypeAttribute actionAttribute)
        {
            if (actionAttribute.IsImplicit) {
                Log_implicitly_creating_member("type", yourType);
            } else {
                Log_creating_member("type", yourType);
            }
            var maybeDuplicate = TargetAssembly.MainModule.GetAllTypes().FirstOrDefault(x => x.FullName == yourType.FullName);
            if (maybeDuplicate != null) {
                Log_duplicate_member("type", yourType, maybeDuplicate);
                if ((DebugOptions & DebugFlags.CreationOverwrites) != 0) {
                    Log_overwriting();
                    return NewMemberStatus.Continue;
                }
                if (actionAttribute.IsImplicit) {
                    return NewMemberStatus.InvalidItem;
                }
                throw Errors.Duplicate_member("type", yourType.FullName, maybeDuplicate.FullName);
            }

            var targetTypeDef = new TypeDefinition(yourType.Namespace, yourType.Name, yourType.Attributes) {
                DeclaringType = yourType.DeclaringType == null ? null : FixTypeReference(yourType.DeclaringType).Resolve(),
                PackingSize = yourType.PackingSize,
                ClassSize = yourType.ClassSize,
            };

            targetTypeDef.SecurityDeclarations.AddRange(yourType.SecurityDeclarations);
            foreach (var yourTypeParam in yourType.GenericParameters) {
                var targetTypeParam = new GenericParameter(yourTypeParam.Name, targetTypeDef);
                targetTypeDef.GenericParameters.Add(targetTypeParam);
            }
            if (yourType.DeclaringType != null) {
                targetTypeDef.DeclaringType.NestedTypes.Add(targetTypeDef);
            } else {
                TargetAssembly.MainModule.Types.Add(targetTypeDef);
            }
            Log.Verbose("Created: {0}", targetTypeDef.FullName);
            //Note that
            return NewMemberStatus.Continue;
        }