What is the best way to write this generic method?

R

Robert Zurer

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
 
J

Jay B. Harlow [MVP - Outlook]

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


| 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
 
R

Robert Zurer

Thanks for your response. A good idea
I'll post this on the Visual C# Language Forum
 
J

Jay B. Harlow [MVP - Outlook]

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

| Thanks for your response. A good idea
| I'll post this on the Visual C# Language Forum
|
|
| > 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.
| >
 
J

Jay B. Harlow [MVP - Outlook]

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

| Thanks. I added the constraint to my code.
|
| | > 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
| >
| > | > | Thanks for your response. A good idea
| > | I'll post this on the Visual C# Language Forum
| > |
| > |
| > | > 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.
| > | >
| >
| >
|
|
 
J

Jay B. Harlow [MVP - Outlook]

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

| Thanks. I added the constraint to my code.
|
| | > 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
| >
| > | > | Thanks for your response. A good idea
| > | I'll post this on the Visual C# Language Forum
| > |
| > |
| > | > 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.
| > | >
| >
| >
|
|
 
Top