首页 \ 问答 \ 如何在属性中使用反射来检索有关调用它的类的信息(How to use reflection inside an attribute to retrieve information about the class that called it)

如何在属性中使用反射来检索有关调用它的类的信息(How to use reflection inside an attribute to retrieve information about the class that called it)

我遇到以下问题,

我得到了一个'Car'类,该函数使用EntityFramework访问数据库,并通过几个过滤器获取所有表格行。

像这样的东西:

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);
    }
}

我还在其上添加了一个属性。 在这个属性里我需要能够检索一些东西,

我需要获取类名(本例中是car),我还需要获取函数返回值的类型(在本例中为对象Vehicles),并且需要在VEHICLE_TYPE中获取值。

之后,我需要能够从属性内执行该功能('GetVehiclesFromDB')

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

      // todo
    }
}

我从来没有做过这样的事情,我不知道我应该采取什么样的方法。 这是一种反思吗? 或者它可能完全不同?

编辑:

我确实需要使用这个缓存,我想使用Redis。


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?

Edit :

I indeed need to use this for cache, i want to use Redis for that.


原文:https://stackoverflow.com/questions/42503569
更新时间:2023-03-01 18:03

最满意答案

我写了关于这个话题的硕士论文:

托马马丁。 “ 卷积神经网络结构的分析和优化 ”。 arXiv预印本arXiv:1707.09725(2017)。

长话短说:有几种分析技术(第2.5章)和学习topoligies的算法(第3章),但实际上它主要是试错法和直觉。


I wrote my masters thesis about the topic:

Thoma, Martin. "Analysis and Optimization of Convolutional Neural Network Architectures." arXiv preprint arXiv:1707.09725 (2017).

Long story short: There are a couple of techniques for analysis (chapter 2.5) and algorithms that learn topoligies (chapter 3), but in practice it is mostly trial and error / gut feeling.

相关问答

更多
  • 如果你使用冲浪功能,这意味着一个漂浮矢量[128]或[64]取决于你的冲浪配置你将设置神经网络如下 - 使用模型创建数据库: -bikes -cars -autobus -truck - 拍摄每种类型物体的照片,例如10张照片模型关闭汽车,10张照片模型关闭自行车10张照片,不同模型从卡车等等,到每张照片提取其每个物体的冲浪特征向量。 - 每种类型的对象都将代表神经网络中的一类对象; -car ;object class 1 =binary representaation in 4 bits= 0 0 ...
  • 这里是一个很好的例子: Brainnet 1 - 神经网络项目 - 带插图和代码 - 学习神经网络编程一步一步开发一个简单的手写检测系统 ,将演示神经网络编程的一些实际用途。 Here is good example: Brainnet 1 - A Neural Netwok Project - With Illustration And Code - Learn Neural Network Programming Step By Step And Develop a Simple Handwriting ...
  • 我可以看到你担心如何训练ANN,但这个项目隐藏着你可能不了解的复杂性 。 通过图像处理对计算机游戏进行对象/角色识别是一项非常具有挑战性的任务(对于FPS和RPG游戏来说,这并不奇怪 )。 我不怀疑你的技能,我也不是说不能做,但是您可以轻松地花费10倍的时间来识别内容,而不是实施ANN本身(假设您已经拥有数字图像处理技术的经验)。 我认为你的想法很有趣,也很有野心 。 在这一点上,您可能需要重新考虑它。 我觉得这个项目是你为大学打算的东西,所以如果工作的重点真的是ANN,你应该选择另一个游戏,更简单一些。 ...
  • 最明显的问题是您的训练数据集非常小 。 由于您使用的是scipy.optimize.minimize而不是通常的迭代梯度下降,我认为您的模型也可能过度拟合您的训练数据 。 在这里,迭代算法可能效果更好。 不要忘记仔细监控验证错误 。 如果您尝试使用梯度下降进行反向传播,请注意,根据反向传播使用的参数,神经网络需要一段时间才能收敛 您可以尝试多次向网络提供相同的训练数据或调整学习速率,但理想情况下您应该使用更多样化的数据。 Correctly normalizing the data solved the p ...
  • 我写了关于这个话题的硕士论文: 托马马丁。 “ 卷积神经网络结构的分析和优化 ”。 arXiv预印本arXiv:1707.09725(2017)。 长话短说:有几种分析技术(第2.5章)和学习topoligies的算法(第3章),但实际上它主要是试错法和直觉。 I wrote my masters thesis about the topic: Thoma, Martin. "Analysis and Optimization of Convolutional Neural Network Architec ...
  • 现在重复我的评论建议作为答案,以便更容易看到后来在此页面上结尾的其他人(因为我不是100%确定它是解决方案,因此首先发布为评论): 将奖励的幅度减小到(或至少接近)[0.0,1.0]或[-1.0,1.0]间隔有助于网络更快地收敛。 以这种方式改变奖励值(简单地将它们全部除以数字以使它们位于较小的间隔中)不会改变网络在理论上能够学习的内容。 通过在整个网络中寻找更大的权重,网络还可以简单地学习具有更大奖励的相同概念。 然而,学习如此大的重量通常需要更多的时间。 主要原因是权重通常初始化为接近0的随机值,因此通 ...
  • 你应该使用卷积神经网络。 我建议只从图像识别项目复制神经网络,并在数据集上重新训练它。 有效的卷积NN实现不应该使用那么多内存,尽管可能需要很长时间才能进行训练。 您还可以使用较低分辨率的图像,这通常是在机器视觉中完成的。 将所有图像缩小到256x256像素。 You should use convolutional neural networks. I'd suggest just copying a neural network from an image recognition project, an ...
  • 这个问题没有简单的答案。 神经网络的优点和缺点是一个非常复杂的话题。 这里有一些提示: 没有免费的午餐定理:粗略地说,这个定理证明没有“完美”的机器学习方法。 对于每种问题,某种方法都是好的,还有另外一个问题,同样的方法会导致可怕的结果。 然而,其失败的方法可以通过其他方法很容易地解决。 在进行任何机器学习时,应始终考虑这一点。 神经网络很容易实现(你不需要一个好的线性代数求解器,例如SVN)。 神经网络的VC维不清楚。 当你想考虑解决方案有多好时,这一点非常重要。 神经网络不能被重新训练。 如果稍后添加数 ...
  • 这方面的一个例子叫做神经进化。 您可以做的是将backprop与evolution结合起来,为您的数据集找到最佳结构。 Neataptic是提供神经进化的NN文库之一。 通过一些简单的编码,您可以将其转换为backprop + evolution。 这样做的缺点是它需要更多的计算能力,因为它需要遗传算法来运行整个群体。 因此,使用神经进化确实会使性能变得微弱。 但是,我认为有更多技术禁用某些节点,如果对输出没有负面影响,它们将被删除。 我不确定。 An example of this is called n ...
  • PyNeurGen是您的问题的可能解决方案。 它是一个支持多种网络架构的python库。 该库还包含一个前馈网络演示。 对于使用NARX net,您可以使用以下定义: NARX Net PyNeurGen PyNeurGen is a possible solution for your problem. Its a python library with support for several net architectures. The library also contains a demo for ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。