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