/// <summary> /// Create Type from resource. /// </summary> /// <typeparam name="T">type to create.</typeparam> /// <param name="resource">resource to bind to.</param> /// <param name="cancellationToken">the <see cref="CancellationToken"/> for the task.</param> /// <returns>task which will resolve to created type.</returns> #pragma warning disable CA1801 // Review unused parameters (we can't remove cancellationToken without breaking binary compat) public async Task <T> LoadTypeAsync <T>(Resource resource, CancellationToken cancellationToken = default) #pragma warning restore CA1801 // Review unused parameters { RegisterComponentTypes(); string id = resource.Id; try { var sourceContext = new ResourceSourceContext(); var(jToken, range) = await ReadTokenRangeAsync(resource, sourceContext).ConfigureAwait(false); using (new SourceScope(sourceContext, range)) { var result = Load <T>(jToken, sourceContext); return(result); } } catch (InvalidOperationException) { throw; } catch (Exception err) { if (err.InnerException is SyntaxErrorException) { throw new SyntaxErrorException(err.InnerException.Message, err) { Source = $"{id}{err.InnerException.Source}" }; } throw new Exception($"{id} error: {err.Message}\n{err.InnerException?.Message}", err); } }
public async Task ResourceExplorer_ReadTokenRangeAdvance_AssignId() { var path = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, PathUtils.NormalizePath(@"..\..\.."))); var sourceContext = new ResourceSourceContext(); const string resourcesFolder = "resources"; const string resourceId = "test.dialog"; using (var explorer = new ResourceExplorer()) { explorer.AddResourceProvider(new FolderResourceProvider(explorer, path)); // Load file using resource explorer var resource = explorer.GetResource(resourceId); // Read token range using resource explorer var(jToken, range) = await explorer.ReadTokenRangeAsync(resource, sourceContext, true).ConfigureAwait(false); // Verify correct range var expectedRange = new SourceRange { StartPoint = new SourcePoint(1, 1), EndPoint = new SourcePoint(14, 1), Path = Path.Join(Path.Join(path, resourcesFolder), resourceId) }; Assert.Equal(expectedRange, range); // Verify ID was added Assert.Equal(resourceId, sourceContext.DefaultIdMap[jToken]); } }