I’ve implemented a simple extension method in my asp.net mvc 3 app to pull objects out of session using generics:
public static T GetVal<T>(this HttpSessionStateBase Session, string key, Func<T> getValues)
{
if (Session[key] == null)
Session[key] = getValues();
return (T)Session[key];
}
This works great if getValues() doesn’t require any arguments.
I was attempting to write an overload that takes in params object[] args to allow me to pass arguments if necessary to the getValues() function, but I don’t know what the syntax is to apply those variables to the function.
Is this even possible? Thanks in advance for your advice.
I would argue that you shouldn’t need to do this – the caller can handle that with a lambda expression. For example:
Here we’re capturing the idea of calling
IndexOfon"something"passing in the argument"o". All of that is captured in a simpleFunc<int>.