Beispiel #1
0
        private Cecil.FieldDefinition CreateFieldDefinition(AnalysisNet.Types.FieldDefinition fieldDefinition)
        {
            Cecil.FieldAttributes fieldAttribute = Cecil.FieldAttributes.Public;
            if (fieldDefinition.IsStatic)
            {
                fieldAttribute |= Cecil.FieldAttributes.Static;
            }

            Cecil.TypeReference   fieldType  = ReferenceGenerator.TypeReference(fieldDefinition.Type);
            Cecil.FieldDefinition cecilField = new Cecil.FieldDefinition(fieldDefinition.Name, fieldAttribute, fieldType);

            if (fieldDefinition.Value != null)
            {
                cecilField.Constant    = fieldDefinition.Value.Value;
                cecilField.HasConstant = true;
            }

            byte[] newArray = new byte[fieldDefinition.InitialValue.Length];
            Array.Copy(fieldDefinition.InitialValue, newArray, newArray.Length);
            cecilField.InitialValue = newArray;

            if (newArray.Length > 0)
            {
                cecilField.Attributes |= Cecil.FieldAttributes.HasFieldRVA;
            }

            return(cecilField);
        }
Beispiel #2
0
        /// <summary>
        /// 生成代理类型的字段
        /// </summary>
        /// <param name="proxyType">代理类型</param>
        /// <param name="fieldName">字段名称</param>
        /// <param name="type">字段类型</param>
        /// <returns></returns>
        private FieldDefinition BuildField(TypeDefinition proxyType, string fieldName, TypeReference type)
        {
            const Mono.Cecil.FieldAttributes filedAttribute = Mono.Cecil.FieldAttributes.Private | Mono.Cecil.FieldAttributes.InitOnly;;
            var filed = new FieldDefinition(fieldName, filedAttribute, type);

            proxyType.Fields.Add(filed);
            return(filed);
        }
Beispiel #3
0
        private static void AddField(TypeDefinition targetType, string fieldName, FieldAttributes fieldAttributes, TypeReference fieldType)
        {
            if (targetType.Fields.Any(f => f.Name == fieldName))
            {
                throw new InvalidOperationException(string.Format("Field '{0}' already exist in type {1}", fieldName, targetType.FullName));
            }

            targetType.Fields.Add(new FieldDefinition(fieldName, fieldAttributes, fieldType));
        }
Beispiel #4
0
        internal void AddOrReplaceIoc(ILProcessor il)
        {
            var mod         = il.Body.Method.DeclaringType.Module;
            var myNamespace = mod.Types.Select(t => t.Namespace)
                              .Where(n => !string.IsNullOrWhiteSpace(n)).OrderBy(n => n.Length).First();
            const TAttr attr = TAttr.Class | TAttr.Public | TAttr.Sealed
                               | TAttr.Abstract | TAttr.BeforeFieldInit;
            var objBase = mod.ImportReference(typeof(object));
            var type    = new TypeDefinition(myNamespace, IocName, attr, objBase);
            var oldType = mod.Types.FirstOrDefault(t => t.FullName == type.FullName);

            if (oldType != null)
            {
                mod.Types.Remove(oldType);
            }
            mod.Types.Add(type);
            var         vesselRef = mod.ImportReference(typeof(IVessel));
            const FAttr fieldAttr = FAttr.Static | FAttr.Private;
            var         contField = new FieldDefinition(IocField, fieldAttr, vesselRef);

            type.Fields.Add(contField);
            const MAttr getAttrs = MAttr.Static | MAttr.Public
                                   | MAttr.SpecialName | MAttr.HideBySig;
            var getMethod = new MethodDefinition(IocMethod, getAttrs, vesselRef);

            type.Methods.Add(getMethod);
            ScopeMethod = getMethod;
            IocType     = type;
            var gmil = getMethod.Body.GetILProcessor();

            gmil.Append(gmil.Create(OpCodes.Ldsfld, contField));
            gmil.Append(gmil.Create(OpCodes.Ret));
            var         voidRef     = mod.ImportReference(typeof(void));
            const MAttr constrAttrs = MAttr.Static | MAttr.SpecialName | MAttr.Private
                                      | MAttr.RTSpecialName | MAttr.HideBySig;
            var constr = new MethodDefinition(Defaults.StaticConstrName, constrAttrs, voidRef);

            type.Methods.Add(constr);
            var cil       = constr.Body.GetILProcessor();
            var multiMeth = typeof(DefaultVessel).GetConstructors().First();
            var multiRef  = mod.ImportReference(multiMeth);

            cil.Append(cil.Create(OpCodes.Newobj, multiRef));
            cil.Append(cil.Create(OpCodes.Stsfld, contField));
            cil.Append(cil.Create(OpCodes.Ret));
            il.Append(il.Create(OpCodes.Call, getMethod));
            il.Append(il.Create(OpCodes.Pop));
            il.Append(il.Create(OpCodes.Ret));
        }
Beispiel #5
0
    void AddVersionField(Assembly assembly, string name, TypeDefinition typeDefinition)
    {
        var weaverVersion = "0.0.0.0";
        var attrs = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute));
        var fileVersionAttribute = (AssemblyFileVersionAttribute)attrs.FirstOrDefault();
        if (fileVersionAttribute != null)
        {
            weaverVersion = fileVersionAttribute.Version;
        }

        const FieldAttributes fieldAttributes = FieldAttributes.Assembly |
                                                FieldAttributes.Literal |
                                                FieldAttributes.Static |
                                                FieldAttributes.HasDefault;
        var field = new FieldDefinition(name, fieldAttributes, TypeSystem.StringReference)
        {
            Constant = weaverVersion
        };

        typeDefinition.Fields.Add(field);
    }
Beispiel #6
0
        public override void VisitFieldDefinition(FieldDefinition field)
        {
            if (!_includeFields)
            {
                return;
            }

            FieldElement fe = new FieldElement();

            fe.Name     = field.Name;
            fe.Type     = field.FieldType.FullName;
            fe.IsStatic = field.IsStatic;

            FieldAttributes fieldAccess = field.Attributes & FieldAttributes.FieldAccessMask;

            fe.IsPrivate = fieldAccess == FieldAttributes.Private;
            fe.IsPublic  = fieldAccess == FieldAttributes.Public;

            // Custom attributes
            fe.Attributes.AddRange(ExtractCustomAttributes(field.CustomAttributes));

            _currentType.Fields.Add(fe);
        }
Beispiel #7
0
 public static GenericFuncContainer <FieldDefinition, bool> FieldNegAttributeComparer(Mono.Cecil.FieldAttributes negAttrs)
 {
     return(new GenericFuncContainer <FieldDefinition, bool>(field => {
         return (field.Attributes & negAttrs) == 0;
     }));
 }
        public static FieldDefinition AddField(this TypeDefinition type, string name, FieldAttributes attributes, TypeReference fieldType)
        {
            FieldDefinition field = new FieldDefinition(name, attributes, fieldType);

            type.Fields.Add(field);
            return(field);
        }
 public static FieldDefinition AddField <T>(this TypeDefinition type, string name, FieldAttributes attributes)
 {
     return(type.AddField(name, attributes, type.Module.GetTypeReference <T>()));
 }
        public static FieldDefinition GetOrCreateField(this TypeDefinition self, string name, Mono.Cecil.FieldAttributes attributes, TypeReference fieldType)
        {
            var field = self.Fields.FirstOrDefault(a => a.Name == name);

            if (null == field)
            {
                field = new FieldDefinition(name, attributes, fieldType);
                self.Fields.Add(field);
            }

            return(field);
        }