/// <summary> /// Constructs a new memory binding. /// </summary> /// <param name="import">The memory import of the binding.</param> /// <param name="field">The field the import is bound to.</param> public MemoryBinding(MemoryImport import, FieldInfo field) { if (import is null) { throw new ArgumentNullException(nameof(import)); } if (field is null) { throw new ArgumentNullException(nameof(field)); } Import = import; Field = field; Validate(); }
private static MemoryBinding BindMemory(MemoryImport import, IEnumerable <FieldInfo> fields) { var field = fields?.Where(f => { var attribute = (ImportAttribute)f.GetCustomAttribute(typeof(ImportAttribute)); return(attribute.Name == import.Name && ((string.IsNullOrEmpty(attribute.Module) && string.IsNullOrEmpty(import.ModuleName)) || attribute.Module == import.ModuleName)); } ).FirstOrDefault(); if (field is null) { throw new WasmtimeException($"Failed to bind memory import '{import}': the host does not contain a memory field with a matching 'Import' attribute."); } return(new MemoryBinding(import, field)); }