今天我面临以下问题:获取某些属性的特定属性及其值。
假设这段代码:
模型:
public class ExampleModel : SBase
{
[MaxLength(128)]
public string ... { get; set; }
[ForeignKey(typeof(Foo))] // Here I wanna get the "typeof(Foo)" which I believe it is the value of the attr
public int LocalBarId { get; set; }
[ForeignKey(typeof(Bar))]
public int LocalFooId { get; set; }
[ManyToOne("...")]
public ... { get; set; }
}
然后在另一个类中,我想获取所有“ForeignKey”属性及其值,以及更多,它们各自的属性,但我不知道如何在实践中做到这一点。(最后,最好将所有这些信息放入任何数组中。)
我最近在写一篇反思。这样做的想法是只获得特定的属性。这是一段代码:
foreach (var property in this.allProperties)
{
var propertyItself = element.GetType().GetProperty(property.Name);
if (propertyItself.PropertyType != typeof(Int32))
{ continue; }
if (propertyItself.ToString().Contains("Global") && (int)propertyItself.GetValue(element, null) == 0)
{ // doSomething; }
else if (propertyItself.ToString().Contains("Local") && (int)propertyItself.GetValue(element, null) == 0)
{ // doSomething; }
}
所以基本上我只对获取 int 类型的属性感兴趣,如果该属性是我所期望的,那么我会处理它们。
好吧,我希望通过这次谈话,任何人或任何人都可以帮助我,或者,只给出一个基本的想法,告诉你如何做到这一点。提前致谢!:)