1

I am not sure if this possible, but if I have a default constructor and something is invoking it (method, another constructor, a property, etc) is it possible to determine what type called it while in that constructor?

I am specifically interested in this use to regards to applying an attribute to a property.

Update : This question was answered by this : When is a custom attribute's constructor run?

Example :


[AttributeUsage(AttributeTargets.Property)]
public class SomeAttribute : Attribute
{
    public Type InvokingType { get; private set; }

    public SomeAttribute()
    {
        //InvokingType = the property it was applied to
    }
}

public class SomeClass
{
    [SomeAttribute]
    public string SomeProperty { get; set; }
}
Community
  • 1
  • 1
Jim Stahl
  • 93
  • 1
  • 5
  • possible duplicate of [How can I find the method that called the current method?](http://stackoverflow.com/questions/171970/how-can-i-find-the-method-that-called-the-current-method) – Jeff Dunlop Jun 26 '14 at 02:04
  • This is a contrived example, but I do want to set some defaults for a property based on the invoking type and then optionally let it get set when using a constructor that takes parameters. – Jim Stahl Jun 26 '14 at 02:05
  • Your sample missing part on how that attribute will be used - attributes don't actively get constructed/invoked... so you need to show where/how (possibly in `set` of your property) you plan find and use that attribute. – Alexei Levenkov Jun 26 '14 at 02:11
  • possible duplicate of [How do I get the member to which my custom attribute was applied?](http://stackoverflow.com/questions/2168942/how-do-i-get-the-member-to-which-my-custom-attribute-was-applied) – EKrueger Jun 26 '14 at 07:19

0 Answers0