public IField GetField(object token) { IField __field = null; if (fieldCache.TryGetValue(token.GetHashCode(), out __field)) { return(__field); } if (token is Mono.Cecil.FieldDefinition) { Mono.Cecil.FieldDefinition field = token as Mono.Cecil.FieldDefinition; var type = GetType(field.DeclaringType.FullName); __field = type.GetField(field.Name); } else if (token is Mono.Cecil.FieldReference) { Mono.Cecil.FieldReference field = token as Mono.Cecil.FieldReference; var type = GetType(field.DeclaringType.FullName); __field = type.GetField(field.Name); } //else if(token is CLRSharp_Instance) // { //CLRSharp_Instance inst = token as CLRSharp_Instance; //return inst.Fields[field.Name]; // } else { throw new NotImplementedException("不可处理的token" + token.GetType().ToString()); } fieldCache[token.GetHashCode()] = __field; return(__field); }
/// <summary> /// Set the access flags of the created field. /// </summary> protected virtual void SetAccessFlags(DexLib.FieldDefinition dfield, FieldDefinition field) { if (field.IsPrivate) { if (field.DeclaringType.HasNestedTypes) { dfield.IsProtected = true; } else { dfield.IsPrivate = true; } } else if (field.IsFamily) { dfield.IsProtected = true; } else { dfield.IsPublic = true; } if (field.IsInitOnly) { dfield.IsFinal = true; } if (field.IsStatic) { dfield.IsStatic = true; } }
/// <summary> /// Set the value of the given dex field. /// </summary> protected virtual void SetFieldValue(DexLib.FieldDefinition dfield, FieldDefinition field) { var constant = field.Constant; if (constant != null) { var fieldType = field.FieldType; if (fieldType.IsByte()) { constant = XConvert.ToByte(constant); } else if (fieldType.IsUInt16()) { constant = XConvert.ToShort(constant); } else if (fieldType.IsUInt32()) { constant = XConvert.ToInt(constant); } else if (fieldType.IsUInt64()) { constant = XConvert.ToLong(constant); } } dfield.Value = constant; }
IField GetField(object token) { if (token is Mono.Cecil.FieldDefinition) { Mono.Cecil.FieldDefinition field = token as Mono.Cecil.FieldDefinition; var type = GetType(field.DeclaringType.FullName, field.Module); return(type.GetField(field.Name)); } else if (token is Mono.Cecil.FieldReference) { Mono.Cecil.FieldReference field = token as Mono.Cecil.FieldReference; var type = GetType(field.DeclaringType.FullName, field.Module); return(type.GetField(field.Name)); } //else if(token is CLRSharp_Instance) // { //CLRSharp_Instance inst = token as CLRSharp_Instance; //return inst.Fields[field.Name]; // } else { throw new NotImplementedException("不可处理的token" + token.GetType().ToString()); } }
public bool ProcessFieldDefinition(Mono.Cecil.FieldDefinition fieldToProcess) { if (fieldToProcess.FieldType.FullName == "Machine.Specifications.It") { return(true); } return(false); }
public FieldInfo(IReflectionInfoFactory reflectionInfoFactory, Mono.Cecil.FieldDefinition member) : base(reflectionInfoFactory, member) { _type = reflectionInfoFactory.GetReference(member.FieldType); _isPublic = member.IsPublic; _isStatic = member.IsStatic; _isReadOnly = member.IsInitOnly; _isConstant = member.IsLiteral; _isOnApiSurface = member.IsPublic || member.IsFamily || member.IsFamilyOrAssembly; _constant = member.Constant; }
public TypeInformation(Mono.Cecil.TypeReference parentType, Mono.Cecil.FieldDefinition fieldInfo, TypeAttribute inheritedAttribute, string parent = null) { FieldInfo = fieldInfo; Type = fieldInfo.FieldType; Fields = new List <TypeInformation>(); Attribute = inheritedAttribute; ParseAttribute(CecilExtensions.GetGhostFieldAttribute(parentType, fieldInfo)); Parent = string.IsNullOrEmpty(parent) ? "" : parent; }
/// <summary> /// Initializes a new instance of the <see cref="FieldDocumentation"/> class. /// </summary> /// <param name="fieldDefinition">The <see cref="FieldDefinition"/> which to document.</param> /// <param name="xElement">The XML element representing the XML comments for the current member.</param> /// <param name="handle">The <see cref="EntityHandle"/> that represents the member to document.</param> /// <param name="declaringType">The type which declares this member.</param> public FieldDocumentation(FieldDefinition fieldDefinition, XElement xElement, EntityHandle handle, TypeDocumentation declaringType) : base(fieldDefinition, xElement, declaringType) { var declaringAssembly = declaringType.DeclaringAssembly; this.Declaration = declaringAssembly.Decompiler.DecompileAsString(handle).Trim(); if (this.Declaration.Contains("=")) { this.Declaration = $"{this.Declaration.Substring(0, this.Declaration.IndexOf('=')).Trim()};"; } }
private static string MapField(Mono.Cecil.FieldDefinition field) { var fieldName = field.Name.Split(">")[0].Split("<")[1]; const string list = "System.Collections.Generic.List"; string typeName = null; if (field.FieldType.FullName.IndexOf(list, StringComparison.Ordinal) >= 0) { var splitString = field.FieldType.FullName.Split("<")[1].Split(">")[0].Split("."); typeName = $"{splitString[^1]}[]";
/// <summary> /// Default ctor /// </summary> public static FieldBuilder Create(AssemblyCompiler compiler, FieldDefinition field) { if (field.IsAndroidExtension()) return new DexImportFieldBuilder(compiler, field); if (field.DeclaringType.IsEnum) { if (!field.IsStatic) throw new ArgumentException("value field should not be implemented this way"); return new EnumFieldBuilder(compiler, field); } return new FieldBuilder(compiler, field); }
static public bool HasAttributeOnMemberOImplementedInterfaces <T>(this Mono.Cecil.FieldDefinition member) where T : Attribute { foreach (var m in member.DeclaringType.Interfaces) { var prop = m.InterfaceType.Resolve().Fields.FirstOrDefault(p => p.Name == member.Name); if (prop != null && prop.GetAttribute <T>() != null) { return(true); } } return(false); }
/// <summary> /// Weave field into current aspect /// </summary> /// <param name="il">IL Generator</param> /// <param name="field">Field</param> /// <param name="fieldMethod">Field Method</param> private void BlockField(Mono.Cecil.Cil.ILProcessor il, Mono.Cecil.FieldDefinition field, MethodInfo fieldMethod) { var fieldName = field.Name; // Return if it is a backing field if (fieldName.IndexOf("k__BackingField") >= 0) { return; } var aspect = field.GetCustomAttribute <MemberInterceptionAspect>() ?? field.DeclaringType.GetCustomAttribute <MemberInterceptionAspect>() ?? field.DeclaringType.Module.Assembly.GetCustomAttribute <MemberInterceptionAspect>(); if (!ILWeaver.IsValidAspectFor(field, aspect)) { return; } var fieldType = field.FieldType; var isStatic = field.IsStatic; var fieldLocal = il.DeclareLocal(fieldType); var memberLocal = il.DeclareLocal(typeof(MemberContext)); var aspectField = ILWeaver.TypeFieldAspects[field.DeclaringType.FullName][aspect.GetType().FullName]; // Store current get field value il.Emit(Mono.Cecil.Cil.OpCodes.Stloc, fieldLocal); // MemberContext(object instance, string locationName, object value) il.Emit(isStatic ? Mono.Cecil.Cil.OpCodes.Ldnull : Mono.Cecil.Cil.OpCodes.Ldarg_0); il.Emit(Mono.Cecil.Cil.OpCodes.Ldstr, fieldName); il.Emit(Mono.Cecil.Cil.OpCodes.Ldloc, fieldLocal); if (fieldType.IsValueType) { il.Emit(Mono.Cecil.Cil.OpCodes.Box, fieldType); } il.Emit(Mono.Cecil.Cil.OpCodes.Newobj, MemberContextCtor); il.Emit(Mono.Cecil.Cil.OpCodes.Stloc, memberLocal); il.Emit(Mono.Cecil.Cil.OpCodes.Ldsfld, aspectField); il.Emit(Mono.Cecil.Cil.OpCodes.Ldloc, memberLocal); il.Emit(Mono.Cecil.Cil.OpCodes.Callvirt, fieldMethod); // Load value back to stack and reflect changes if any il.Emit(Mono.Cecil.Cil.OpCodes.Ldloc, memberLocal); il.Emit(Mono.Cecil.Cil.OpCodes.Callvirt, MemberContextValueMethod); // Convert to expected type il.Emit(fieldType.IsValueType ? Mono.Cecil.Cil.OpCodes.Unbox_Any : Mono.Cecil.Cil.OpCodes.Isinst, fieldType); }
public ILField(ILType type, Mono.Cecil.FieldDefinition field) { this.type = field.FieldType.FullName; this.name = field.Name; this.displayName = this.name; this.field = field; foreach (var ev in field.DeclaringType.Events) { if (ev.Name == field.Name && ev.EventType.FullName == field.FieldType.FullName) { this.isEvent = true; Mono.Collections.Generic.Collection <Mono.Cecil.CustomAttribute> ca = ev.CustomAttributes; foreach (var attr in ca) { if (attr.AttributeType.Name == "DisplayNameAttribute") { this.displayName = (string)attr.ConstructorArguments[0].Value; } } var eventtype = field.FieldType as Mono.Cecil.TypeDefinition; if (eventtype == null) { try { eventtype = field.FieldType.Resolve(); } catch { throw new Exception("can't parese event type from:" + field.FieldType.FullName + ".maybe it is System.Action<xxx> which is defined in mscorlib.dll,copy this dll in."); } } if (eventtype != null) { foreach (var m in eventtype.Methods) { if (m.Name == "Invoke") { this.returntype = m.ReturnType.FullName; foreach (var src in m.Parameters) { this.paramtypes.Add(new NeoParam(src.Name, src.ParameterType.FullName)); } } } } break; } } }
/// <summary> /// Set the access flags of the created field. /// </summary> protected virtual void SetAccessFlags(DexLib.FieldDefinition dfield, FieldDefinition field) { if (field.IsPrivate) { if (field.DeclaringType.HasNestedTypes) dfield.IsProtected = true; else dfield.IsPrivate = true; } else if (field.IsFamily) dfield.IsProtected = true; else dfield.IsPublic = true; if (field.IsInitOnly) dfield.IsFinal = true; if (field.IsStatic) dfield.IsStatic = true; }
public override void VisitField(Mono.Cecil.FieldDefinition field) { base.VisitField(field); if (field.HasConstant) { string stringValue = field.Constant as string; if (stringValue != null && IsMatch(stringValue)) { MatchContext context = new MatchContext("String Match", mySearchString); context["String"] = stringValue; base.Aggregator.AddMatch(field, context); } } }
/// <summary> /// Default ctor /// </summary> public static FieldBuilder Create(AssemblyCompiler compiler, FieldDefinition field) { if (field.IsAndroidExtension()) { return(new DexImportFieldBuilder(compiler, field)); } if (field.DeclaringType.IsEnum) { if (!field.IsStatic) { throw new ArgumentException("value field should not be implemented this way"); } return(new EnumFieldBuilder(compiler, field)); } return(new FieldBuilder(compiler, field)); }
TypeInformation ParseTypeField(Mono.Cecil.TypeReference typeDefinition, Mono.Cecil.FieldDefinition fieldInfo, TypeAttribute inheritedAttribute, string parent = "") { var information = new TypeInformation(typeDefinition, fieldInfo, inheritedAttribute, parent); //blittable also contains bool, but does not contains enums if (fieldInfo.FieldType.IsBlittable()) { return(information); } var fieldTypeDef = fieldInfo.FieldType.Resolve(); if (fieldTypeDef.IsEnum) { return(information); } if (!fieldTypeDef.IsStruct()) { return(default);
/// <summary> /// Aspect code to inject at the beginning of weaved method /// </summary> /// <param name="typeBuilder">Type Builder</param> /// <param name="method">Method</param> /// <param name="parameter">Parameter</param> /// <param name="il">ILGenerator</param> internal void BeginBlock(Mono.Cecil.TypeDefinition typeBuilder, Mono.Cecil.MethodDefinition method, Mono.Cecil.ParameterDefinition parameter, Mono.Cecil.Cil.ILProcessor il) { field = method.DeclaringType.DefineField("<unsafe>_" + method.Name, typeof(int), Mono.Cecil.FieldAttributes.Static | Mono.Cecil.FieldAttributes.Private); var notZero = il.DefineLabel(); il.Emit(Mono.Cecil.Cil.OpCodes.Ldsfld, field); il.Emit(Mono.Cecil.Cil.OpCodes.Brfalse, notZero); il.Emit(Mono.Cecil.Cil.OpCodes.Newobj, typeof(ConcurrentAccessException).GetConstructor(Type.EmptyTypes)); il.Emit(Mono.Cecil.Cil.OpCodes.Throw); il.MarkLabel(notZero); il.Emit(Mono.Cecil.Cil.OpCodes.Ldsfld, field); il.Emit(Mono.Cecil.Cil.OpCodes.Ldc_I4_1); il.Emit(Mono.Cecil.Cil.OpCodes.Add); il.Emit(Mono.Cecil.Cil.OpCodes.Stsfld, field); }
public static GhostFieldAttribute GetGhostFieldAttribute(Mono.Cecil.TypeReference parentType, Mono.Cecil.FieldDefinition componentField) { var attribute = componentField.GetAttribute <GhostFieldAttribute>(); if (attribute != null) { var fieldAttribute = new GhostFieldAttribute(); if (attribute.HasProperties) { foreach (var a in attribute.Properties) { typeof(GhostFieldAttribute).GetProperty(a.Name)?.SetValue(fieldAttribute, a.Argument.Value); } } return(fieldAttribute); } return(default(GhostFieldAttribute)); }
public ILField(ILType type, Mono.Cecil.FieldDefinition field) { this.type = field.FieldType.FullName; this.name = field.Name; this.displayName = this.name; this.field = field; foreach (var ev in field.DeclaringType.Events) { if (ev.Name == field.Name && ev.EventType.FullName == field.FieldType.FullName) { this.isEvent = true; Mono.Collections.Generic.Collection <Mono.Cecil.CustomAttribute> ca = ev.CustomAttributes; foreach (var attr in ca) { if (attr.AttributeType.Name == "DisplayNameAttribute") { this.displayName = (string)attr.ConstructorArguments[0].Value; } } var eventtype = field.FieldType as Mono.Cecil.TypeDefinition; if (eventtype == null) { eventtype = field.FieldType.Resolve(); } foreach (var m in eventtype.Methods) { if (m.Name == "Invoke") { this.returntype = m.ReturnType.FullName; foreach (var src in m.Parameters) { this.paramtypes.Add(new AntsParam(src.Name, src.ParameterType.FullName)); } } } break; } } }
/// <summary> /// Aspect code to inject at the beginning of weaved method /// </summary> /// <param name="typeBuilder">Type Builder</param> /// <param name="method">Method</param> /// <param name="parameter">Parameter</param> /// <param name="il">ILGenerator</param> internal void BeginBlock(Mono.Cecil.TypeDefinition typeBuilder, Mono.Cecil.MethodDefinition method, Mono.Cecil.ParameterDefinition parameter, Mono.Cecil.Cil.ILProcessor il) { var meth = method; var module = typeBuilder.Module; var returnType = meth.ReturnType.ReflectionType(); if (returnType == typeof(void) || !(meth.Name.StartsWith("get_") || meth.Parameters.Count == 0)) { return; } var isStatic = meth.IsStatic; var isPrimitive = returnType.IsPrimitive(); if (isPrimitive) { returnType = typeof(Nullable <>).MakeGenericType(returnType); } autoField = method.DeclaringType.DefineField("<auto_lazy>_" + meth.Name, returnType, isStatic ? Mono.Cecil.FieldAttributes.Static | Mono.Cecil.FieldAttributes.Private : Mono.Cecil.FieldAttributes.Private); autoLabel = il.DefineLabel(); if (!isStatic) { il.Emit(Mono.Cecil.Cil.OpCodes.Ldarg_0); } il.Emit(isStatic ? Mono.Cecil.Cil.OpCodes.Ldsflda : Mono.Cecil.Cil.OpCodes.Ldflda, autoField); if (isPrimitive) { il.Emit(Mono.Cecil.Cil.OpCodes.Call, module.Import(returnType.GetMethod("get_HasValue"))); } il.Emit(isPrimitive ? Mono.Cecil.Cil.OpCodes.Brtrue : Mono.Cecil.Cil.OpCodes.Brfalse, autoLabel); }
/// <summary> /// Weave field into current aspect /// </summary> /// <param name="il">IL Generator</param> /// <param name="field">Field</param> private void BlockGetField(Mono.Cecil.Cil.ILProcessor il, Mono.Cecil.FieldDefinition field) { BlockField(il, field, GetValueMethod); }
protected abstract IEnumerable<TField> GetFields (TType type); // Must return all instance fields. May return static fields (they are filtered out automatically). protected abstract TType GetFieldType (TField field);
public static bool IsGhostField(this Mono.Cecil.FieldDefinition fieldDefinition) { return(fieldDefinition.HasAttribute <GhostFieldAttribute>()); }
/// <summary> /// Default ctor /// </summary> public DexImportFieldBuilder(AssemblyCompiler compiler, FieldDefinition field) : base(compiler, field) { }
protected abstract string GetFieldName (TField field);
/// <summary> /// Set the field type of the given dex field. /// </summary> protected override void SetFieldType(DexLib.FieldDefinition dfield, FieldDefinition field, DexTargetPackage targetPackage) { dfield.Type = dfield.Owner; }
public FieldInterlockedBuilder(AssemblyCompiler compiler, FieldDefinition field, FieldBuilder fieldBuilder) : base(compiler, field) { _baseFieldBuilder = fieldBuilder; }
/// <summary> /// Weave event invoke /// </summary> /// <param name="il">IL Generator</param> /// <param name="method">Method</param> /// <param name="field">Field</param> private void BlockInvokeEvent(Mono.Cecil.Cil.ILProcessor il, Mono.Cecil.MethodDefinition method, Mono.Cecil.FieldDefinition field) { var isStatic = method.IsStatic; var eventContext = il.DeclareLocal(typeof(EventContext)); var locals = new List <Mono.Cecil.Cil.VariableDefinition>(); var objLocal = il.DeclareLocal(typeof(object[])); var parameters = method.Parameters; var parameterLength = parameters.Count; var fieldDeclaringType = field.DeclaringType; var aspect = field.GetCustomAttribute <EventInterceptionAspect>() ?? fieldDeclaringType.GetCustomAttribute <EventInterceptionAspect>() ?? fieldDeclaringType.Module.Assembly.GetCustomAttribute <EventInterceptionAspect>(); if (!ILWeaver.IsValidAspectFor(field, aspect, allowEvents: true)) { return; } var aspectField = ILWeaver.TypeFieldAspects[fieldDeclaringType.FullName][aspect.GetType().FullName]; il.Emit(Mono.Cecil.Cil.OpCodes.Ldc_I4, parameterLength); il.Emit(Mono.Cecil.Cil.OpCodes.Newarr, typeof(object)); il.Emit(Mono.Cecil.Cil.OpCodes.Stloc, objLocal); for (var i = 0; i < parameterLength; i++) { var parameter = parameters[i]; var parameterType = parameter.ParameterType; var local = il.DeclareLocal(parameterType); il.Emit(Mono.Cecil.Cil.OpCodes.Stloc, local); il.Emit(Mono.Cecil.Cil.OpCodes.Ldloc, objLocal); il.Emit(Mono.Cecil.Cil.OpCodes.Ldc_I4, parameterLength - i - 1); il.Emit(Mono.Cecil.Cil.OpCodes.Ldloc, local); if (parameterType.IsValueType) { il.Emit(Mono.Cecil.Cil.OpCodes.Box, parameterType); } il.Emit(Mono.Cecil.Cil.OpCodes.Stelem_Ref); locals.Add(local); } if (!isStatic) { il.Emit(Mono.Cecil.Cil.OpCodes.Ldarg_0); } il.Emit(Mono.Cecil.Cil.OpCodes.Ldnull); il.Emit(Mono.Cecil.Cil.OpCodes.Ldc_I4, 1); il.Emit(Mono.Cecil.Cil.OpCodes.Ldloc, objLocal); il.Emit(Mono.Cecil.Cil.OpCodes.Newobj, EventContextCtor); il.Emit(Mono.Cecil.Cil.OpCodes.Stloc, eventContext); //InvokeEventMethod il.Emit(Mono.Cecil.Cil.OpCodes.Ldsfld, aspectField); il.Emit(Mono.Cecil.Cil.OpCodes.Ldloc, eventContext); il.Emit(Mono.Cecil.Cil.OpCodes.Callvirt, InvokeEventMethod); //Restore original invoke event parameters //locals.Reverse(); foreach (var local in locals) { il.Emit(Mono.Cecil.Cil.OpCodes.Ldloc, local); } }
/// <summary> /// Default ctor /// </summary> protected FieldBuilder(AssemblyCompiler compiler, FieldDefinition field) { this.compiler = compiler; this.field = field; }
public static GhostFieldAttribute GetGhostFieldAttribute(Mono.Cecil.TypeReference parentType, Mono.Cecil.FieldDefinition componentField) { if (GhostAuthoringComponentEditor.GhostDefaultOverrides.TryGetValue(parentType.FullName.Replace('/', '+'), out var newComponent)) { foreach (var field in newComponent.fields) { if (field.name == componentField.Name) { return(field.attribute); } } return(default(GhostFieldAttribute)); } var attribute = componentField.GetAttribute <GhostFieldAttribute>(); if (attribute != null) { var fieldAttribute = new GhostFieldAttribute(); if (attribute.HasProperties) { foreach (var a in attribute.Properties) { typeof(GhostFieldAttribute).GetProperty(a.Name)?.SetValue(fieldAttribute, a.Argument.Value); } } return(fieldAttribute); } return(default(GhostFieldAttribute)); }
public LoadFieldValue(FieldDefinition fieldToLoad, DynamicallyAccessedMemberTypes dynamicallyAccessedMemberTypes) { Kind = ValueNodeKind.LoadField; Field = fieldToLoad; DynamicallyAccessedMemberTypes = dynamicallyAccessedMemberTypes; }
/// <summary> /// Set the value of the given dex field. /// </summary> protected override void SetFieldValue(DexLib.FieldDefinition dfield, FieldDefinition field) { // Do nothing }
protected abstract bool IsStatic (TField field);
/// <summary> /// Set the field type of the given dex field. /// </summary> protected virtual void SetFieldType(DexLib.FieldDefinition dfield, FieldDefinition field, DexTargetPackage targetPackage) { dfield.Type = field.FieldType.GetReference(targetPackage, compiler.Module); }
public Field_Common_CLRSharp(Type_Common_CLRSharp type, Mono.Cecil.FieldDefinition field) { this.field = field; this.FieldType = type.env.GetType(field.FieldType.FullName); this._DeclaringType = type; }
/// <summary> /// Set the access flags of the created field. /// </summary> protected override void SetAccessFlags(DexLib.FieldDefinition dfield, FieldDefinition field) { base.SetAccessFlags(dfield, field); dfield.IsFinal = true; }
/// <summary> /// Default ctor /// </summary> internal EnumFieldBuilder(AssemblyCompiler compiler, FieldDefinition field) : base(compiler, field) { }