How do I set a Type TSomething at run time in C#?
I am writting a function that takes a parameter and that parameter requires a Type of TEntity. I want to be able to pass it a specific Type during runtime but I am having trouble getting it to compile:
public LoadOperation LoadQuery(EntityQuery<???> query)
{
LoadOperation loadOperation = DomainContext.Load(query,LoadBehavior.MergeIntoCurrent, false);
return loadOperation;
}
The code that wont compile looks like this:
EntityQuery<Person> q = DomainContext.GetPerson();
LoadQuery(q);
I have tried different things to m开发者_StackOverflow中文版ake this work but am at a loss. What do I need to do?
Depending on what your DomainContext.Load() function looks like:
public LoadOperation LoadQuery<T>(EntityQuery<T> query)
{
LoadOperation loadOperation = DomainContext.Load(query,LoadBehavior.MergeIntoCurrent, false);
return loadOperation;
}
And then still use it exactly like you did before:
EntityQuery<Person> q = DomainContext.GetPerson();
LoadQuery(q);
The type system should infer you mean the LoadQuery<Person>()
version of the function from the argument.
Unfortunately, I suspect this will also mean some revision to the aforementioned Load() function.
精彩评论