Using MethodBase, is it possible to get the parameters and their values ββof the called method?
To be specific, I am trying to use reflection to create cache keys. Since each method and its parameter list is unique, I thought it would be ideal to use this as a key. This is what I do:
public List<Company> GetCompanies(string city)
{
string key = GetCacheKey();
var companies = _cachingService.GetCacheItem(key);
if (null == company)
{
companies = _companyRepository.GetCompaniesByCity(city);
AddCacheItem(key, companies);
}
return (List<Company>)companies;
}
public List<Company> GetCompanies(string city, int size)
{
string key = GetCacheKey();
var companies = _cachingService.GetCacheItem(key);
if (null == company)
{
companies = _companyRepository.GetCompaniesByCityAndSize(city, size);
AddCacheItem(key, companies);
}
return (List<Company>)companies;
}
Where is GetCacheKey()defined (approximately) as:
public string GetCacheKey()
{
StackTrace stackTrace = new StackTrace();
MethodBase methodBase = stackTrace.GetFrame(1).GetMethod();
string name = methodBase.DeclaringType.FullName;
string parameterVals =
return name + parameterVals;
}
source
share