我有Stack<Query>
,其中Query
如下:
public class Query
{
private readonly IEnumerable<string> _distinguishedNames;
private readonly string _searchText;
private CancellationTokenSource _cancellationTokenSource;
public Query(
QueryType queryType,
Scope scope = null,
IEnumerable<string> distinguishedNames = null,
string searchText = null)
{
QueryType = queryType;
Scope = scope;
_distinguishedNames = distinguishedNames;
_searchText = searchText;
}
private CancellationToken CancellationToken
=> _cancellationTokenSource.Token;
public Scope Scope { get; }
public IEnumerable<ExpandoObject> Data { get; private set; }
public QueryType QueryType { get; }
public void Cancel()
{
_cancellationTokenSource?.Cancel();
}
public void DisposeData()
{
Data = null;
}
public async Task Execute()
{
_cancellationTokenSource = new CancellationTokenSource();
var task = Task.Run(() =>
{
Data = new Searcher(
QueryType,
Scope,
_distinguishedNames,
CancellationToken,
_searchText).GetData();
},
_cancellationTokenSource.Token);
await task;
}
public override string ToString()
{
return QueryType.ToString();
}
}
管理查询的类包含以下相关部分:
Stack<Query> Queries = new Stack<Query>();
private async void ExecuteRunPreviousQuery()
{
Queries.Pop();
await RunQuery(Queries.Pop());
}
private async Task RunQuery(
QueryType queryType,
Scope scope = null,
IEnumerable<string> selectedItemDistinguishedNames = null,
string searchText = null)
{
await RunQuery(
new Query(
queryType,
scope,
selectedItemDistinguishedNames,
searchText));
}
private async Task RunQuery(Query query)
{
StartTask();
try
{
Queries.Push(query);
await Queries.Peek().Execute();
Data = Queries.Peek().Data.ToDataTable().AsDataView();
ContextMenuItems = GenerateContextMenuItems();
}
catch (NullReferenceException e)
{
ShowMessage(e.Message);
ResetQuery();
}
catch (OperationCanceledException)
{
ShowMessage("Operation was cancelled.");
ResetQuery();
}
catch (ArgumentNullException)
{
ShowMessage(
"No results of desired type found in selected context.");
ResetQuery();
}
catch (OutOfMemoryException)
{
ShowMessage("The selected query is too large to run.");
ResetQuery();
}
FinishTask();
}
我可以正常运行任何查询,但如果我尝试运行上一个查询方法,则查询始终将其_distinguishedNames
字段设置为空,即使该字段以前有内容。不是空的,只是某种程度上是空的。传递Searcher
的{{1}}类不会操纵它;搜索者只是迭代它。我甚至尝试传递_distinguishedNames
的副本只是为了确保搜索者不会以某种方式搞乱它,并且这并没有改变任何东西。
为了澄清,我使用_distinguishedNames
参数和非空RunQuery
来调用QueryType
。它执行没有问题。然后,我再次使用其他一些参数调用IEnumerable<string>
,然后再次执行而没有问题。然后我调用RunQuery
并且第一个查询无法正常运行,因为应该存储在ExecuteRunPreviousQuery
中的IEnumerable<string>
现在为空。
如果您想查看其他任何代码,请告诉我们。据我所知,这是一个MCVE。