0

today I'm facing the following problem: Get the especific attribute and his value of some properties.

Suppose this code:

Model:

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

Then inside another class I want to get all the "ForeignKey" attribute and their values, and more, their respective properties too, but I have no I ideia how to do this in practice. (At end, would be nice throw all this information into any array.)

I recently was writing a Reflection. The ideia of this was to get only especifics properties. Here's one piece of the code:

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

So basically I was interested only in get properties of type int and if that property was which I was expecting then I'd work on em.

Well, I hope with this conversation, any or someone can help me on, or, only give a basic Idea of how you could do this. Thanks in Advance! :)

3
  • 2
    I think it's about time for FooBar examples to die. Commented Oct 22, 2013 at 19:43
  • Can you describe what is your problem? Are you asking how to get an attribute for a property? Commented Oct 22, 2013 at 19:46
  • @ISun, yes. That's what I am trying to do right now. :s Commented Oct 22, 2013 at 20:38

1 Answer 1

3
var properties = typeof(ExampleModel).GetProperties();
foreach (var property in properties)
{
    foreach (ForeignKeyAttribute foreignKey in
                       property.GetCustomAttributes(typeof(ForeignKeyAttribute)))
    {
        // you now have property's properties and foreignKey's properties
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hey @Tim S. I think that's gonna work now, first let me try it out and then I'll be back. Thanks! :D
This didn't work the first time i tried because my property is set to private, i believe this might be why this answer wasn't marked as accepted, for those who see this later use GetProperties along with the proper BindingFlags. Refer to this answer for more details. Thanks, +1.
@Azura Ah yeah, that's something to pay attention to; OP's question showed only public properties, so I didn't need to specify anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.