Esempio n. 1
0
        public virtual void Weave(TypeDefinition item)
        {
            if (item.Interfaces.Contains(_hostType))
            {
                return;
            }

            item.Interfaces.Add(_hostType);
            item.AddProperty("MethodBodyReplacementProvider", typeof(IMethodReplacementProvider));
            item.AddProperty("MethodCallReplacementProvider", typeof(IMethodReplacementProvider));
        }
        public override void Weave(TypeDefinition item)
        {
            base.Weave(item);

            // Implement IModifiableType
            if (item.Interfaces.Contains(_modifiableInterfaceType))
            {
                return;
            }

            item.Interfaces.Add(_modifiableInterfaceType);
            item.AddProperty("IsInterceptionDisabled", typeof(bool));
            item.AddProperty("AroundInvokeProvider", typeof(IAroundInvokeProvider));
        }
        /// <summary>
        /// Adds a rewritable property to the <paramref name="typeDef">target type</paramref>.
        /// </summary>
        /// <param name="typeDef">The target type that will hold the newly-created property.</param>
        /// <param name="propertyName">The name of the property itself.</param>
        /// <param name="propertyType">The <see cref="TypeReference"/> instance that describes the property type.</param>
        public static void AddProperty(this TypeDefinition typeDef, string propertyName,
                                       TypeReference propertyType)
        {
            var fieldName   = string.Format("__{0}_backingField", propertyName);
            var actualField = new FieldDefinition(fieldName, FieldAttributes.Private,
                                                  propertyType);


            typeDef.Fields.Add(actualField);


            FieldReference backingField = actualField;

            if (typeDef.GenericParameters.Count > 0)
            {
                backingField = GetBackingField(fieldName, typeDef, propertyType);
            }

            var getterName = string.Format("get_{0}", propertyName);
            var setterName = string.Format("set_{0}", propertyName);


            const MethodAttributes attributes = MethodAttributes.Public | MethodAttributes.HideBySig |
                                                MethodAttributes.SpecialName | MethodAttributes.NewSlot |
                                                MethodAttributes.Virtual;

            var module   = typeDef.Module;
            var voidType = module.Import(typeof(void));

            // Implement the getter and the setter
            var getter = AddPropertyGetter(propertyType, getterName, attributes, backingField);
            var setter = AddPropertySetter(propertyType, attributes, backingField, setterName, voidType);

            typeDef.AddProperty(propertyName, propertyType, getter, setter);
        }
        /// <summary>
        /// Adds a rewritable property to the <paramref name="typeDef">target type</paramref>.
        /// </summary>
        /// <param name="typeDef">The target type that will hold the newly-created property.</param>
        /// <param name="propertyName">The name of the property itself.</param>
        /// <param name="propertyType">The <see cref="System.Type"/> instance that describes the property type.</param>
        public static void AddProperty(this TypeDefinition typeDef, string propertyName, Type propertyType)
        {
            var module  = typeDef.Module;
            var typeRef = module.Import(propertyType);

            typeDef.AddProperty(propertyName, typeRef);
        }
Esempio n. 5
0
        /// <summary>
        ///     Constructs a type that implements the
        ///     <see cref="IProxy" /> interface.
        /// </summary>
        /// <param name="module">The module that will hold the target type.</param>
        /// <param name="targetType">The type that will implement the <see cref="IProxy" /> interface.</param>
        public void Construct(ModuleDefinition module, TypeDefinition targetType)
        {
            var proxyInterfaceType = module.Import(typeof(IProxy));
            var interceptorType    = module.Import(typeof(IInterceptor));

            // Implement the IProxy interface only once
            if (targetType.Interfaces.Contains(proxyInterfaceType))
            {
                return;
            }

            targetType.Interfaces.Add(proxyInterfaceType);
            targetType.AddProperty("Interceptor", interceptorType);
        }
Esempio n. 6
0
        /// <summary>
        /// Modifies the target type.
        /// </summary>
        /// <param name="type">The type to be modified.</param>
        public void Weave(TypeDefinition type)
        {
            // Implement IActivatorHost only once
            if (type.Interfaces.Contains(_hostInterfaceType))
            {
                return;
            }

            type.AddProperty("FieldInterceptor", _interceptorPropertyType);

            if (!type.Interfaces.Contains(_hostInterfaceType))
            {
                type.Interfaces.Add(_hostInterfaceType);
            }
        }
 public static PropertyDefinition AddProperty <T>(this TypeDefinition type, string name, PropertyAttributes attributes)
 {
     return(type.AddProperty(name, attributes, type.Module.GetTypeReference <T>()));
 }
