Beispiel #1
0
        // Gets or created a field definition for a type as needed.
        // This will first check if a RW one is available, if that is the case we should use that.
        // If not it will check to see if a RO one is available, use that and promote to RW if needed.
        // Finally, if we don't have one at all, let's create one with the appropriate access rights
        FieldDefinition GetOrCreateComponentDataFromEntityField(TypeReference type, bool asReadOnly)
        {
            if (ComponentDataFromEntityFields.TryGetValue(type.FullName, out var result))
            {
                if (result.HasReadOnlyAttribute() && !asReadOnly)
                {
                    result.RemoveReadOnlyAttribute();
                }

                return(result);
            }

            var fieldType     = TypeDefinition.Module.ImportReference(typeof(ComponentDataFromEntity <>)).MakeGenericInstanceType(type);
            var uniqueCounter = ComponentDataFromEntityFields.Count;

            result = new FieldDefinition($"_ComponentDataFromEntity_{type.Name}_{uniqueCounter}", FieldAttributes.Private, fieldType);
            TypeDefinition.Fields.Add(result);
            result.AddNoAliasAttribute();

            if (asReadOnly)
            {
                result.AddReadOnlyAttribute();
            }

            ComponentDataFromEntityFields[type.FullName] = result;
            return(result);
        }
        // Gets or created a field definition for a type as needed.
        // This will first check if a RW one is available, if that is the case we should use that.
        // If not it will check to see if a RO one is available, use that and promote to RW if needed.
        // Finally, if we don't have one at all, let's create one with the appropriate access rights
        FieldDefinition GetOrCreateDataFromEntityField(TypeReference type, bool asReadOnly, PatchableMethod.ComponentDataType patchableMethodDataType)
        {
            if (DataFromEntityFields.TryGetValue(type.FullName, out var result))
            {
                if (result.HasReadOnlyAttribute() && !asReadOnly)
                {
                    result.RemoveReadOnlyAttribute();
                }

                return(result);
            }

            var dataFromEntityType = patchableMethodDataType == PatchableMethod.ComponentDataType.ComponentDataFromEntity
                ? typeof(ComponentDataFromEntity <>) : typeof(BufferFromEntity <>);
            var fieldType     = TypeDefinition.Module.ImportReference(dataFromEntityType).MakeGenericInstanceType(type);
            var uniqueCounter = DataFromEntityFields.Count;

            result = new FieldDefinition($"_{patchableMethodDataType.ToString()}_{type.Name}_{uniqueCounter}", FieldAttributes.Private, fieldType);
            TypeDefinition.Fields.Add(result);
            result.AddNoAliasAttribute();

            if (asReadOnly)
            {
                result.AddReadOnlyAttribute();
            }

            DataFromEntityFields[type.FullName] = result;
            return(result);
        }
 public static void AddReadOnlyAttribute(this FieldDefinition fieldDefinition)
 {
     fieldDefinition.AddReadOnlyAttribute(fieldDefinition.Module);
 }