给定以下类(class)
[KeyField("dap_name")]
public class nhs_acquisition_profile
: DALObject
, IDisposable
{
public nhs_acquisition_profile()
: base()
{
}
public nhs_acquisition_profile(String ConnectionString)
: base(ConnectionString)
{
}
}
如何从基类中找到 KeyField 属性的值。
最佳答案
我想你在构建阶段需要它
public DALObject() // base constructor
{
var fieldAttr = GetType() // real type
.GetCustomAttributes(typeof(KeyFieldAttribute), true) // look for attribute
.FirstOrDefault(); // can take more than one, it's an example
var resultField = (fieldAttr as KeyFieldAttribute)?.Field; // cast and use
}
相同的代码在类中的其他函数中的工作方式相同
关于C# 给定一个自定义类和自定义属性,如何从基类中找到类的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46409526/