mardi 28 février 2017

How to use reflection inside an attribute to retrieve information about the class that called it

I got the following problem,

I got a class 'Car' with a function that uses EntityFramework to reach the Database and gets all the table rows by a few filters.

Something like this :

public class car
{
     private const string VEHICLE_TYPE = "something_here";
    [CachedAttribute(30)]
    public virtual List<Vehicles> GetVehiclesFromDB()
    {
        return _repository.Set<Vehicles>().Where(e => e.VehicleType == VEHICLE_TYPE && e.Weight < CAR_MAX_WEIGHT);
    }
}

I also added an attribute on top of it. Inside this attribute i need to be able to retrieve a few things,

I need to get the class name( in this case car), I also need to get the type of the returned value of the function ( in this case the object Vehicles) and i need to get the value inside VEHICLE_TYPE .

After that i need to be able to execute that function ('GetVehiclesFromDB') from inside the attribute

public class CachedAttribute : Attribute
{
    public CachedAttribute(int seconds)
    {

      // todo
    }
}

I never done those sort of things and i am not sure what is the approach i should take. Is it a case of reflection? or maybe it is something completely different?





Aucun commentaire:

Enregistrer un commentaire