Esempio n. 8
0
        private void AddTypeDefinition(TypeDefinition owningType, string name, PapyrusTypeDefinition type, bool isNested)
        {
            if (mainModule.Types.Any(t => t.Name.ToLower() == name.ToLower()))
            {
                // Type already exists? Don't do anything.
                return;
            }

            var newType = new TypeDefinition(NamespaceResolver.Resolve(name), name,
                                             isNested
                    ? TypeAttributes.NestedPublic | TypeAttributes.SequentialLayout | TypeAttributes.BeforeFieldInit |
                                             TypeAttributes.Sealed
                    : TypeAttributes.Public | TypeAttributes.Class);

            if (isNested)
            {
                newType.IsClass  = false;
                newType.BaseType = mainModule.Import(typeof(ValueType));

                if (owningType.NestedTypes.Any(t => t.Name.ToLower() == name.ToLower()))
                {
                    // Structure already exists? Don't do anything.
                    return;
                }
                owningType.NestedTypes.Add(newType);
            }
            else
            {
                mainModule.Types.Add(newType);
            }

            AddEmptyConstructor(newType);

            if (!isNested)
            {
                if (!string.IsNullOrEmpty(type.BaseTypeName?.Value))
                {
                    var baseType = ResolveTypeReference(null, type.BaseTypeName.Value);
                    newType.BaseType = baseType ?? objectType;
                }
                else
                {
                    newType.BaseType = objectType;
                }
            }

            foreach (var field in type.Fields)
            {
                var fieldType = field.DefaultValue;
                var typeName  = fieldType.Name;
                var typeRef   = ResolveTypeReference(null, typeName);

                var attributes = FieldAttributes.Public;

                if (field.Name.Value.ToLower().EndsWith("_var"))
                {
                    if (type.Properties.Any(
                            n => field.Name.Value.Contains('_') && n.Name.Value == field.Name.Value.Split('_')[0] ||
                            n.AutoName == field.Name.Value))
                    {
                        attributes = FieldAttributes.Private;
                    }
                }

                //if (field.IsConst)
                //{
                //    attributes |= FieldAttributes.InitOnly;
                //}

                var fieldDef = new FieldDefinition(field.Name.Value.Replace("::", ""), attributes, typeRef);
                newType.Fields.Add(fieldDef);
            }

            foreach (var prop in type.Properties)
            {
                FieldDefinition targetField = null;
                foreach (var field in newType.Fields)
                {
                    if (!string.IsNullOrEmpty(prop.AutoName))
                    {
                        if (prop.AutoName.Contains(field.Name))
                        {
                            targetField = field;
                            break;
                        }
                    }
                    if (field.Name.ToLower().Contains(prop.Name.Value.ToLower() + "_var"))
                    {
                        targetField = field;
                        break;
                    }
                }

                var typeRef = ResolveTypeReference(null, prop.TypeName);

                newType.AddProperty(nameConventionResolver.Resolve(prop.Name.Value), typeRef, targetField);
            }


            foreach (var structure in type.NestedTypes)
            {
                AddTypeDefinition(newType, structure.Name, structure, true);
            }

            foreach (var state in type.States)
            {
                foreach (var method in state.Methods)
                {
                    method.Name.Value = nameConventionResolver.Resolve(method.Name.Value);

                    var typeRef    = ResolveTypeReference(null, method.ReturnTypeName);
                    var attributes = MethodAttributes.Public;

                    if (method.IsGlobal /* || method.IsNative */)
                    {
                        attributes |= MethodAttributes.Static;
                    }
                    else if (method.IsEvent)
                    {
                        attributes |= MethodAttributes.Virtual;
                        attributes |= MethodAttributes.NewSlot;
                    }


                    var methodDef = new MethodDefinition(method.Name.Value, attributes, typeRef);

                    // methodDef.IsNative = method.IsNative;
                    foreach (var param in method.Parameters)
                    {
                        var paramTypeRef = ResolveTypeReference(null, param.TypeName);
                        var paramDef     = new ParameterDefinition(param.Name.Value, ParameterAttributes.None, paramTypeRef);
                        methodDef.Parameters.Add(paramDef);
                    }

                    var existingMethod =
                        newType.Methods.Any(m => m.Name == methodDef.Name &&
                                            methodDef.ReturnType == typeRef &&
                                            methodDef.Parameters.Count == m.Parameters.Count
                                            );

                    if (!existingMethod)
                    {
                        CreateEmptyFunctionBody(ref methodDef);
                        newType.Methods.Add(methodDef);
                    }
                }
            }
            // return newType;
        }