I have written the following method:
async Task<T> Load<T>(Func<T> function)
{
T result = await Task.Factory.StartNew(() =>
{
IsLoading = true;
T functionResult = function.Invoke();
IsLoading = false;
return functionResult;
});
return result;
}
I have two questions:
Can I simplify the code?
I can pass any parameterless method/function to this method to get a return type of any type. E.g.:
string GetString()By saying:
string someString = await Load(GetString);Is there a way I could make this method more generic so that I could pass methods with parameters as well? E.g. One single method that would also accept:
string GetString(string someString) string GetString(string someString, int someInt)This might look like:
string someString = await Load(GetString("string")); string someString = await Load(GetString("string", 1));This obviously doesn't work, but as the
Load<T>method doesn't reference the parameters, I feel this should be possible somehow.