3

I can call GetCustomAttributesData on a MemberInfo, which is nice because I know which constructor was called to initialize the attribute and what named arguments were used.

If I put [return: ...] on a method to attribute the return value, I can't get to GetCustomAttributesData. The only thing I can use is ReturnTypeCustomAttributes, which gets me the attributes, not their data.

Is there any way to get the attribute data via reflection for a return value of a method? Note: using GetCustomAttributesData on the MethodInfo object doesn't return attributes for the return value.

5
  • 5
    can you show the code that you are using so that we can understand exactly what it is you are trying to do..? also according to MSDN the GetCustomAttributesData returns IList so you should be able to get something returned.. perhaps from there you will need to iterate through the List using a foreach loop Commented May 6, 2015 at 19:11
  • 2
    Note that it's the method that has the attribute - not the returned value itself. Commented May 6, 2015 at 19:12
  • Perhaps off topic, but why on earth would you want to do something like this? Are you doing you part to keep the global software pletora down? Commented May 6, 2015 at 19:16
  • @vidstige It seems like a reasonable thing to want to do. You want to find all methods that have the return value annotated with an attribute in a reflection only context. From a cursory glance at the reflection APIs I don't see how you get the CustomAttributeData objects for this scenario. Commented May 6, 2015 at 19:25
  • I'm writing a mocking library, and I want to add all attribute annotations to generic parameters, parameters and return values for a method I'm mocking. That's why I need to know. Commented May 6, 2015 at 20:31

1 Answer 1

3

I figured it out:

x.ReturnParameter.GetCustomAttributesData()
Sign up to request clarification or add additional context in comments.

Comments

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.