public string RegisterCachedFieldInfo(FieldInfo field) { var key = field.FieldHandle; CachedFieldInfo cachedField; if (!this._cachedTargetFields.TryGetValue(key, out cachedField)) { Contract.Assert(field.DeclaringType != null, "field.DeclaringType != null"); cachedField = new CachedFieldInfo( field, "_field" + field.DeclaringType.Name.Replace('`', '_') + "_" + field.Name + this._cachedTargetFields.Count.ToString(CultureInfo.InvariantCulture) ); this._cachedTargetFields.Add(key, cachedField); this._buildingType.Members.Add( new CodeMemberField( typeof(FieldInfo), cachedField.StorageFieldName ) ); } return(cachedField.StorageFieldName); }
/// <summary> /// Regisgters <see cref="FieldInfo"/> usage to the current emitting session. /// </summary> /// <param name="field">The <see cref="FieldInfo"/> to be registered.</param> /// <returns> /// <see cref=" Action{T1,T2}"/> to emit serializer retrieval instructions. /// The 1st argument should be <see cref="TracingILGenerator"/> to emit instructions. /// The 2nd argument should be argument index of the serializer holder, normally 0 (this pointer). /// This value will not be <c>null</c>. /// </returns> public Action <TracingILGenerator, int> RegisterFieldCache(FieldInfo field) { #if !NETSTANDARD1_1 && !NETSTANDARD1_3 var key = field.FieldHandle; #else var key = field; #endif // !NETSTANDARD1_1 && !NETSTANDARD1_3 CachedFieldInfo result; if (!this._cachedFieldInfos.TryGetValue(key, out result)) { Contract.Assert(field.DeclaringType != null, "field.DeclaringType != null"); result = new CachedFieldInfo( field, this.DefineInitonlyField( "_field" + field.DeclaringType.Name + "_" + field.Name + this._cachedFieldInfos.Count, typeof(FieldInfo) ) ); this._cachedFieldInfos.Add(key, result); } return ((il, thisIndex) => { il.EmitAnyLdarg(thisIndex); il.EmitLdfld(result.StorageFieldBuilder); }); }
public override Action <TracingILGenerator, int> RegisterField(FieldInfo field) { if (this._typeBuilder.IsCreated()) { throw new InvalidOperationException("Type is already built."); } var key = field.FieldHandle; CachedFieldInfo result; if (!this._cachedFieldInfos.TryGetValue(key, out result)) { Contract.Assert(field.DeclaringType != null, "field.DeclaringType != null"); result = new CachedFieldInfo( field, this._typeBuilder.DefineField( "_field" + field.DeclaringType.Name + "_" + field.Name + this._cachedFieldInfos.Count, typeof(FieldInfo), FieldAttributes.Private | FieldAttributes.InitOnly ) ); this._cachedFieldInfos.Add(key, result); } return ((il, thisIndex) => { il.EmitAnyLdarg(thisIndex); il.EmitLdfld(result.StorageFieldBuilder); }); }
private static bool FindFieldInfo(IntPtr typeClass, IntPtr unrealStruct, string fieldName, out CachedFieldInfo fieldInfo) { if (typeClass == IntPtr.Zero || unrealStruct == IntPtr.Zero || string.IsNullOrEmpty(fieldName)) { fieldInfo = default(CachedFieldInfo); return(false); } if (unrealStruct == lastUnrealStruct) { return(lastUnrealStructChildren.TryGetValue(fieldName, out fieldInfo)); } else if (unrealStruct == lastUnrealFunction) { return(lastUnrealFunctionChildren.TryGetValue(fieldName, out fieldInfo)); } else { Dictionary <string, CachedFieldInfo> fields = null; if (Native_UObjectBaseUtility.IsA(unrealStruct, Classes.UFunction)) { fields = lastUnrealFunctionChildren; lastUnrealFunction = unrealStruct; } else { fields = lastUnrealStructChildren; lastUnrealStruct = unrealStruct; } fields.Clear(); foreach (IntPtr field in new NativeReflection.NativeFieldIterator(EClassCastFlags.UFunction | EClassCastFlags.UProperty, unrealStruct, false)) { Native_UObjectBaseUtility.GetNameOut(field, ref nameUnsafe.Array); string name = nameUnsafe.Value; fields[name] = new CachedFieldInfo() { Address = field, Offset = Native_UProperty.GetOffset_ForInternal(field) }; } return(fields.TryGetValue(fieldName, out fieldInfo)); } }
public static CachedFieldInfo[] GetStrippedFields(Type type, bool caseInsensitive) { CachedFieldInfo[] fields; var cache = (caseInsensitive ? strippedInsensitiveFields : strippedFields); if(!cache.TryGetValue(type, out fields)) { var unstrippedFields = GetAllFields(type); var legalFields = new List<FieldInfo>(unstrippedFields.Length); foreach(var unstrippedField in unstrippedFields) { if(!unstrippedField.IsSpecialName) { legalFields.Add(unstrippedField); } } fields = new CachedFieldInfo[legalFields.Count]; for(int i = 0; i < fields.Length; i++) { fields[i] = new CachedFieldInfo { field = legalFields[i], strippedName = StripFieldname(legalFields[i].Name, caseInsensitive) }; } cache[type] = fields; } return fields; }
private static bool FindFieldInfo(IntPtr typeClass, IntPtr unrealStruct, string fieldName, out CachedFieldInfo fieldInfo) { if (typeClass == IntPtr.Zero || unrealStruct == IntPtr.Zero || string.IsNullOrEmpty(fieldName)) { fieldInfo = default(CachedFieldInfo); return(false); } if (unrealStruct == lastUnrealStruct) { return(lastUnrealStructChildren.TryGetValue(fieldName, out fieldInfo)); } else if (unrealStruct == lastUnrealFunction) { return(lastUnrealFunctionChildren.TryGetValue(fieldName, out fieldInfo)); } else { Dictionary <string, CachedFieldInfo> fields = null; // For structs we want to get all fields in the hierarchy as we can't struct inheritance in C# // (C++ structs can have inheritance, if we skip checking the hierarchy we wont get all the fields we want) bool isScriptStruct = false; if (Native_UObjectBaseUtility.IsA(unrealStruct, Classes.UFunction)) { fields = lastUnrealFunctionChildren; lastUnrealFunction = unrealStruct; } else { isScriptStruct = Native_UObjectBaseUtility.IsA(unrealStruct, Classes.UScriptStruct); fields = lastUnrealStructChildren; lastUnrealStruct = unrealStruct; } fields.Clear(); foreach (IntPtr field in new NativeReflection.NativeFieldIterator( EClassCastFlags.UFunction | EClassCastFlags.UProperty, unrealStruct, false, isScriptStruct)) { Native_UObjectBaseUtility.GetNameOut(field, ref nameUnsafe.Array); string name = nameUnsafe.Value; // Temporary check for debugging purposes. There shouldn't be any duplicate fields as we aren't looking in the hierarchy. if (System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debug.Assert(!fields.ContainsKey(name)); } fields[name] = new CachedFieldInfo() { Address = field, Offset = Native_UProperty.GetOffset_ForInternal(field) }; } return(fields.TryGetValue(fieldName, out fieldInfo)); } }