C # interface with static property or methods?

I need to define a static property or method in specific classes of my business logic to find out which classes are cached in an ASP.NET service session or cache. I think a static property or method in an interface would be ideal, but C # 4.0 does not support this.

All that is needed is to evaluate in the universal manager which classes are cacheable, and if they exist, at what level: session (user) or cache (application).

Now I am trying with an empty interface with a parameter T to evaluate, but maybe there is a better approach? Thanks.

public interface ICacheable<T> { } public class Country : ICacheable<CacheApplication> { } public class Department : ICacheable<CacheUser> { } public class Gestor<T> { // ... if (typeof(T) is ICacheable<CacheApplication>) { } // ... } 
+4
source share
2 answers

How about using a custom attribute? Then your classes will look something like this:

 [Cacheable(Level = CacheLevels.Application)] public class Country { } [Cacheable(Level = CacheLevels.User)] public class Department { } 

You can read here about how to create your own custom attribute, and then access its value using reflection.

+12
source

You cannot define static interfaces, on the one hand, you cannot create instances of static classes, so you cannot replace them for others with the same base class.

You might be better off having one instance of one class of one class and using interfaces as usual. You can force an instance of one and one only with the factory template.

+2
source

Source: https://habr.com/ru/post/1395851/


All Articles