Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What is the best way to write this generic method?

43 views
Skip to first unread message

Robert Zurer

unread,
Jun 21, 2005, 7:06:43 AM6/21/05
to
This method works but FxCop rightly complains

"Generic methods should provide type parameter"

http://www.gotdotnet.com/team/fxcop/docs/rules.aspx?version=1.32
&url=/Design/GenericMethodsShouldProvideTypeParameter.html

public sealed class ReflectionAgent
{
public static T GetCustomAttribute<T>(MemberInfo memberInfo)
{
if (memberInfo == null) return default(T);
object[] attributes = memberInfo.GetCustomAttributes(typeof(T), true);
return ((attributes != null) && (attributes.Length == 1)) ?
(T)attributes[0] : default(T);
}
}

I want to call it like this

PropertyInfo propInfo = typeof(TestClass).GetProperty("FirstName");
ItemTypeAttribute attr =
ReflectionAgent.GetCustomAttribute<ItemTypeAttribute>(propInfo);

TIA

Robert Zurer

Jay B. Harlow [MVP - Outlook]

unread,
Jun 21, 2005, 4:18:05 PM6/21/05
to
Robert,
I came up with a similar routine for Assembly.GetCustomAttributes in VB the
other day. :-)

I didn't realize that it breaks a "broken" FxCop rule (I have not yet tried
FxCop on the project).

I say a "broken" FxCop rule, as unfortunately I think this type of generic
method is a valid exception to the rule you quote. As the type parameter
allows you to encapsulate the downcast & define the type that
GetCustomAttributes uses. Requiring a method parameter for inference feels
like overkill here.

Have you tried asking in one of the 2005 forums?

http://forums.microsoft.com/msdn/

I'll see what I can find out.

Hope this helps
Jay


"Robert Zurer" <robert...@zurer.com> wrote in message
news:MPG.1d21bece8...@news.microsoft.com...

Robert Zurer

unread,
Jun 22, 2005, 6:37:28 AM6/22/05
to
Thanks for your response. A good idea
I'll post this on the Visual C# Language Forum

Jay B. Harlow [MVP - Outlook]

unread,
Jun 22, 2005, 3:51:01 PM6/22/05
to
Robert,
I sent a note to AskFxCop alias at:

http://www.gotdotnet.com/team/fxcop/gotdotnetstyle.aspx?url=DocFrameset.htm

Asking about this "problem".

Thinking about it I have to wonder if the rule should check for parameters
or *return type* that matches the Type Parameter. As I see our routines to
be very handy method of encapsulating the type & downcast when working with
attributes.

FWIW: On my version of the function I used a class constraint of Attribute,
as only Attributes are allowed on the Assembly.GetCustomAttributes call.

Something like:

public static T GetCustomAttribute<T> (MemberInfo memberInfo) where T:
Attribute

This helps ensure that you don't attempt to call GetCustomAttribute for
other classes...

Hope this helps
Jay

"Robert Zurer" <robert...@zurer.com> wrote in message

news:MPG.1d230970f...@news.microsoft.com...

news.microsoft.com

unread,
Jun 23, 2005, 9:27:29 AM6/23/05
to
Thanks. I added the constraint to my code.

"Jay B. Harlow [MVP - Outlook]" <Jay_Har...@msn.com> wrote in message
news:ebsP$O2dFH...@TK2MSFTNGP15.phx.gbl...

Jay B. Harlow [MVP - Outlook]

unread,
Jun 27, 2005, 9:36:28 AM6/27/05
to
Robert,
I have not heard back from the AskFxCop alias, however I did here back on a
different mailing list.

The thought on the other list is that using the type parameter to
encapsulate a downcast (as in our functions) is a "good" reason to exclude
the message you mention.

' GOOD: We are using the type parameter to encapsulate a downcast
Private Shared Function GetAttribute(Of T As Attribute)() As T
Dim thisAssembly As Assembly = Assembly.GetExecutingAssembly()
Return GetAttribute(Of T)(thisAssembly)
End Function

' BAD: We are using the type parameter in place of a regular parameter.

Private m_table As List<Type>

Private Shared Sub SetAttribute(Of T As Attribute)()
m_table.Add(GetType(T))
End Function

Hope this helps
Jay

"news.microsoft.com" <rob...@zurer.com> wrote in message
news:O1%23TRd$dFHA...@tk2msftngp13.phx.gbl...

Jay B. Harlow [MVP - Outlook]

unread,
Jul 3, 2005, 10:21:02 PM7/3/05
to
Robert,
I got the following link back from the AskFxCop alias:

http://www.gotdotnet.com/community/messageboard/Thread.aspx?id=311608

It provides some rational for the rule & for its exceptions.

Hope this helps
Jay

"news.microsoft.com" <rob...@zurer.com> wrote in message
news:O1%23TRd$dFHA...@tk2msftngp13.phx.gbl...

0 new messages