Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6016179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:56:44+00:00 2026-05-23T02:56:44+00:00

I am trying the CacheAttribute feature of PostSharp . Following is the method I

  • 0

I am trying the CacheAttribute feature of PostSharp.

Following is the method I am using to build cache keys for each cache entry.

    private string BuildCacheKey(Arguments arguments)
    {
        var sb = new StringBuilder();
        sb.Append(_methodName);
        foreach (var argument in arguments.ToArray())
        {
            sb.Append(argument == null ? "_" : argument.ToString());
        }
        return sb.ToString();
    }

Below is a sample class that I use.

class State
{
    public string Code { get; set; }
    public string Name { get; set; }
}

And the method that needs to be cached is:

    [Cache]
    private static IDictionary<string, string> GetStateRegions(IEnumerable<State> states)
    {
        //some db call here to retrieve values;
    }

And this is how I call the method:

        IList<State> states = new List<State>();

        states.Add( new State {Code = "NM", Name = "New Mexico"});
        states.Add(new State {Code = "CA", Name = "California"});  

        GetStateRegions(states);

The BuildCacheKey method builds the following cache key – “GetStateRegionsSystem.Collections.Generic.List`1[ConsoleApplication1.State]”

I would like to have the cache key built something like “GetStateRegions[ConsoleApplication1.State]” – for example for the above call- “GetStateRegionsNMCA[ConsoleApplication1.State]” for state codes NM and CA.

What would be a good approach to achieve this? Also is it possible to use different logic to build keys for different methods (based on method arguments type)? Would greatly appreciate any pointers/suggestions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:56:45+00:00Added an answer on May 23, 2026 at 2:56 am

    The issue you’ll have is that you wont know what the arguments are unless of course you’re only using this on a single method. You can use MethodInfo.Name in a switch statement if you want to change your key stratgey. You might want to use a delegate and specify the key building method when you declare the aspect.

    You can try using

    if(Argument is List<State>) 
    {
     //gen key based ons tates
    } else
    {
    //gen key based on ToString()
    }
    

    You can always reflect into the argument to get the value instead of ToString()

    [serializable]
    public class MyAspect : OnMethodBoundaryAspect
    {
       public MyKeyBuilderDelegate KeyBuildMethod;
    
      ...
    }
    

    then declare the aspect like

    [MyAspect(KeyBuilderMethod = BuildByState)]
    public void MyMethod() { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make a MySQL-based application support MS SQL, I ran into the following
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
Trying to honor a feature request from our customers, I'd like that my application,
Trying to write a windows speech recognition macro. Written using XML and scripting language
Trying to read registry keys remotly (from a host on local intranet)... All the
Trying to connect to a SQL Server 2005 instance remotely but getting the following
Trying to insert values with Unicode Chars into a MySQL-database using Delphi 2010 and
Trying to use the http://www.dinnermint.org/css/creating-triangles-in-css/ method to create an arrow _________________ \ \ \
Trying to create an SSIS package to process a Datacube. Using SMS 2008, I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.